开发者

Django checkbox want to check only the first box

开发者 https://www.devze.com 2023-02-07 18:10 出处:网络
another checkbox question. I have items in a list. Each item has a checkbox. What I want to do is to tick the FIRST item\'s checkbox in the list. Right know, it has ticked all checkbox\'s because of c

another checkbox question. I have items in a list. Each item has a checkbox. What I want to do is to tick the FIRST item's checkbox in the list. Right know, it has ticked all checkbox's because of checked="checked".

{% 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 %}


You could add the checked property to the second item as such:

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

The default forloop.counter is 1-indexed, or you can specifically use a 0-indexed counter:

forloop.counter0


The forloop variable set by the Django {% for %} tag is your friend here.

Pop this in:

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

i.e.

{% 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

精彩评论

暂无评论...
验证码 换一张
取 消