如何有条件地显示 TableTools 按钮

How to conditionally display TableTools buttons

我正在使用 jQuery DataTables 和 TableTools 扩展来显示 table header 中的按钮。但是是否可以选择在满足某些条件时显示按钮?

我的table初始化代码如下所示:

projectsTable = $('#projects_table').DataTable({
    "dom": '<"cleaner">lf<"cleaner">T<"cleaner"><"cleaner">rtip',
    "stateSave": true,        
    "data":tableData,
    "bSortCellsTop": true,
    "responsive": true,
    "autoWidth": false,
    "tableTools":{
        "aButtons": [
            {
                "sExtends": "text",
                "sButtonText": "New project",
                "fnClick": function (mButton, oConfig, oFlash){
                    addProjectDialog();
                }
            },{
                "sExtends": "text",
                "sButtonText": "Reset all filters",
                "fnClick": function (mButton, oConfig, oFlash){
                    resetAllFilters();
                }
            }
        ]
    }
});

我只想在用户具有正确权限时显示 "New project" 按钮。有可能吗?

因为aButtons是一个数组,可以这样解决:

var canUserCreateProjects = true;

// DataTables TableTools buttons options
var aButtonsData = [];

// If user can create projects
if(canUserCreateProjects){
   aButtonsData.push({
      "sExtends": "text",
      "sButtonText": "New project",
      "fnClick": function (mButton, oConfig, oFlash){
         addProjectDialog();
      }
   });
}

aButtonsData.push({
   "sExtends": "text",
   "sButtonText": "Reset all filters",
   "fnClick": function (mButton, oConfig, oFlash){
      resetAllFilters();
   }
});

// Initialize DataTable
var projectsTable = $('#projects_table').DataTable({
    "dom": '<"cleaner">lf<"cleaner">T<"cleaner"><"cleaner">rtip',
    "stateSave": true,        
    "data":tableData,
    "bSortCellsTop": true,
    "responsive": true,
    "autoWidth": false,
    "tableTools": {
        "aButtons": aButtonsData
   }
});