使用 jQuery DataTables 向工具栏添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32549987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a button to toolbar with jQuery DataTables
提问by Abrar Jahin
I am trying to add a button to my toolbar of my datatable. So, my datatableis:
我想一个按钮添加到我的我的工具栏上的数据表。所以,我的数据表是:
var dataTable = $('#employee-grid').DataTable(
{
processing: true,
serverSide: true,
ajax: "employee-grid-data.php", // json datasource for AJAX Data
"pagingType": "full_numbers", //Adding Last and First in Pagination
stateSave: true,
"language":{ //Custom Message Setting
"lengthMenu": "Display _MENU_ records per page", //Customizing menu Text
"zeroRecords": "Nothing found - sorry", //Customizing zero record text - filtered
"info": "Showing page _PAGE_ of _PAGES_", //Customizing showing record no
"infoEmpty": "No records available", //Customizing zero record message - base
"infoFiltered": "(filtered from _MAX_ total records)" //Customizing filtered message
},
"lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]], //For customizing number of data sets per page
});
And what I have done is like this:
我所做的是这样的:
$(document).ready(function()
{
var dataTable = $('#employee-grid').DataTable(
{
processing: true,
serverSide: true,
ajax: "employee-grid-data.php", // json datasource for AJAX Data
"pagingType": "full_numbers", //Adding Last and First in Pagination
stateSave: true,
"language":{ //Custom Message Setting
"lengthMenu": "Display _MENU_ records per page", //Customizing menu Text
"zeroRecords": "Nothing found - sorry", //Customizing zero record text - filtered
"info": "Showing page _PAGE_ of _PAGES_", //Customizing showing record no
"infoEmpty": "No records available", //Customizing zero record message - base
"infoFiltered": "(filtered from _MAX_ total records)" //Customizing filtered message
},
"lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]], //For customizing number of data sets per page
"dom": '<"toolbar">frtip'
});
$("div.toolbar").html('<button type="button" id="any_button">Click Me!</button>');
} );
But I am finding something like this:
但我发现这样的事情:
But I like to have something like this-
但我喜欢有这样的东西——
Can anyone please help?
有人可以帮忙吗?
回答by Gyrocode.com
SOLUTION
解决方案
Use the code below:
使用下面的代码:
JavaScript:
JavaScript:
var table = $('#example').DataTable({
// ... skipped ...
dom: 'l<"toolbar">frtip',
initComplete: function(){
$("div.toolbar")
.html('<button type="button" id="any_button">Click Me!</button>');
}
});
CSS:
CSS:
.toolbar {
float:left;
}
DEMO
演示
See this jsFiddlefor code and demonstration.
有关代码和演示,请参阅此 jsFiddle。
回答by Jai Gupta
You can also use datatable button.js. Here is the source link:
您还可以使用数据表 button.js。这是源链接:
https://datatables.net/extensions/buttons/examples/initialisation/custom.html
https://datatables.net/extensions/buttons/examples/initialisation/custom.html
Don't forget to add below libraries (as mentioned in the above URL)
不要忘记添加以下库(如上述 URL 中所述)
https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.jshttps://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js
https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js https://cdn.datatables.net/按钮/1.5.2/js/dataTables.buttons.min.js