【发布时间】:2019-04-12 08:22:13
【问题描述】:
我目前有一个数据表(1.10.18 版),其中加载了几个使用 js 的选项,但我需要使我的代码更具可重用性,并且我正在尝试使用 html5 data-* 属性初始化我的数据表。
<table id="dataTable" cellspacing="0" width="100%" data-source="ajax.mysource.php">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th><i class="fas fa-low-vision"></i></th>
</tr>
</thead>
</table>
我的 jQuery 代码如下所示:
var dataTable = $('#dataTable').DataTable({
processing: true,
serverSide: true,
ajax: $('#dataTable').data('source'),
columns: [
{ 'data': 'name' },
{ 'data': 'address' },
{ 'data': 'visible' }
],
order: [[ 1, 'asc' ], [ 0, 'asc' ]],
responsive: true,
nowrap: true,
pageLength: 15,
lengthChange: false,
select: 'single',
columnDefs: [
{ targets: 0, width: "110px" },
{ targets: 1, width: "150px" },
{ targets: 1, render: $.fn.dataTable.render.ellipsis(80) },
{ targets: 2, render: $.fn.dataTable.render.visibilityIcon() }
],
rowCallback: function(row, data, index) {
if (data.visible == "0") {
$(row).addClass("notVisible");
}
}
});
对于每个数据表,我都会使用一些共同的选项,但如果我可以使用 html5 data-* 属性直接在我的 html 中设置列、columnDefs 和 rowCallBack,那就太好了,这样我就可以使用相同的代码不同的表,例如:
<th data-columns="address" data-column-defs="{targets: 1, width:'150px'}" data-row-callback="function...">Address</th>
除了排序、排序和页面长度之外,我还没有找到如何使用 html5-* 属性。
用 html5 设置这个选项真的可以用 datatables.js 吗?
【问题讨论】:
标签: javascript jquery datatables datatables-1.10