带有服务器端的数据表自定义过滤 [英] Datatables custom filtering with server side
问题描述
我正在使用 DataTables 以及服务器端处理 (Django).
我有一个单独的文本字段,在表格已经呈现后,我用它来自定义过滤数据表中的数据.
以下工作正常(我想自定义过滤列):
var table = $('#problem_history').DataTable( {bJQueryUI":真,"asorting": [[ 1, "desc" ]],"aoColumns": [//这里的各个列],处理":真的,服务器端":真,阿贾克斯":{"url": "/getdata",数据": {"friend_name": '罗伯特'}}});
所以在页面加载(数据表的初始加载)时,它过滤罗伯特"就好了.但现在我想以编程方式更改数据以过滤 "friend_name" == "Sara"
我已经尝试了以下操作,filteredData
有一个正确的过滤对象,但表格本身没有使用新过滤器重绘.
varfilteredData = table.column( 4 ).data().filter(函数(值,索引){返回值 == 'Sara' ?真假;});table.draw();
我也试过这个,但没有运气:
filteredData.draw();
我怎样才能做到这一点?
感谢您的帮助.
这里有一个很好的解释:https://datatables.net/reference/option/ajax.data
我目前正在使用此代码:
"ajax": {"url":"/someURL/Backend",数据":函数(d){返回 $.extend( {}, d, {"parameterName": $('#fieldIDName').val(),"parameterName2": $('#fieldIDName2').val()});}}
您可以通过执行以下操作来调用它:
$('#myselectid').change(function (e) {table.draw();});
如果您想通过单击按钮提交,请将 .change
更改为 .click
并确保 ID 指向 HTML 中按钮的 ID>
I'm using DataTables and also using server side processing (Django).
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'
}
}
} );
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"
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?
Thank you for your help.
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();
});
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屋!