django checkbox select all

丶灬走出姿态 提交于 2019-12-08 18:37:27

You have to use javascript for that. This is an example using jquery...

$(".checkbox_delete").attr('checked', true);

put that in the click event of the header checkbox and add that class (checkbox_delete) to all the checkboxes you want checked.

Check this one: you'll be able to toggle master checkbox selection as well in case you click on item checkbox:

$(function() {

  var masterCheckbox = $('#select_all');
  var slaveCheckboxes = $('.checkbox_delete');

  masterCheckbox.click(function() {
    slaveCheckboxes.attr('checked', masterCheckbox.attr('checked'));
  });

  slaveCheckboxes.click(function() {
      // Check all slave checkboxes selections: in case all are checked - check the master checkbox as well
      masterCheckbox.attr('checked', $.grep(slaveCheckboxes, function(e) {
          return $(e).attr('checked');
      }).length == slaveCheckboxes.length);
  });

});

You've no way but Javascript. Once the page is loaded you cannot do anything by django. A stupid method can be to attach a handler to the master checkbox to reload the page and set them checked by django but it's a very very bad idea.

If you're using some library like mootools or jquery it's easy to achieve.

As second hint, take a look at django forms, they make your life easier when working with everything near forms.

I use jquery and write this, but it is nouse when i click 'select_all'...

run.html

<script type="text/javascript" >
$(document).ready( function () {
  $('#select_all').click( function() {
    $(".checkbox_delete").attr('checked', true);
  });
}
</script>

<form name="form" method="post" action="/home/{{build}}/">
<br>
<input type="submit" value="Delete" style="margin-left:149px; width:80px; height:30px">
<input type="hidden" name="build_id" value="{{build_id}}" />
<table border="1"; style="margin-left:150px; border-collapse:collapse;margin-top:10px"; cellpadding="4" borderColor=black>
<tr bgcolor=#888888>
<td><input id="select_all" type="checkbox" align="center"></td>
<td><b>Run</b></td>
<td><b>Product</b></td>
</tr>

{% for run in run_list %}
    <tr>
    <td><input type="checkbox" name="var_delete" value="{{run.id}}" class="checkbox_delete"></td>
    <td><a href="/home/{{build}}/{{run.name}}">{{build}} {{run.name}}</a></td>
    <td>{{run.build.version}}</td>
    </tr>
{% endfor %}
</table>
</form>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!