了解数据库中的fnServerData

了解数据库中的fnServerData

本文介绍了了解数据库中的fnServerData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中使用Datatables。我想了解使用fnServerData回调选项。我浏览了文档,并看到以下示例代码 -

  $(document).ready(function(){
$('#example')。dataTable({
bProcessing:true,
bServerSide:true,
sAjaxSource:xhr.php,
fnServerData:function(sSource,aoData,fnCallback,oSettings){
oSettings.jqXHR = $ .ajax({
dataType:'json',
type:POST,
url:sSource,
data:aoData,
success:fnCallback
});
}
});
});

这里是什么是sSource,aoData参数,以及我们如何提供价值?
此外,我们可以提交一个可以动态获取JSON数据的表单,而不是将JSP或PHP作为源代码(sAjaxSource)?

解决方案

fnServerData 是dataTable中的一个内部函数,可以使用您自己的ajax处理程序进行写入。在这种情况下,使用舒适的jQuery函数



参数是在dataTables核心中定义的,并且按照这个特定顺序进行。



sSource 是数据源所在的URL。它在initizalition设置为 sAjaxSource 中的值。在这种情况下 xhr.php



aoData 是一个将发送到数据源的参数数组。这包含您的dataSource脚本应该作出的paginationinfo,sortinginfo,filterinfo等(它们由内核自动设置)。 (例如:将sql查询限制为pagesize等)要向您的请求发送更多信息,您可以将其他值推送到 aoData 。像这样:

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



如果您想知道发送的内容,可以使用Firebugs控制台查看POST数据,也可以将类型更改为 GET ,然后再看到地址栏中传递的参数(请注意,这可能是一个很长的字符串,可能会被中断)。



fnCallback 也是一个内置的内核函数,可以被覆盖,但不是这种情况,您应该提供自己的功能,以防在数据收到后想在JS中执行一些后处理。



关于您的问题的第二部分:当然您不需要使用PHP或JSP。任何可以动态提供JSON数据的服务器端语言都很好(Phyton,Node ,你的名字...)


I am trying to use Datatables in my project. I want to understand the use of "fnServerData" callback option. I have gone through the doc Here and have seen following example code -

$(document).ready( function() {
  $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "xhr.php",
    "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
      oSettings.jqXHR = $.ajax( {
        "dataType": 'json',
        "type": "POST",
        "url": sSource,
        "data": aoData,
        "success": fnCallback
      } );
    }
  } );
} );

What is "sSource", "aoData" parameters here and how we are supplying values in it?Also, instead of putting a JSP or PHP as a source (sAjaxSource), can we submit a form which will get JSON data dynamically?

解决方案

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( {

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 ...)

这篇关于了解数据库中的fnServerData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:02