Javascript 数据表:未捕获的类型错误:无法读取未定义的属性“按钮”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35517667/
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
DataTables: Uncaught TypeError: Cannot read property 'buttons' of undefined
提问by Jeromy French
I am having trouble setting up a custom dataTables that uses the Buttons plugin.
我在设置使用 Buttons 插件的自定义数据表时遇到问题。
I can set up a custom defaultdom
layout that works:
我可以设置一个有效的自定义默认dom
布局:
//vanilla dom (frtip...)
$.extend($.fn.dataTable.defaults, {
dom: 'frtip'
});
But if I try to include the "B" character in the dom
layout:
但是,如果我尝试在dom
布局中包含“B”字符:
// Invoke Buttons plugin (Bfrtip...)
$.extend($.fn.dataTable.defaults, {
dom: 'Bfrtip'
});
...then run dataTables, this JavaScript error is reported:
...然后运行dataTables,报这个JavaScript错误:
Uncaught TypeError: Cannot read property 'buttons' of undefined
未捕获的类型错误:无法读取未定义的属性“按钮”
What am I doing wrong?
我究竟做错了什么?
Please see an example of this at https://jsfiddle.net/jhfrench/at83rcoL/
回答by Jeromy French
I figured it out while drafting this question. Sharing the hard-won answer here:
我在起草这个问题时想通了。在这里分享来之不易的答案:
It is not enough to just include the relevant JS assets (jquery.dataTables.min.js, dataTables.buttons.min.js, etc). You alsohave to invoke the Buttons plugin by either extending the defaults with the button
object element:
仅仅包含相关的 JS 资产(jquery.dataTables.min.js、dataTables.buttons.min.js 等)是不够的。您还必须通过使用button
object 元素扩展默认值来调用 Buttons 插件:
// Invoke Buttons plugin (Bfrtip...)
$.extend($.fn.dataTable.defaults, {
buttons: [ 'copy', 'csv', 'excel' ]
});
Or you can call it at dataTable()
initialization:
或者你可以在dataTable()
初始化时调用它:
$("#table2").DataTable({
buttons: [
'copy', 'excel', 'pdf'
]
});
See https://jsfiddle.net/jhfrench/at83rcoL/8/for examples of both approaches working.