我有一个dataTable,它在表头有两行。一行是列名,另一行是输入字段
我不知道,但默认情况下,排序是在输入字段(第二行(上完成的,而不是标签字段(第一行(
我的数据表代码是
$('#result-table').DataTable({
"paging": true,
"lengthChange": false,
"searching": true,
"ordering": true,
"info": true,
"autoWidth": false,
"responsive": true,
});
我正在克隆标题行并将输入字段添加为
$("#result-table thead tr").clone(true).appendTo("#result-table thead");
// add a text input filter to each column of the new row
$("#result-table thead tr:eq(1) th").each(function (i) {
var title = $(this).text();
$(this).html("<input type='text' class='form-control' placeholder='Search '" + title + "' />");
$("input", this).on( "keyup change", function () {
if ($("#result-table").DataTable().column(i).search() !== this.value) {
$("#result-table").DataTable().column(i).search(this.value).draw();
}
});
});
怎么了?
以下是您需要确保正在执行的两件事:
-
确保
clone()
代码和创建输入字段的代码位于$(document).ready(function()
代码内,但必须将其放置在$('#result-table').DataTable({...})
代码之前。 -
确保DataTables初始化包括以下选项:
orderCellsTop: true,
您可能已经在执行(1(,但问题中的代码中缺少(2(。