简体   繁体   English

jQuery DataTables Ajax数据源

[英]jQuery DataTables Ajax Data Source

I find the documentation for DataTables when using AJAX as the data source limited. 当使用AJAX作为数据源受限时,我找到了DataTables的文档。 I've got a method in my controller in an ASP.NET MVC 4 application that returns a simple JSON result, and I'm trying to fill a DataTable with the following: 我在ASP.NET MVC 4应用程序的控制器中有一个返回简单JSON结果的方法,并且我正在尝试用以下数据填充DataTable:

$.ajax({
    type: "GET",
    dataType: "json",
    contentType: "application/json",
    url: "/Chart/Ajax",
    success: function (data) {
        var returnData = [];
        for (var i = 0; i < data.length; i++) {
            //makes the data an array of arrays
            returnData.push($.makeArray(data[i]));
        }
        $('#secondary').dataTable({
            "aaData": returnData,
            "aoColumns": [
                { "sTitle": "DrugClass" },
                { "sTitle": "DrugClassSize" },
                { "sTitle": "DistinctPatients" },
            ]
        });
    }
});

Ideally, I wouldn't create the table element until the success callback had fired, but in this instance an empty table element is on the page. 理想情况下,在触发成功回调之前,我不会创建table元素,但是在这种情况下,页面上会出现一个空的table元素。 With the following code I get the error: Uncaught TypeError: Object [object Object] has no method 'dataTable' the thing is, I already have a different DataTable already on the page and it works fine. 通过以下代码,我得到了错误: Uncaught TypeError: Object [object Object] has no method 'dataTable' ,事实是,页面上已经有一个不同的DataTable,并且工作正常。 This script is at the very bottom of the page, after the working data table. 该脚本在页面的最底部,在工作数据表之后。 Why am I getting this error and what's an easy way to get DataTables to play nice with AJAX data sources? 为什么我会收到此错误?有什么简单的方法可以使DataTables与AJAX数据源完美配合?

It seems as though you are putting the initialisation of the datatable in the success of the ajax call, and what you need to do is set it up the other way round ie initialise the datatable and set the correct options and the plugin will take care of the rest. 似乎您正在将数据表的初始化放在ajax调用成功的过程中,而您需要做的是反过来设置它,即初始化数据表并设置正确的选项,然后插件将负责其余的部分。

You already have your controller action that returns a json result, so you simply need to set the sAjaxSource to that url, so in your case: "sAjaxSource": "/Chart/Ajax" . 您已经具有返回json结果的控制器操作,因此只需将sAjaxSource设置为该url,因此在您的情况下: "sAjaxSource": "/Chart/Ajax"

You then do what you were going to do in the success of the ajax call and set that function to be the fnServerData option, as shown below: 然后,执行ajax调用成功时要执行的操作,并将该函数设置为fnServerData选项,如下所示:

$('#secondary').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "/Chart/Ajax",
    "fnServerData": function ( sSource, aoData, fnCallback ) {

        var returnData = [];
        for (var i = 0; i < aoData.length; i++) {
        //makes the data an array of arrays
        returnData.push($.makeArray(aoData[i]));
       }

        $.getJSON( sSource, returnData , function (json) { 
            /* Do whatever additional processing you want on the callback, then 
           tell DataTables */
            fnCallback(json)
        } );
    }
} );

The fnCallback will then send the json to the datatable in the view. 然后,fnCallback将json发送到视图中的数据表。

More information here and here . 这里这里有更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM