Problem check/uncheck into datatable column

Problem check/uncheck into datatable column

micheld88micheld88 Posts: 12Questions: 3Answers: 0

Hello !

I'm using DataTables 1.10.5 with jQuery v1.11.1.

I want to check/uncheck all checkboxes into a datatable column in one click, but it doesn't work as expected.
Just after page load, clicking the button checks all boxes then clicking again unchecks all but additional clicks do nothing.

Here is the code I'm using

            $('#checkall').click( function() {
                oTable.find('input').each( function(i) {
                    console.log('row # '+i);
                    if ($(this).prop("checked") == true) {
                        $(this).attr("checked", false);
                        //$(this).removeAttr("checked");
                    } else {
                        $(this).attr("checked", true);
                    }
                } );
                return false; // prevent page refresh
            } );

I tried removeAttr (commented line above) but same effect.

Any idea of what I'm doieng wrong ?

Thanks
Michel.

Answers

  • micheld88micheld88 Posts: 12Questions: 3Answers: 0
    edited May 2015

    Hello !

    I've changed the 'click' method to 'change' and reorganized the source and it works now:

                $('#checkAll').change( function() {
                    oTable.find('input').each( function(i) {
                        if ($(this).attr('name') != 'checkAll') {
                          var isSelected = $(this).is(':checked');
                          if(isSelected){
                              $(this).prop("checked", false);
                          } else {
                              $(this).prop("checked", true);
                          }
                        }
                    } );
                    return false; // prevent page refresh
                } );
    
This discussion has been closed.