使行选择成为 jQuery DataTables 的内置功能
在此页面上
可以看到以下代码附加了一个单击 tbody 的处理程序以在初始化实际数据表对象之前实现行选择功能:
$("#example tbody").click(function(event) {
$(oTable.fnSettings().aoData).each(function (){
$(this.nTr).removeClass('row_selected');
});
$(event.target.parentNode).addClass('row_selected');
});
/* Init the table */
oTable = $('#example').dataTable();
由于我的项目中的所有数据表都需要此功能,因此我希望这些代码自动运行在每个数据表上对象初始化。
我在这里找到了一个可能的地方(这是fnInitComplete
事件),可以在其中添加这些代码:
但是,代码应该默认运行,而不是像示例那样将它们传递给选项对象中的 fnInitComplete
事件。
您认为这可以如何做到?
非常感谢大家。
编辑:
最后,我决定定义一个全局表选项对象,并在使用它初始化数据表之前克隆它并在必要时修改它,如下所示:
//default table options defined globally, you can namespace it if you like
var jqDataTablesDefaultOptions:{
"fnDrawCallback": function (oSettings, json) {
var nTrs = this.fnGetNodes();
$(nTrs).click(
function(){
$(nTrs).removeClass('row_selected');
$(this).addClass('row_selected');
}
);
},
"aLengthMenu": [5,10,15,20,100],
"iDisplayLength":5,
"oLanguage": {
"sUrl": "/assets/lib/DataTables-1.8.2/media/language/zh_TW.txt"
},
"bJQueryUI": true
"sPaginationType":"full_numbers"
};
在我使用数据表的每个页面上,我将有以下代码:
//copy the default options
var tableOptions=$.extend(true,{},jqDataTablesDefaultOptions);
//modify the options if necessary
tableOptions.iDisplayLength=10;
//init the datatable
$('#example').dataTable(tableOptions);
请随意阅读此页面我使用 fnDrawCallback
而不是 fnInitComplete
的原因。
On this page
You can see that the following codes attach a click
handler to the tbody
to implement the row selection feature before the actual datatable object is inited:
$("#example tbody").click(function(event) {
$(oTable.fnSettings().aoData).each(function (){
$(this.nTr).removeClass('row_selected');
});
$(event.target.parentNode).addClass('row_selected');
});
/* Init the table */
oTable = $('#example').dataTable();
Since I need this feature for all datatables in my project, I would like these codes to run automatically on every datatable object initialization.
I have found a possible place here (which is the fnInitComplete
event) where these codes can be added:
However, the codes should run by default, rather than like the example, passing them to the fnInitComplete
event in the option object.
How do you think this can be done?
Many thanks to you all.
EDIT:
In the end, I have decided to define a global table option object, and I clone it and modify it if necessary before using it to init the dataTable like this:
//default table options defined globally, you can namespace it if you like
var jqDataTablesDefaultOptions:{
"fnDrawCallback": function (oSettings, json) {
var nTrs = this.fnGetNodes();
$(nTrs).click(
function(){
$(nTrs).removeClass('row_selected');
$(this).addClass('row_selected');
}
);
},
"aLengthMenu": [5,10,15,20,100],
"iDisplayLength":5,
"oLanguage": {
"sUrl": "/assets/lib/DataTables-1.8.2/media/language/zh_TW.txt"
},
"bJQueryUI": true
"sPaginationType":"full_numbers"
};
On every page that I use datatable, I will have the following codes:
//copy the default options
var tableOptions=$.extend(true,{},jqDataTablesDefaultOptions);
//modify the options if necessary
tableOptions.iDisplayLength=10;
//init the datatable
$('#example').dataTable(tableOptions);
Please feel free to read this page for the reason I use fnDrawCallback
rather than fnInitComplete
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会制作自己的插件并使用 jQuery $.extend 函数来扩展您使用所需默认值设置的选项。如果您不想为此创建插件,您可以使用 DataTools 插件。
另一个选择是编辑 DataTable js 文件 - 在代码中搜索“classSettings”。但显然这有一个很大的缺点,如果你想升级到新版本,你必须重新编辑它。
对于这种东西,我宁愿使用 fnRowCallback 因为它有 table tr doom 元素作为参数您可以绑定任何您想要的事件。但如果您想使用任何 jQuery 功能,则必须将其包装到 jQuery。
I would make my own plugin and employ jQuery $.extend function to extend options you set with defaults you want. If you don't want to bother making plugin for this, you may use a DataTools plugin.
Another option to go is edit DataTable js file - search for "classSettings" in the code. But obviously this has big disadvantage that if you want to upgrade to new version, you have to edit it again.
And for this kind of stuff I would rather use fnRowCallback as it has table tr doom element as an argument to which you may bind any event you want. But you have to wrap it to jQuery if you want to use any jQuery functionality.