DataTables 服务器端单个列过滤
我希望有人能帮助我解决这个问题。我一直为此发疯。
我遇到了一种情况,我加载了 DataTables 网格(顺便说一句,这真是太棒了!),一切都很棒。然后我去搜索,遇到了问题。网格中填充的数据来自两个不同的数据库表(这很好),但是当有人执行搜索时,我无法知道去哪里获取数据。我需要知道搜索的标准是什么(即标题或联系人)。我看到当通过默认搜索框从服务器调用搜索时,有像“sSearch_0”这样的变量都未设置,这些变量是如何设置的?
这是表的初始化代码:
oTable = $('#example').dataTable({
"bJQueryUI": true,
"bFilter": true,
"sPaginationType": "full_numbers",
"bPaginate " : true,
"bServerSide" : true,
"sAjaxSource" : "php/tabledata.php",
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0,6,8 ] },
{ "sClass": "tdCenter", "aTargets": [ 0,1,2,3,4,5,6,7,8 ] }
],
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "userid", "value": userid } );
$.getJSON( sSource, aoData, function (json) {
fnCallback(json)
});
}
});
我已经研究了将数据添加到“fnServerData”的选项,并实际上将其用于第一个初始化服务器调用,但不确定如何将其用于后续服务器调用。我尝试使用“fnFilter”,但我没有看到执行服务器调用以获取更多数据。此时,除了默认搜索框之外,我没有看到任何其他执行服务器调用的方法,并且我看到了一些了解搜索针对哪一列的方法。
有人可以在这里帮助我并指出正确的方向吗?
I was hoping someone can help me with this. I have been running myself crazy with this.
I have a situation where I load the DataTables grid (awesome piece by the way!) and all is great. Then I go to search and I run into problems. The data being populated in the grid is coming from two different database tables (which is fine) but when someone executes a search I have no way of knowing where to go and get the data. I would need to know what criteria the search is about (i.e. title or contact). I see what when the search is called from the server via the default search box there are variables like "sSearch_0" which are all unset, how do those get set?
Here is the initialization code for the table:
oTable = $('#example').dataTable({
"bJQueryUI": true,
"bFilter": true,
"sPaginationType": "full_numbers",
"bPaginate " : true,
"bServerSide" : true,
"sAjaxSource" : "php/tabledata.php",
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0,6,8 ] },
{ "sClass": "tdCenter", "aTargets": [ 0,1,2,3,4,5,6,7,8 ] }
],
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "userid", "value": userid } );
$.getJSON( sSource, aoData, function (json) {
fnCallback(json)
});
}
});
I have looked into options for adding data to the "fnServerData " and actually use that for the first initialization server call but am unsure how to use that for a subsequent server call. I have tried to use "fnFilter" but I do not see that executing a server call for more data. At this point I do not see any other way to execute a server call besides the default search box and I see some way of knowing which column the search is for.
Can someone help me here and point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要从 DataTables 插件的服务器获取数据,则必须将 bServerSide 设置为 true,将 sAjaxSource 设置为适当的 URL,如果需要执行任何回调,则最好配置 fnServerData。
如果使用服务器端处理,则所有排序、过滤和分页都需要由您在服务器上处理。如果正确配置 DataTables,每当发生分页、过滤或排序事件时,它都会向服务器请求数据。
DataTables 服务器端 API 文档
服务器端处理的PHP示例
If you are getting data from the server for the DataTables plugin, you have to set bServerSide to true, set the sAjaxSource to the appropriate URL, and ideally configure fnServerData if you need to do any callbacks.
If you use server-side processing, all sorting, filtering, and paging needs to be handled by you on the server. If you configure DataTables correctly, it will request data from the server any time there is a paging, filtering, or sorting event.
DataTables server-side API documentation
PHP example of server-side processing
为了所有提出这个问题的人的利益,这是我所实施的。
客户端 (JavaScript)
按 Enter 键后执行 fnFilter。
服务器端 (Ruby)
查找 sSearch_(int) 参数哈希,并从键中检索列索引。
从数组中获取列名称并构建搜索字符串。
For the benefit of all who would refer this question, here is what I have implemented.
Client Side (JavaScript)
Execute fnFilter upon pressing Enter key.
Server Side (Ruby)
Find the sSearch_(int) param hash, and retrieve the column index from the key.
Get the column names from an array and build the search string.