【发布时间】:2017-01-26 11:49:37
【问题描述】:
我在DataTable 的底部有一排按钮,如下所示。但现在我想在表格顶部添加一个单独的按钮colVis。
在this example 之后,我尝试了以下方法,指定要放置在dom: 'Bfrtip' 的新按钮,但我收到错误 - Uncaught TypeError: Cannot read property 'tag' of undefined at new m (dataTables.buttons.min.js:6)
问题:
如何将数据表按钮与按钮组分开显示?
var historyTable = $('#escalation').DataTable({
"order": [[10, "desc"]],
colReorder: true,
columnDefs: [
{
orderable: false,
targets: [7, 11, 12, 13, 14]
},
{
"width": "15%",
"targets": [7, 11]
}
],
responsive: true,
buttons: [
{
extend: 'pdf',
footer: true,
exportOptions: {
columns: [1, 2, 5, 6, 11]
}
},
{
extend: 'print',
footer: true,
exportOptions: {
columns: [1, 2, 5, 6, 11]
}
},
'copy', 'csv', 'excel'
],
'iDisplayLength': 55,
});
new $.fn.DataTable.Buttons(historyTable, {
dom: 'Bfrtip',
buttons: [
{
extend: 'colvis'
},
]
});
historyTable.buttons(1, null).container().appendTo(
historyTable.table().container()
);
我还尝试了以下方法,为 colVis 创建了一个新按钮,并在表格顶部附加了一个 ID 为 colFilter 的 div,但无济于事:
//Place colVis filter button at top of table
var colvis = new $.fn.dataTable.Buttons(historyTable, {
dom: 'Bfrtip',
buttons: [
{
extend: 'colvis'
}
]
});
colvis.container().appendTo('#colFilter');
并在第三次尝试中尝试了这种方法,解释了here:
//Place colVis filter button at top of table
var colvis = new $.fn.dataTable.Buttons(historyTable, {
buttons: [
{
extend: 'colvis',
postfixButtons: ['colvisRestore']
}
]
});
historyTable.buttons(3, null).container().appendTo('#colFilter');
这样的例子:
【问题讨论】:
-
这是一个更强大的解决方案:stackoverflow.com/a/34577139/3393929
标签: jquery button datatables