2011-02-01 10 views
1

別のチェックボックスの質問。私はリストに項目を持っています。各項目にはチェックボックスがあります。私がしたいのは、FIRSTの項目のチェックボックスをチェックすることです。それはchecked="checked"のためにすべてのチェックボックスにチェックを入れました。ジャンゴ{% for %}タグによって設定されたDjangoチェックボックスは、最初のボックスのみをチェックしたい

{% for item in items %} 
      <tr class="items_table_row"> 
        <td><input type="checkbox" name="{{item.pk}}" value="{{item.pk}}" checked="checked"></td> 
        <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td><td>{{item.type}}</td><td>{{item.format}}</td> 
        <td><span id="{{item.pk}}" name="type">{{item.itemstatushistory_set.latest}}</span></td><td>{{item.itemstatushistory_set.latest.date.date|date:"d M Y"}}</td> 
        <td><a href="{% url tiptop.views.edit_item item.client.pk item.pk %}" onclick="return showAddAnotherPopup(this);">Edit</a></td> 
      </tr> 
    {% endfor %} 

答えて

1

すなわち

{% if forloop.first %} checked="checked"{% endif %} 

{% ifequal forloop.counter 2 %} checked="checked"{% endifequal %} 

をデフォルトforloop.counterが1インデックスされます0指数カウンタを使用することができます。

forloop.counter0 
+0

大丈夫です。これは私が探しているものです。 – Shehzad009

+0

参考までに - Django 1.2(または1.1の場合もある)から、{%ifloal forloop.counter 2%}は{%if forloop.counter == 2%}として書き直すことができます。 –

1

The forloop variableここにあなたの友達です。

でこれをポップ:あなたのような2番目の項目にチェックをプロパティを追加することができ

{% for item in items %} 
    <tr class="items_table_row"> 
     <td><input type="checkbox" name="{{item.pk}}" value="{{item.pk}}"{% if forloop.first %} checked="checked"{% endif %}></td> 
     <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td><td>{{item.type}}</td><td>{{item.format}}</td> 
     <td><span id="{{item.pk}}" name="type">{{item.itemstatushistory_set.latest}}</span></td><td>{{item.itemstatushistory_set.latest.date.date|date:"d M Y"}}</td> 
     <td><a href="{% url tiptop.views.edit_item item.client.pk item.pk %}" onclick="return showAddAnotherPopup(this);">Edit</a></td> 
    </tr> 
{% endfor %} 
+0

これはありがとうございました。私のリストの2番目の項目だけをチェックしたい場合はもう1つ。私は{%if forloop.second%}とタイプすることはできませんので、どうすればいいですか? – Shehzad009

+0

@ Shehzad009:確かに '{%if forloop.counter == 2%}'になります。 '{%for%}'タグの他の機能をドキュメントで見ることができます:http://docs.djangoproject.com/en/dev/ref/templates/builtins/#for –

関連する問題