1

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.

yaserso
  • 2,638
  • 5
  • 41
  • 73
  • 1
    give me clarity . if you are trying to fill why cant you just use GET . I guess your are not returning properly . would you please post the return code in your controller ? mvc using right . – super cool Jun 03 '14 at 18:00
  • Good idea with the GET method. Forgot all about it. Thanks. – yaserso Jun 03 '14 at 18:08

1 Answers1

3

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

super cool
  • 6,003
  • 2
  • 30
  • 63
  • 1
    cool that's the way to go . any issues anytime feel free to share your doubt in this forum :) – super cool Jun 03 '14 at 18:37
  • Alright, so, sorry to bother you again, but, any idea on how to approach this without MVC? I'm not using MVC. I'm sorry I didn't say this earlier. I just didn't think it would matter. – yaserso Jun 03 '14 at 20:30
  • 1
    Not a problem . i guess you are using asp.net if so just you need to return a json as return type in method and ajax call will be similar no probs with that . for more info look into this : http://stackoverflow.com/questions/18244696/how-to-return-json-with-asp-net-jquery . cheers – super cool Jun 04 '14 at 04:51