理解数据表中的 fnServerData [英] Understanding fnServerData in Datatables

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

问题描述

我正在尝试在我的项目中使用数据表.我想了解fnServerData"回调选项的使用.我已经阅读了文档 Here 并看到了以下示例代码 -

$(document).ready( function() {$('#example').dataTable({bProcessing":真,bServerSide":真,"sAjaxSource": "xhr.php",fnServerData":函数(sSource,aoData,fnCallback,oSettings){oSettings.jqXHR = $.ajax( {"数据类型": 'json',"type": "POST",网址":sSource,数据":aoData,成功":fnCallback});}});});

这里的sSource"、aoData"参数是什么?我们如何在其中提供值?另外,我们可以提交一个动态获取 JSON 数据的表单,而不是将 JSP 或 PHP 作为源(sAjaxSource)吗?

解决方案

fnServerDatadataTables 中的一个内部函数,可以用你自己的 ajax<覆盖/code> 处理程序.在这种情况下,使用舒适的 jQuery 函数 在这里阅读更多内容.

参数在 dataTables 核心中定义,并按以下特定顺序要求:

1 - sSource 是数据源所在的 URL.它在初始化时设置为 sAjaxSource 中的值.在这种情况下 xhr.php

2 - aoData 是将发送到数据源的参数数组.这默认包含 paginationinfosortinginfofilterinfo 等(由核心自动设置),您的 dataSource 脚本应该做出反应.(例如:将 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( {

<块引用>

(将通过 jQuery 从一个名为 data1 和 data2 的输入字段中获取值表单并将它们作为 Input1 和 Input2 包含在 POST 中)

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

3 - fnCallback 也是内核的内置函数,可以覆盖,但在这种情况下不是.如果你想在接收到数据后在 JS 中做一些后处理,你应该提供你自己的函数.

关于您问题的第二部分:当然您不需要使用PHPJSP.任何可以动态提供 JSON 数据的服务器端语言都很好(Python、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 overwritten 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:

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

2 - aoData is an array of parameters that will be sent to the datasource. This contains by default paginationinfo, sortinginfo, filterinfo etc. (which are automatically 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 address bar (Beware that this can be a very long string which might get cut off).

3 - 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 post-processing 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 (Python, Node, you name it ...)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆