如何在twig文件中的字段上附加计数器变量?

时间:2013-10-09 05:06:27

标签: html symfony for-loop twig

我在twig文件中创建一个表,我想通过循环填充数据,但我的字段名称相同,所以我想在每个字段名称附加一个计数器变量,我是怎么做的?

这是我的代码:

<div id="table">
    <table class="table table-bordered">
    <thead>
        <tr>
            <th style="text-align:left; width:120px;">Item</th>
            <th style="text-align:left; width:200px;">Description</th>
            <th style="width:100px;">Unit Cost</th>
            <th style="text-align:right; width:60px;">Qty</th>
            <th style="text-align:left; width:100px;">Tax</th>
            <th style="text-align:left; width:100px;">Tax</th>
            <th style="text-align:right; width:100px;">Line Total</th>
       </tr>
    </thead>
    <tbody>
    {% for items in itemdata %}         
     <tr>        
     <td colspan=7 width=800>
     <select class="items" name="items" style="width:127px; float:left;" id="items"><option value="1" selected="selected" disabled="disabled"></option></select>
     <textarea name="description" id="description" class="description" style="float:left; display: block; height: 30px; width:209px; border-radius:0px; margin: -1px 1px 0;">{{ items.description }}</textarea>
     <input type="text" name="unitprice" id="unitprice" class="unitprice" value="{{ items.unitPrice }}" style="float:left; display:block; height:30px; width:106px; border-radius:0px; margin:-1px -1px 0;">
     <input type="text" name="quantity" id="quantity" class="quantity" value="{{ items.quantity }}" style="float:left; display: block; height: 30px; width:64px; border-radius:0px; margin: -1px 1px 0;">
     <select name="firsttax" id="firsttax" style=" float:left; display: block; height: 31px; width:106px; border-radius:0px; margin: -2px -1px 0;"><option value="1" selected="selected" ></option></select>
     <select name="secondtax" id="secondtax" style="float:left; display: block; height: 31px; width:107px; border-radius:0px; margin: -2px 0px 0;"><option value="1" selected="selected"></option></select>
     <input type="text" name="linetotal" id="linetotal" class="linetotal" value="{{ items.linetotal }}" placeholder="0.00" readonly style="float:right; display: block; height: 31px; width:107px; border-radius:0px; background-color: #F0F0F0; text-align:right; margin: -31px -1px 0;">       
     </td>
     </tr>
     <input type="hidden" id="itemscounter" name="itemscounter" value=""/>
     {% endfor %}   

我的字段名称相同,这是截图:

enter image description here

如何更改字段名称,如description,description2,description3,description4等.....

哪里

itemscounter value = number of rows

1 个答案:

答案 0 :(得分:3)

如果你只是在循环中寻找它的索引:

<textarea name="description{{ loop.index }}">

Here's the docs.