How to disable the Remove button upon row elect when a value is > 0 ?

How to disable the Remove button upon row elect when a value is > 0 ?

idealcomsidealcoms Posts: 10Questions: 5Answers: 1

I defined the 2 buttons :

              buttons: [
                 { extend: "create", editor: editor },
                 { extend: "edit",  editor: editor },
                 { extend: "remove",  editor: editor }
              ]

on selecting a row , when a value is > 0 I need to avoid removing this row by disabling the Remove button ...

             var nb_users;
             table
              .on( 'select', function ( e, dt, type, indexes ) {
                var rowData = table.rows( indexes ).data().toArray();
                nb_users = rowData[0].nb_users;
                if (nb_users > 0) {
                  $('#groups_wrapper .dt-buttons a.buttons-remove:first').addClass('disabled');
                }

             .on( 'deselect', function ( e, dt, type, indexes ) {
                if (nb_users > 0) {
                  $('#groups_wrapper .dt-buttons a.buttons-remove:first').removeClass('disabled');
                }
              } );

but this doesn't work... any help or clue will be appreciated ...

This question has an accepted answers - jump to answer

Answers

  • aaronwaaronw Posts: 89Questions: 3Answers: 4
    edited November 2015 Answer ✓

    Instead of adding and removing the CSS class, call the datatables built in function on the button itself to disable and enable it:

    https://datatables.net/reference/api/button().disable()

    Or you could enable using false:

    https://datatables.net/reference/api/button().enable()

  • idealcomsidealcoms Posts: 10Questions: 5Answers: 1

    Thanks .. it's working

    on select

                    removeBtn = $('#groups_wrapper .dt-buttons a.buttons-remove:first');
                    if (nb_users > 0 && !removeBtn.hasClass( "hidden")) {
                      $('#groups_wrapper .dt-buttons a.buttons-remove:first').addClass( "hidden");
                    }
    
                  .on( 'deselect', function ( e, dt, type, indexes ) {
                    if (removeBtn.hasClass( "hidden")) {
                      $('#groups_wrapper .dt-buttons a.buttons-remove:first').removeClass( "hidden" );
                    }
                  } );
    
This discussion has been closed.