ajax.dataSrc not working as expected (issue #468 on github)

ajax.dataSrc not working as expected (issue #468 on github)

ferreiradanielferreiradaniel Posts: 2Questions: 1Answers: 0

The answer provided on Github worked partially. The data was loaded into the table, but the footer (paging and items count) didn't work. keeps showing "filtered from NaN total entries".

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Have you moved the draw and other properties to the top level of the JSON object, as I noted was required in the Github issue?

    Allan

  • ferreiradanielferreiradaniel Posts: 2Questions: 1Answers: 0

    Well, I can't do that. Thats the situation:

    The server-side service always return an object of type Response to all requests.

    public class Response
    {
            public Boolean Error { get; set; }
            public IList<string> Messages { get; set; }
            public object Data { get; set; }
    }
    

    So there's this Error property that indicates if there was any server-side error, the array of error messages and de generic Data object where the data itself is set, in case of success.

    This model works fine in all ohter parts of the system, i.e., I send a User object in the Data and the client cast this generic object to the User type.

    In the case of DataTables, I have the type DataTablesResponse which looks like this:

    public class DataTablesResponse
    {
        public int draw { get; private set; }
        public IEnumerable data { get; private set; }
        public int recordsTotal { get; private set; }
        public int recordsFiltered { get; private set; }
        public DataTablesResponse(int draw, IEnumerable data, int recordsFiltered, int recordsTotal)
        {
            this.draw = draw;
            this.data = data;
            this.recordsFiltered = recordsTotal;
            this.recordsTotal = recordsTotal;
        }
    }
    

    And I send this in my generic Data object. The draw is still the first parameter, but inside the Data.

    That's why I oppened an Issue on Git instead of asking here.. I reasearch a lot and found this as a limitation. I reckon that many applications use this generic Response.Data model so would be cool if we could set the root of the entire DataTablesResponse on ajax.dataSrc, not only the array of data that is shown in the table.

    Thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    The problem with using ajax.dataSrc is that it points to the root array of the data for the table - not the root object. This is the documented implementation and changing that would break everything that already depends upon ajax.dataSrc.

    However, there is another option also involving ajax.dataSrc - use it as a function and map the parameters as needed, manipulating the JSON.

    dataSrc: function ( json ) {
      json.draw = json.Data.draw;
      json.recordsTotal = json.Data.recordsTotal;
      json.recordsFiltered = json.Data.recordsFiltered;
    
      return json.Data.data;
    }
    

    I've yet to work out a good way of offering the dataSrc like ability for the server-side processing parameters...

    Allan

  • veglosveglos Posts: 1Questions: 0Answers: 0
    edited December 2014

    Hi Allan,

    I tried doing that, but the function _fnAjaxUpdateDraw ( settings, json ) is getting the old json. In other words I can't get *json[modern]* because *json['draw']* is undefined. I can only access it through *json.d['draw']* (notice the 'd')

    My situation is very similar:

    dataSrc: function (json) {
    json.draw = json.d.draw;
                            json.recordsTotal = json.d.recordsTotal;
                            json.recordsFiltered = json.d.recordsFiltered;
                            json.data = json.d.data;
    
                            return json.d.data;
                        }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Good point! The draw, etc, parameters are being evaluated before the dataSrc](https://github.com/DataTables/DataTablesSrc/blob/master/js/core/core.ajax.js#L287) at the moment. I don't see any good reason why that shouldn't be swapped around. I'll commit that in, which will allow my above suggestion to work (apologies, I thought it was already like this!).

    Allan

  • moondogmoondog Posts: 1Questions: 0Answers: 0

    Hello Allan,
    I have an ajax response form the server which reads:

    {'total': 13,
    'firstRecord':10,
    'from':1,
    'itemsPerPage':10,
    'wData':[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]
    }

    it was easy to display the data using:

    dataSrc: function (json) {
    json.start = json.from;
    json.recordsTotal = json.total;
    json.recordsFiltered = json.itemsPerPage;
    return json.wData;
    }

    but no matter how I tried, I could not map the total, first record and the other pagination parameters because the pagination properties are evaluated from the loaded JSON before the dataSrc function kicks in.

    I think my code is correct, judging from the above examples, the data shows correctly, but no pagination data is passed.

    Is there another way to change the loaded JSON before any processing gets done (which is needed to have a parser) ?

    Using 1.10.5 from cdn

    Thank you

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Using 1.10.5 from cdn

    1.10.5 doesn't have the fix that I committed above. It is an, as yet, unreleased change and you would need to use the nightly version to be able to use it.

    Allan

This discussion has been closed.