سؤال

So, here is my AJAX call for the DataTables table object. I am trying to get the JSON to fill in a table with rows only, as it already has header rows.

I am not clear as to what the syntax of the DataTables ajax calls are, as they differ from one version to the other.

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

I keep getting the error: Uncaught TypeError: Cannot read property 'length' of undefined

Here is the JSON being returned:

{
  "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"
             ]
        ]
    }
}

Update: Here is my return method:

[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;

}

Note: The FillTable() only fills in the data into the business and economy Table objects.

هل كانت مفيدة؟

المحلول

Well i guess this could make inroads to you datatables journey .

DataTable filling with data :

$('#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" }
            ]

        });

With an Ajax call means you have to set something like this :

$.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
                        }
                    });

Your controller return type if sets like this means it works cool :

inside DataProvider action method

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 : When i am new to datatables i began with these awesome articles to move forward . This will give you better idea & sample projects included :

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

Regards

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top