datatables tries to read by int index instead object name from data

datatables tries to read by int index instead object name from data

eeseneesen Posts: 5Questions: 1Answers: 0

Hi,

i have implemented a clean webmethod in asp and datatables with the following code:

$('#list').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "FieldAgents.aspx/GetList",
            "type": "POST",
            "contentType": "application/json; charset=utf-8",
            "dataType": "text",
            "dataSrc": "data",
            "data": function (d) {
                return JSON.stringify({ "parameters": JSON.stringify(d) });
            },
            "columns": [
                { "data": "salutation" },
                { "data": "firstname" },
                { "data": "lastname" },
                { "data": "channel" },
                { "data": "company" },
                { "data": "region" }
            ],
            "dataFilter": function (res) {
                var parsed = JSON.parse(res);
                return parsed.d;
            }
        }
    });

the server returns this:

{"d":{"__type":"MyTestNameSpace.Classes.DataTableResponse","draw":1,"recordsTotal":1,"recordsFiltered":1,"data":[{"salutation":"Herr","firstname":"Max","lastname":"Payne","channel":"Channel X","company":"Company 1","region":"East"}]}}

everything seems to be fine, but i get this error:

DataTables warning: table id=list - Requested unknown parameter '0' for row 0, column 0.

Debugging dataTables.js i see it is calling _fnGetCellData which tries to fetch from row (which is an object) using int index and that obviously fails. It looks like dataTables isn't even checking for my columns setting it is expecting the row data to be an array. What is missing here?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    Looks like your dataFilter function is not within the ajax option. What you might want to do is remove dataFilter and use that function as part of the ajax.dataSrc. You can supply a function to ajax.dataSrc. The docs have an example.

    Kevin

  • eeseneesen Posts: 5Questions: 1Answers: 0

    Nope, just tried dataSrc. Instead of the dataFilter option in ajax i used the following resulting in same error as above:

    "dataSrc": function (json) {
        var j = JSON.parse(json);
        return j.d.data;
    }
    
  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    I think the problem is the json variable is a Javascript object not a JSON string when passed to the dataSrc option. Try this:

    "dataSrc": function (json) {
        return json.d.data;
    }
    
  • eeseneesen Posts: 5Questions: 1Answers: 0

    no it is a string, your solution did not work. it gives me json.d undefined because json is a string containing my result wrapped in "d" field

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    ajax.dataSrc doesn't work very well with server-side processing at the moment - the draw etc properties need to be at the top level regardless of ajax.dataSrc.

    Instead, remove the ajax.dataSrc option and use:

                "dataFilter": function (res) {
                    var parsed = JSON.parse(res);
                    return JSON.stringify( parsed.d );
                }
    

    inside the ajax object.

    That should then do it since the object inside d is what server-side processing needs.

    If that doesn't work, please link to a test case showing the issue so we can debug it.

    Allan

  • eeseneesen Posts: 5Questions: 1Answers: 0
    edited July 2018

    i tried this already, but in your case you are returning a string, but looking at the DT code i see it is expecting the returning element in dataFilter to be valid JSON (not string containing JSON).

    Im not sure how to create a test case, this is running as part of a big application, the data returned comes from a webmethod which fetches the data from DB.

    And looking at the code i see it is always trying to fetch the cell content with data[0], data[1] etc. instead of data['salutation'], data['firstname'], etc. don't know why it is ignoring my columns definition.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    I used dataSrc:"d.data" from my web service.

    My sample is on GitHub.
    https://github.com/bindrid/DataTablesServerSide/blob/master/Server%20Side%20Page

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    My reading of the jQuery docs was that dataFilter would return the modified data in its original state, but rereading it, that isn't the case, it says it can return anything. I haven't traced the jQuery code though, but it suggests that return parsed.d should work.

    don't know why it is ignoring my columns definition.

    Your columns array is inside your ajax object. Move it outside of that and DataTables will pick up the columns array correctly.

    Allan

  • eeseneesen Posts: 5Questions: 1Answers: 0

    uuh, that one hurts. yeah, now it's working fine. thanks for pointing out. SOLVED.

This discussion has been closed.