Heim  >  Fragen und Antworten  >  Hauptteil

Lenkerstruktur

Ich habe eine Tabelle mit einer Lenkervorlage. Die #each,在下一行中声明了标记,在最后一行中完成了#each-Abschlussdeklaration wird in der ersten Zeile der Tabelle deklariert.

<table style="border-collapse: collapse; width: 100%;" border="1"><colgroup><col><col><col><col></colgroup>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4">{{#each itemList}}</td>
</tr>
<tr>
<td>{{name}}</td>
<td>{{price}}</td>
<td>{{qty}}</td>
<td>{{total}}</td>
</tr>
<tr>
<td colspan="4">{{/each}}</td>
</tr>
</tbody>
</table>

Nachdem die kompilierte Vorlage ausgeführt wurde, sieht die generierte Ausgabe wie unten dargestellt aus. In der Tabelle, in der jede Zeile definiert ist, werden die erste und die letzte Zeile nicht gelöscht. Gibt es eine Möglichkeit, es zu entfernen?

<table style="border-collapse: collapse; width: 100%;" border="1" data-mce-style="border-collapse: collapse; width: 100%;">
  <colgroup>
    <col>
    <col>
    <col>
    <col>
  </colgroup>
  <thead>
    <tr>
      <th>a</th>
      <th>c</th>
      <th>v</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="4">
        <tr>
          <td>12</td>
          <td>3</td>
          <td>2</td>
          <td>2</td>
        </tr>
         <tr>
            <td colspan="4"></td>
         </tr>
      </td>
    </tr>
  </tbody>
</table>

P粉478445671P粉478445671377 Tage vor475

Antworte allen(1)Ich werde antworten

  • P粉011912640

    P粉0119126402023-09-11 11:07:02

    我不确定您为什么要包装 #each 调用,如 {{#each itemList}}。这将产生完全损坏的 HTML。

    您希望 itemList 中的每个项目都有一个 ,因此您需要确保行标记 < code> 和 #each 包含,并且每个的外部的任何标记都是有效且封闭的 :

    const template = Handlebars.compile(`
    <table style="border-collapse: collapse; width: 100%;" border="1">      <colgroup><col><col><col><col></colgroup>
       <thead>
         <tr>
           <th>Name</th>
           <th>Price</th>
           <th>Quantity</th>
           <th>Total</th>
         </tr>
       </thead>
       <tbody>
         <tr>
           <td colspan="4"></td>
         </tr>
         {{#each itemList}}
           <tr>
             <td>{{name}}</td>
             <td>{{price}}</td>
             <td>{{qty}}</td>
             <td>{{total}}</td>
           </tr>
         {{/each}}
         <tr>
           <td colspan="4"></td>
         </tr>
      </tbody>
    </table>
    `);
    
    const data = {
      itemList: [
        {
          name: 'One',
          price: 1,
          qty: 1,
          total: 1
        },
        {
          name: 'Two',
          price: 2,
          qty: 2,
          total: 4
        }
      ]
    };
    
    const output = template(data);
    
    document.body.innerHTML = output;
    <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>

    Antwort
    0
  • StornierenAntwort