【问题标题】:How to attach counter variable on field in twig file?如何在树枝文件中的字段上附加计数器变量?
【发布时间】:2013-10-09 05:06:27
【问题描述】:

我在 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 %}   

我的字段名称在这里是截图:

如何更改描述、描述2、描述3、描述4等字段名称.....

在哪里

itemscounter value = number of rows

【问题讨论】:

    标签: html symfony for-loop twig


    【解决方案1】:

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

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

    Here's the docs.

    【讨论】:

    • 如果您希望 itemscounter 显示编号。可以放在 for 循环之外的项目:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2011-11-11
    相关资源
    最近更新 更多