0

我有一个问题,我的代码是这样的:

{% for order in aFilteredIgDetails %}
    {% if order.availability == 0 %}
        <tr>
             <td colspan="3" style="background: #FF0000;color:#ffffff">Available</td>
         </tr>
        <tr>
            <td>
                {{ order.gift_id }}
            </td>
            <td>
                {{ order.idm }}
            </td>
            <td>
                {{ order.email }}
            </td>
        </tr>
    {% else %}
        <tr>
            <td colspan="3" style="background: #0000ff;color:#ffffff">Unavailable</td>
        </tr>
        <tr>
            <td>
                {{ order.gift_id }}
            </td>
            <td>
                {{ order.idm }}
            </td>
            <td>
                {{ order.email }}
            </td>
        </tr>
    {% endif %}
{% endfor %}

一切正常,问题是如果我有多个 order.availability == 0 总是对于我在顶部的每一行<tr>background : #FF0000;能帮帮我吗?提前谢谢。有人有想法吗?请帮帮我!!!!!!!

4

2 回答 2

0

您可以使用变量来检测它是否是可用性为 0 的第一个订单:

{% set first = true %}
{% for order in aFilteredIgDetails %}
    {% if order.availability == 0 and first == true %}
        <tr>
          <td colspan="3" style="background: #FF0000;color:#ffffff">Available</td>
        </tr>
    {% set first = false %}
    {% else %}
        <tr>
            <td colspan="3" style="background: #0000ff;color:#ffffff">Unavailable</td>
        </tr>
    {% endif %}
    <tr>
        <td>
            {{ order.gift_id }}
        </td>
        <td>
            {{ order.idm }}
        </td>
        <td>
            {{ order.email }}
        </td>
    </tr>
{% endfor %}
于 2015-07-02T07:26:17.863 回答
0

我找到了一个解决方案:

{% if aFilteredIgDetails %}
                {% for order in aFilteredIgDetails %}
                    {% if order.availability == 0 %}
                    {% set counter_unavail = ( counter_unavail | default(0) ) + 1 %}
                    {% if counter_unavail == 1 %}
                    <tr>
                         <td colspan="3" style="background: #FF0000;color:#ffffff">Unaivailable</td>
                     </tr>
                    {% endif %}
                    <tr>
                        <td>
                            {{ order.gift_id }}
                        </td>
                        <td>
                            {{ order.idm }}
                        </td>
                        <td>
                            {{ order.email }}
                        </td>
                    </tr>
                    {% elseif order.availability == 1 %}
                        {% set counter_avail = ( counter_avail | default(0) ) + 1 %}
                        {% if counter_avail == 1 %}
                        <tr>
                            <td colspan="3" style="background: #0000ff;color:#ffffff">Available</td>
                        </tr>
                        {% endif %}
                        <tr>
                            <td>
                                {{ order.gift_id }}
                            </td>
                            <td>
                                {{ order.idm }}
                            </td>
                            <td>
                                {{ order.email }}
                            </td>
                        </tr>
                    {% endif %}
                {% endfor %}
            {% else %}
                <td colspan="3">---</td>
            {% endif %}
于 2015-07-02T07:56:07.387 回答