如何有条件地显示 TableTools 按钮
Posted
技术标签:
【中文标题】如何有条件地显示 TableTools 按钮【英文标题】:How to conditionally display TableTools buttons 【发布时间】: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();
]
);
我只想在用户拥有正确权限的情况下显示“新建项目”按钮。有没有可能?
【问题讨论】:
【参考方案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
);
【讨论】:
以上是关于如何有条件地显示 TableTools 按钮的主要内容,如果未能解决你的问题,请参考以下文章
Datatables Tabletools 打印模式显示所有数据。只需要以前显示的内容