SelectAll + disable other button

SelectAll + disable other button

bgApebgApe Posts: 5Questions: 3Answers: 0

Hi, I'm using the datatables built in selectAll button and I want it to select all rows AND disable the delete button I have added to the page. I'm using this code:

`var table = $('#data-table-combine').DataTable();

        var buttons = new $.fn.dataTable.Buttons(table, {
            buttons: [
                { extend: 'selectAll', text: 'Select All Rows', action: function (e, dt, button, config) { document.getElementById('btnDelete').disabled = true } },  

`

This allows the Select All Rows button to disable the Delete button but it will not select all rows. If I remove the action: function() then it does select the rows. Isn't it possible to do both? Or is there something wrong with my code?

Any help is greatly appreciated!

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    You will probably need to do something like the last example in the buttons.buttons.action docs to execute the selectAll default action after executing the disable button code.

    Kevin

  • bgApebgApe Posts: 5Questions: 3Answers: 0

    Thank you for your help. That makes sense however, a user could possibly still select all rows manually and get around that. Is there a way to disable the Delete button when all rows are selected? I tried both options below but can't get it to work.

    var selectedRows = table.rows( { selected: true } ).count();
    var allRows = table.rows().count()
    
    table.button(1).disable( selectedRows === allRows );
    
    if (table.rows({ selected: true }).indexes().length == table.rows().indexes().length) {
    table.button(1).disable();
    }
    
  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Its hard to say without seeing exactly what you are doing. Your first code snippet seems to work here:
    http://live.datatables.net/yemowara/1/edit

    In order for us to help debug please post a link to your page or a test case replicating the issue.

    Kevin

  • bgApebgApe Posts: 5Questions: 3Answers: 0

    Thank you. If needed I will create a test case for further help

This discussion has been closed.