我正在将Datatables用于我的Web应用程序,并使用了响应扩展,它将在屏幕变小时自动折叠我的表列,并且还有一个称为列优先级的功能,该功能将使某些列不会折叠/将是最后一个如果屏幕变小则崩溃。
我可以通过使用从javascript方面使它工作
$('#myTable').DataTable( {
responsive: true,
columnDefs: [
{ responsivePriority: 1, targets: 0 },
{ responsivePriority: 2, targets: -1 }
]
} );
但是这种方法有一个缺点,因为我在每个页面中都有不同种类的数据表,这些数据表具有我希望其保留且不折叠的不同列,因此我不能真正使用该代码,幸好有一种更好的方法加
data-priority="value"
放入<th>
标签中,但是在我尝试不起作用之后,它仍然折叠了我已经赋予数据优先级的列,有人知道为什么吗?
<table id="example" class="display nowrap" width="100%">
<thead>
<tr>
<th data-priority="1">First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
<th>E-mail</th>
<th data-priority="2">Extn.</th>
</tr>
</thead>
<tbody>
...
</tbody>
</table>
您可以在Jquery中使用此技巧
var columnDefs = [];
$datatable.find('th').each(function (i, el) {
var $el = $(el);
if ($el.is('[data-priority]')) {
var defs = { responsivePriority: $el.data('priority'), targets: i};
columnDefs.push(defs);
}
})
然后在数据表初始化中使用columnDefs
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句