2014-02-12 76 views
0

我試圖在員工關閉時隱藏字段中的評論。我已將自己的if標記添加到該字段本身,但在ifchange循環中,如果員工處於打開狀態,則它不會看到更改並在字段中添加註釋。我已將此移動到ifchange之上,但如果員工關閉,則不會顯示任何字段。所以基本上我的檢查跳過了第一個(小時數),但是if_changed忽略了第二個,因爲bar_record是相同的。我無法弄清楚這種方法。任何幫助是極大的讚賞!檢查模板標籤中is_off是否正在檢測更改

UPDATE

我得到這個固定的移動if not foo.is_off了DOM,但有人告訴我,這可以更短。 if即是。

CODE

{% for foo in bar_information.bar_hours %} 
     {% if not foo.is_off %} <!-- is there a shorter better way of writing this? --> 
     {% ifchanged foo.bar_record %} 
     <tr class="{% cycle 'rowEven' 'rowOdd' %}"> 
     <td> 
      {{ foo.bar_record.get_formatted_id }} 
     </td> 
     <td> 
      {{ foo.bar_record.entered_in|date:"DATETIME_FORMAT" }} 
     </td> 
     <td> 
      {{ foo.bar_record.get_display }} 
     </td> 
     <td> 
      {{ foo.bar_record.get_mistake_display }} 
     </td> 
     <td> 
      {% if foo.bar_record.number_shifts %} 
      {{ foo.bar_record.number_shifts }} 
      {% endif %} 
     </td> 
     <td> 
      <div style="width: 400px; word-wrap: break-word;"> 
       {{ foo.bar_record.comments }} <!-- THIS COMMENT IS WHAT I NEED TO HIDE IF EMPLOYEE IS OFF --> 
      </div> 
     </td> 
     <td align="right"> 
      <a class="button" 
      href="{% url 'bar_record_view' foo.bar_record.pk %}"> 
         View Record</a> 
      {% has_permission_cust CHECK_COMPLIANCE %} 
      {% url 'check_compliance' foo.bar_record.pk as check_compliance_url %} 
      <a href="{{ check_compliance_url }}" class="button">Check Compliance</a> 
      {% end_has_permission_cust %} 
     </td> 
     </tr> 
     {% endifchanged %} 
     {% endif %} 
    {% empty %} 
    {% endfor %} 
+1

foo.bar_record在每次循環迭代中如何更改?如果不改變,如果改變將始終回答錯誤。 – professorDante

回答

0

我發現移動 '如果沒有foo.is_off' 了DOM它的工作。因此:

{% for foo in bar_information.bar_hours %} 
    {% if not foo.is_off %} <!-- is there a shorter better way of writing this? --> 
    {% ifchanged foo.bar_record %} 
    <tr class="{% cycle 'rowEven' 'rowOdd' %}"> 
    <td> 
     {{ foo.bar_record.get_formatted_id }} 
    </td> 
    <td> 
     {{ foo.bar_record.entered_in|date:"DATETIME_FORMAT" }} 
    </td> 
    <td> 
     {{ foo.bar_record.get_display }} 
    </td> 
    <td> 
     {{ foo.bar_record.get_mistake_display }} 
    </td> 
    <td> 
     {% if foo.bar_record.number_shifts %} 
     {{ foo.bar_record.number_shifts }} 
     {% endif %} 
    </td> 
    <td> 
     <div style="width: 400px; word-wrap: break-word;"> 
      {{ foo.bar_record.comments }} <!-- THIS COMMENT IS WHAT I NEED TO HIDE IF EMPLOYEE IS OFF --> 
     </div> 
    </td> 
    <td align="right"> 
     <a class="button" 
     href="{% url 'bar_record_view' foo.bar_record.pk %}"> 
        View Record</a> 
     {% has_permission_cust CHECK_COMPLIANCE %} 
     {% url 'check_compliance' foo.bar_record.pk as check_compliance_url %} 
     <a href="{{ check_compliance_url }}" class="button">Check Compliance</a> 
     {% end_has_permission_cust %} 
    </td> 
    </tr> 
    {% endifchanged %} 
    {% endif %} 
{% empty %} 
{% endfor %}