【问题标题】:How to conditionally display TableTools buttons如何有条件地显示 TableTools 按钮
【发布时间】:2015-09-04 10:18:00
【问题描述】:

我正在使用 jQuery DataTables 和 TableTools 扩展在表格标题中显示按钮。但是,当满足某些条件时,是否有显示按钮的选项?

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

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();
                }
            }
        ]
    }
});

我只想在用户拥有正确权限的情况下显示“新建项目”按钮。有没有可能?

【问题讨论】:

    标签: jquery datatables tabletools


    【解决方案1】:

    由于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
       }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-09-18
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2023-04-11
      • 2020-10-06
      相关资源
      最近更新 更多