问题
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?
回答1:
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
回答2:
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.
$(tableId + " thead input").keypress( function () {
if (event.which == 13) {
event.preventDefault();
oTable.fnFilter( this.value, $("thead input").index(this) );
}
} );
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.
def column_search
search_string = []
params.keys.map {|x| x if params[x].present? and x.include? "sSearch_"}.compact.each do |search|
index = search.split("_").last.to_i
search_string << "#{columns[index]} ilike '%#{params[search]}%'"
end
search_string.join(' and ')
end
来源:https://stackoverflow.com/questions/4492388/datatables-server-side-individual-column-filtering