且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何禁用DataTables / TableTools按钮

更新时间:2023-12-04 11:35:16

这是一个很好的问题!似乎

  dataTable.tabletools()中的 fnClick  fnSettings ).buttonSet [id] .fnClick 

只是对其他地方存储的事件的引用,不可访问更改 fnClick 对API没有影响)。但是,您可以使用预定义的类 DTTT_disabled ,并检查您的 fnClick -handler中的

  var dataTable = $(#example)DataTable({
sDom:'TC'
oTableTools:{
aButtons:[{
sExtends:text,
sButtonText:Edit,
fnClick:function(nButton,oConfig,oFlash){
if($(nButton).hasClass('DTTT_disabled'))return;
alert('edit button clicked');
}
}]
}
});

示例,复选框启用或禁用按钮:



()($(this).is(':checked)($($) ')){
$('。DTTT_button_text')。removeClass('DTTT_disabled');
} else {
$('。DTTT_button_text' b $ b}
});

请参阅demo - > http://jsfiddle.net/ev2N2/


I'm using DataTable 1.10 and TableTools 2.2.1.

Given the following snipped I would like to disable/enable the edit-button.

var myTable = $("#myTable ").DataTable({
tableTools : {
    "aButtons" : [ {
        "sExtends" : "text",
        "sButtonText" : "Edit",
        "fnClick" : function(nButton, oConfig, oFlash) {
            /* some stuff */    
        }
    }]
  }
})

Is there a possibility to do this at runtime?

Thanks a lot

This was a good question! Seems that the fnClick in

dataTable.tabletools().fnSettings().buttonSet[id].fnClick 

only is a reference to the event stored elsewhere, not accessible (changing fnClick on the API has no effect). However, you can use the predefined class DTTT_disabled and check for that in your fnClick-handler :

var dataTable = $("#example").DataTable({
   sDom: 'TC',
   oTableTools : {
       aButtons : [{
            sExtends : "text",
            sButtonText : "Edit",
            fnClick :  function(nButton, oConfig, oFlash) {
                 if ($(nButton).hasClass('DTTT_disabled')) return;
                 alert('edit button clicked');
            }
       }]  
  }
});

example with a checkbox enabling or disabling the button :

$("#enable").click(function() {
    if ($(this).is(':checked')) {
        $('.DTTT_button_text').removeClass('DTTT_disabled');
    } else {
        $('.DTTT_button_text').addClass('DTTT_disabled');
    }
});

see demo -> http://jsfiddle.net/ev2N2/