mercredi 7 août 2019

Could you help how to make button enable or disable when theres a checked table in Datatable.js? i

anyone who use the datatable.js here? Can anyone help me? i have a code here that makes button enable when some rows in table is checked and disabled when theres no checked rows. (datatables.js has define pagination right?).

My problem is, on the 1st page: it works fine, when i checked or unchecked any rows the button enables and disables just as what i want. but on other pages other than page 1, when i check or uncheck ay rows. theres nothing happenning on the button. it stays disable. please anyone help me. I luv u guyss. Thanks in advance and godbless.

$(document).ready(function(){

GetAllUsers = () =>{ $.ajax({ type: 'POST', dataType: "json", url: './controllers/classfunction/admin/admincontroller.php', data: {mode:1}, beforeSend: function() { $('#loadingmodal').modal('show'); }, success: function(result){ $('button[id=btn-manage-privillege]').attr("disabled", true); $('button[id=btn-manage-account]').attr("disabled", true); let html = JSON.parse(result[0]); employees = result[2]; var table = $('#dataTable').DataTable();

    table.rows().remove();

    $(html).each(function(i){
        let ctr = html[i]["ctr"];
        let id = html[i]["empno"];
        let name = html[i]["empname"];
        let assign = html[i]["assigndept"];
      let privillege = html[i]["privilleges"];
        table.row.add(['<div class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" id="'+id+'"><label class="custom-control-label" for="'+id+'">'+ id +'</label></div><span name="user-list-ctr" style="display: none;">' +ctr+'</span>',name,assign,privillege,'<button class="btn btn-primary"><span class="fa fa-history"></span> Logs</button>']).draw( false );
     });

        $('#user-list-departments-selectAll').click(function (e) {
                $('table[id=dataTable]').find('td input:checkbox').prop('checked', this.checked);
                var countchecked = $("#dataTable input[type=checkbox]:checked").length;
            if(countchecked > 0){
                $('button[id=btn-manage-privillege]').attr("disabled", false);
                $('button[id=btn-manage-account]').attr("disabled", false);
            }
            else{
                $('button[id=btn-manage-privillege]').attr("disabled", true);
                $('button[id=btn-manage-account]').attr("disabled", true);
            }
    });


        $('table[id=dataTable]').find('td input:checkbox').click(function (e) {
            $('#user-list-departments-selectAll').prop('checked', false);
            var countchecked = $("#dataTable input[type=checkbox]:checked").length;
            if(countchecked > 0){
                $('button[id=btn-manage-privillege]').attr("disabled", false);
                $('button[id=btn-manage-account]').attr("disabled", false);
            }
            else{
                $('button[id=btn-manage-privillege]').attr("disabled", true);
                $('button[id=btn-manage-account]').attr("disabled", true);
            }
          });
  },
  error: function (xhr, ajaxOptions, thrownError) {

    },
  complete: function() {
    $('#loadingmodal').modal('hide');
  }
});
}

GetAllUsers(); });




Aucun commentaire:

Enregistrer un commentaire