Jquery Datatable列筛选在服务器端不起作用
我已经在datatable中完成了服务器端处理。 现在,我还想在服务器端处理单个列过滤 我试过了Jquery Datatable列筛选在服务器端不起作用,jquery,laravel,datatables,Jquery,Laravel,Datatables,我已经在datatable中完成了服务器端处理。 现在,我还想在服务器端处理单个列过滤 我试过了 $('#example thead th').each(function () { var title = $(this).text(); $(this).html(title+' <input type="text" class="col-search-input" placeholder="Search ' + title + '" />'); }); var
$('#example thead th').each(function () {
var title = $(this).text();
$(this).html(title+' <input type="text" class="col-search-input" placeholder="Search ' + title + '" />');
});
var table = $('#example').DataTable({
dom: 'lfBrtip',
"processing":true,
"serverSide":true,
"ajax":{
"url":"{{route('homeajax')}}",
"dataType": "json",
"type": "POST",
},
"columns":[
{"data":"name"},
{"data":"Position"},
{"data":"office"},
{"data":"age"},
{"data":"start_date"},
{"data":"salary"},
],
initComplete: function(){
this.api().columns().every(function(){
$('input', this.header()).on('keyup change', function () {
if (table.search() !== this.value) {
table.search(this.value,true).draw();
}
});
});
},
paginate: true,
});
$('#示例thead th')。每个(函数(){
var title=$(this.text();
$(this.html(title+'');
});
变量表=$('#示例')。数据表({
dom:'lfBrtip',
“处理”:对,
“服务器端”:正确,
“ajax”:{
“url”:“{route('homeajax')}}”,
“数据类型”:“json”,
“类型”:“职位”,
},
“栏目”:[
{“数据”:“名称”},
{“数据”:“位置”},
{“数据”:“办公室”},
{“数据”:“年龄”},
{“数据”:“开始日期”},
{“数据”:“工资”},
],
initComplete:function(){
this.api().columns().every(函数()){
$('input',this.header()).on('keyup change',function(){
if(table.search()!==此.value){
table.search(this.value,true).draw();
}
});
});
},
paginate:是的,
});
它只适用于一列,但当我尝试从另一个字段搜索时,它不起作用
当我尝试从另一列输入时,它会显示所有数据。这取决于用于数据获取的服务器端查询。如果在查询中加入,则需要传递列名,如下面的代码所示
"columns":[
{"data":"name"},
{"data":"tbl.Position"}, //tbl is join table alias.
{"data":"office"},
{"data":"age"},
{"data":"start_date"},
{"data":"salary"},
],
我知道这会稍微打乱你的设计,但是你能让DataTables创建搜索输入吗?这样,它可以将事件侦听器设置为仅搜索该列。我使用此链接中的代码变体: 另一件需要注意的事情(也许先试试这个)是,当您只想搜索该列时,我认为您正在搜索整个表。在columns()中尝试类似的操作。每个()函数:
this.api().columns().every(function(){
let column = this;
$('input', this.header()).on('keyup change', function () {
if (column.search() !== this.value) {
column.search(this.value,true).draw();
}
});
});
也许这段ajax代码会对您有所帮助。 如果需要仅启用过滤器名称、位置和开始日期(自定义过滤器)
"aoColumns": [
{ "bSortable": true },
{ "bSortable": true },
{ "bSortable": false },
{ "bSortable": false },
{ "bSortable": true },
{ "bSortable": false }
]