Understanding fnServerData in Datatables

房东的猫 提交于 2019-11-28 23:59:12
mainguy

fnServerData is an internal function in dataTables that can be ovewritten with your own ajax handler. In this case with a comfortable jQuery function Read more here

The parameters are defined in dataTables core and are required in this particular order.

sSource is the URL where your datasource is located. It is set at initizalition to the value in sAjaxSource. In this case xhr.php

aoData is an array of parameters that will be sent to the datasource. This contains by default paginationinfo, sortinginfo, filterinfo etc. (which are automaticaly set by the core) to which your dataSource script should react. (For Example: Limit an sql query to pagesize etc.) To send more info to your request you can push other values to aoData. Like so:

"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
               aoData.push( { "name": "Input1", "value": $("#data1").val() } );
               aoData.push( { "name": "Input2", "value": $("#data2").val() } );
  oSettings.jqXHR = $.ajax( {

(Will get the values of two input fields named data1 and data2 via jQuery from a form and include them in the POST as Input1 and Input2)

If you want to know what's being sent, you can look at the POST Data with Firebugs console, or you can change type to GET. Then you'll see the passed parameters in the addressbar (Beware that this can be a very long string which might get cut off).

fnCallback is also a built-in function of the core that can be overwritten, but is not in this case. You should provide your own function in case you want to do some postprocessing in JS after the data was received.

Regarding the second part of your question: Of course you don't need to use PHP or JSP. Any server-side language that can serve JSON data dynamically is fine (Phyton, Node, you name it ...)

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