【发布时间】:2021-12-04 13:19:39
【问题描述】:
我在由 Datatables 插件生成的表的标题中添加了一个搜索框。它在标题的每一列上方放置一个搜索框。我需要将搜索过滤器仅应用于几列。假设第 0 列和第 6 列需要搜索过滤器,而其他列则不需要。有没有办法做到这一点?我无法在任何地方找到如何实现这一点的答案,而且我是编码新手,所以我还没有找到实现这一点的方法。下面的代码我用来创建搜索标题。
$('#OBTable thead tr')
.clone(true)
.addClass('filters')
.appendTo('#OBTable thead');
var table = $('#OBTable').dataTable({
"ajax": {
"url": "SelectData.php",
"dataSrc": "data",
},
orderCellsTop: true,
select: true,
initComplete: function () {
var api = this.api();
// For each column
api
.columns()
.eq(0)
.each(function (colIdx) {
// Set the header cell to contain the input element
var cell = $('.filters th').eq(
$(api.column(colIdx).header()).index()
);
var title = $(cell).text();
$(cell).html('<input type="text" placeholder="' + title + '" />');
// On every keypress in this input
$(
'input',
$('.filters th').eq($(api.column(colIdx).header()).index())
)
.off('keyup change')
.on('keyup change', function (e) {
e.stopPropagation();
// Get the search value
$(this).attr('title', $(this).val());
var regexr = '({search})'; //$(this).parents('th').find('select').val();
var cursorPosition = this.selectionStart;
// Search the column for that value
api
.column(colIdx)
.search(
this.value != ''
? regexr.replace('{search}', '(((' + this.value + ')))')
: '',
this.value != '',
this.value == ''
)
.draw();
$(this)
.focus()[0]
.setSelectionRange(cursorPosition, cursorPosition);
});
});
},
【问题讨论】:
标签: jquery datatables