javascript DataTables:在服务器端搜索所有列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8120508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTables: search all columns when server-side
提问by Richard
I'm using IgnitedDatatables(CodeIgniter library) for DataTables. The table gets generated without problems but if I search/filter it can only filter one column at a time. If I set "bServerSide" to false it works but then I lose the server-side functionality.
我将IgnitedDatatables(CodeIgniter 库)用于DataTables。该表生成时没有问题,但如果我搜索/过滤它一次只能过滤一列。如果我将“bServerSide”设置为 false 它可以工作,但随后我会失去服务器端功能。
In the examples, this one is working: http://datatables.net/release-datatables/examples/ajax/custom_data_property.html
在示例中,这个正在工作:http: //datatables.net/release-datatables/examples/ajax/custom_data_property.html
while this isn't (server-side): http://datatables.net/release-datatables/examples/data_sources/server_side.html
虽然这不是(服务器端):http: //datatables.net/release-datatables/examples/data_sources/server_side.html
Is this not possible to achieve when running server-side?
运行服务器端时这是不可能实现的吗?
This is my JSON response (shortened and with replaced data):
这是我的 JSON 响应(缩短并替换了数据):
{"sEcho":0,"iTotalRecords":45438,"iTotalDisplayRecords":45438,"aaData":[["abc","12345","[email protected]","","","2010-01-27 22:31:10","Edit<\/a> Delete<\/a>"],["abc2"," test123","[email protected]","","","2008-06-15 22:09:33","Edit<\/a> Delete<\/a>"]],"sColumns":"fname,lname,email,phone,cellphone,created,edit"}
JavaScript code:
JavaScript 代码:
$("#members").dataTable( {
"bProcessing": true,
"bServerSide": true,
'sAjaxSource': '<?php echo base_url();?>members/listener',
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": 'POST',
"url": sSource,
"data": aoData,
"success": fnCallback
} );
},
"bLengthChange": false,
"aaSorting": [[ 0, "asc" ]],
"iDisplayLength": 15,
"sPaginationType": "full_numbers",
"bAutoWidth": false,
"aoColumnDefs": [
{ "sName": "fname", "aTargets": [ 0 ] },
{ "sName": "lname", "aTargets": [ 1 ] },
{ "sName": "email", "aTargets": [ 2 ] },
{ "sName": "phone", "sWidth": "80px", "aTargets": [ 3 ] },
{ "sName": "cellphone", "sWidth": "100px", "aTargets": [ 4 ] },
{ "sName": "created", "sWidth": "120px", "aTargets": [ 5 ] },
{ "bSortable": false, "sName": "edit", "sWidth": "115px", "aTargets": [ 6 ] }
]
});
Thank you!
谢谢!
采纳答案by Nicola Peluchetti
Well, the problem if you filter server side is that you are filtering through an SQL query and this is what is written on the exampleyou posted;
好吧,如果您过滤服务器端,问题在于您正在通过 SQL 查询进行过滤,这就是您发布的示例中所写的内容;
- Filtering
- NOTE this does not match the built-in DataTables filtering which does it
- word by word on any field. It's possible to do here, but concerned about efficiency
- on very large tables, and MySQL's regex functionality is very limited
- 过滤
- 注意这与内置的 DataTables 过滤不匹配
- 在任何领域逐字逐句。可以在这里做,但担心效率
- 在非常大的表上,并且 MySQL 的正则表达式功能非常有限
Basically you could do what you want to do (a regular expression match an all columns) but it's going to kill the performance server side.
基本上你可以做你想做的事情(一个正则表达式匹配所有列),但它会杀死性能服务器端。
What i usually do is provide a filter for each column, i need to filter.
我通常做的是为每一列提供一个过滤器,我需要过滤。