服务器端数据表自定义过滤 [英] Datatables custom filtering with server side
问题描述
我正在使用 DataTables ,并且还在使用服务器端处理(Django).
I'm using DataTables and also using server side processing (Django).
我有一个单独的文本字段,在该文本字段中,我已经使用它来自定义表格后在DataTable中自定义过滤器数据.
I have a seperate textfield in which I use it to custom filter data in the DataTable after the table has been rendered already.
以下工作正常(我想自定义过滤器列):
The following works just fine (I want to custom filter columns):
var table = $('#problem_history').DataTable( {
"bJQueryUI": true,
"aaSorting": [[ 1, "desc" ]],
"aoColumns": [
// various columns here
],
"processing": true,
"serverSide": true,
"ajax": {
"url": "/getdata",
"data": {
"friend_name": 'Robert'
}
}
} );
因此,在页面加载(DataTable的初始加载)时,它可以很好地过滤"Robert".但是现在我想以编程方式更改数据以过滤"friend_name" == "Sara"
So on the page load (initial load of the DataTable) it filters for 'Robert' just fine. But now I want to programmatically change the data to filter for "friend_name" == "Sara"
我已经尝试以下操作,filteredData
具有正确的过滤对象,但是表本身不会使用新的过滤器重绘.
I already tried the following, the filteredData
has a correct filtered object but the table itself does not redraw with the new filter.
var filteredData = table.column( 4 ).data().filter(
function ( value, index ) {
return value == 'Sara' ? true : false;
}
);
table.draw();
我也尝试过,但是没有运气:
I also tried this but no luck:
filteredData.draw();
我该如何实现?
How can I achieve this?
谢谢您的帮助.
推荐答案
以下是有关如何执行此操作的很好的解释: https://datatables.net/reference/option/ajax.data
Here is a very nice explanation on how to do it: https://datatables.net/reference/option/ajax.data
我当前正在使用此代码:
I am currently using this code:
"ajax": {"url":"/someURL/Backend",
"data": function ( d ) {
return $.extend( {}, d, {
"parameterName": $('#fieldIDName').val(),
"parameterName2": $('#fieldIDName2').val()
} );
}
}
您可以通过以下操作来调用它:
You call it by doing the following:
$('#myselectid').change(function (e) {
table.draw();
});
如果要通过单击按钮提交,请将.change
更改为.click
并确保ID指向HTML中按钮的ID.
If you want to submit by clicking on the button, change the .change
to .click
and make sure that ID is pointing to button's id in a HTML
这篇关于服务器端数据表自定义过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!