DataTables Server Side Individual Column Filtering

心不动则不痛 提交于 2019-12-05 16:13:25

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!