数据表:遗漏的类型错误:无法读取属性“长度”的不确定?(DataTables: Uncaught T

2019-10-20 05:50发布

所以,这里是我的AJAX调用的数据表的表对象。 我试图让JSON填写只有行的表,因为它已经有标题行。

我并不清楚什么数据表Ajax调用,因为它们之间的区别从一个版本到其他的语法。

$('#MainContentPlaceHolder_business_return_flights').dataTable({
    "ajax": {
        "url": "Browse.aspx/GetBusinessFlights",
        "type": "POST",
        "contentType": "application/json; charset=utf-8",
        "dataType": "json"
    }
});

我不断收到错误: Uncaught TypeError: Cannot read property 'length' of undefined

这里是返回的JSON:

{
  "d":{
  "draw":"1",
  "recordsTotal":"70",
  "recordsFiltered":"70",
  "aData":[
             [
                "BI 098",
                "London (LHR)",
                "Dubai",
                "08-08-2014",
                "12:55 PM",
                "11:55 PM",
                "Royal Brunei",
                "1",
                "0",
                "1300",
                "\u003cbutton type=\"button\" href=\"javascript:void(0)\" onclick=\"selectFlight($(this))\" data-toggle=\"oflight\" class=\"btn btn-block btn-info\"\u003eBook\u003c/button\u003e"
             ],
             [
                "CY 383",
                "Dubai",
                "Larnaca",
                "08-06-2014",
                "1:45 PM",
                "4:05 PM",
                "Cyprus Airways",
                "1",
                "0",
                "1100",
                "\u003cbutton type=\"button\" href=\"javascript:void(0)\" onclick=\"selectFlight($(this))\" data-toggle=\"oflight\" class=\"btn btn-block btn-info\"\u003eBook\u003c/button\u003e"
             ]
        ]
    }
}

更新:这是我回报的方法:

[WebMethod]
public static Dictionary<string, object> GetBusinessFlights()
{
    listRows = new List<List<string>>();
    business = new Table();
    economy = new Table();

    FillTable(economy, business, scheduledFlights.List);

    foreach (TableRow row in business.Rows)
    {
        listRow = new List<string>();

        foreach (TableCell cell in row.Cells)
        {
            listRow.Add(cell.Text);
        }

        listRows.Add(listRow);
    }

    field = new Dictionary<string, object>() { { "draw", "1" }, { "recordsTotal", economy.Rows.Count.ToString() }, { "recordsFiltered", economy.Rows.Count.ToString() }, { "aData", listRows } };

    return field;

}

注: FillTable()只填充数据到商业和经济表对象。

Answer 1:

嗯,我想给你的DataTable的旅程,这可能大干一场。

数据表充填数据:

$('#myDataTable').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": 'DataProvider', // This will be controller action method with Json return type which in turn fills your DataTable 
            "bJQueryUI": true,
            "aoColumns": [
                         { "sName": "ID"  },
                         { "sName": "COMPANY_NAME" },
                         { "sName": "ADDRESS" },
                         { "sName": "TOWN" }
            ]

        });

随着Ajax调用意味着你必须设置是这样的:

$.ajax({
                        "type": "GET",
                        "dataType": 'json',
                        "contentType": "application/json; charset=utf-8",
                        "url": //source url,
                        "data": {},
                        "success": function (data) {
                            //on success you will reach into it
                        }
                    });

如果套这样意味着你的控制器返回类型它的工作原理酷:

DataProvider的操作方法内

return Json(new
            {
                sEcho = param.sEcho, //communication b/w subsequent calls
                iTotalRecords = //your count here,
                iTotalDisplayRecords = //per page display records count,
                aaData = your array list which will bind to dataTable
            },
                        JsonRequestBehavior.AllowGet);

PS:我是新来的数据表,我开始与这些真棒文章向前发展。 这会给你更好的想法和包含的样本项目:

http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part

问候



文章来源: DataTables: Uncaught TypeError: Cannot read property 'length' of undefined?