最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to select all checkbox in Datatables - Stack Overflow

programmeradmin2浏览0评论

I want to select all my checkbox

I have an input in header of datatables like this

  <th style="width: 25%" ><input type="checkbox" name="checkall" class="select-checkall" id="checkall" value=""></th>

i use it like this to checked it but only work in the page that iam

   $("#checkall").click(function(){
            $(':checkbox').prop('checked', true);                       
     });  

check this fiddle /

I want to select all my checkbox

I have an input in header of datatables like this

  <th style="width: 25%" ><input type="checkbox" name="checkall" class="select-checkall" id="checkall" value=""></th>

i use it like this to checked it but only work in the page that iam

   $("#checkall").click(function(){
            $(':checkbox').prop('checked', true);                       
     });  

check this fiddle http://jsfiddle/juxHn/46/

Share Improve this question edited Nov 10, 2015 at 11:39 Mathias Stavrou asked Nov 10, 2015 at 11:15 Mathias StavrouMathias Stavrou 8811 gold badge8 silver badges16 bronze badges 3
  • can you create a fiddle for this? – Anoop Joshi P Commented Nov 10, 2015 at 11:17
  • stackoverflow./a/23675009/4161269 – Anik Islam Abhi Commented Nov 10, 2015 at 11:20
  • i try this but not working stackoverflow./questions/23637586/… check this fiddle jsfiddle/juxHn/2 – Mathias Stavrou Commented Nov 10, 2015 at 11:34
Add a ment  | 

1 Answer 1

Reset to default 5

jQuery will not be able to find checkboxes in other pages than the current one because DataTables somehow hides them (maybe removes them from DOM?)

Please refer to DataTables API to access your table cells irrespective of which ones are currently shown or not / of which page you are on.

In your case, you could do:

// Use "DataTable" with upper "D"
oTableStaticFlow = $('#flow-table').DataTable({
    "aoColumnDefs": [{
        'bSortable': false,
        'aTargets': [0]
    }],
});

$("#flowcheckall").click(function () {
    var cells = oTableStaticFlow.column(0).nodes(), // Cells from 1st column
        state = this.checked;

    for (var i = 0; i < cells.length; i += 1) {
        cells[i].querySelector("input[type='checkbox']").checked = state;
    }
});

Demo: http://jsfiddle/juxHn/47/

发布评论

评论列表(0)

  1. 暂无评论