我目前有一个datatable(1.10.18版),里面有几个js选项,但我需要让我的代码更具可重用性,我正在尝试用html5 data-*属性初始化我的datatable。
<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");
}
}
});
每个datatable都有一些常用的选项,但如果我可以使用html5 data-*属性直接在html中设置列、列定义和行回调,那就太好了,这样我就可以对不同的表使用相同的代码,比如:
<th data-columns="address" data-column-defs="{targets: 1, width:'150px'}" data-row-callback="function...">Address</th>
除了排序、排序和页面长度之外,我还没有发现如何使用html5-*属性。
用html5设置这个选项实际上可以用datatables实现吗。js?