How to properly format and filter this server-side data?

How to properly format and filter this server-side data?

GoldenJoeGoldenJoe Posts: 6Questions: 2Answers: 0

I have an existing API that returns data in a format like:

"status":"success",
"data":{
    "groupA":[
    {
        "name_first":"Joe",
        "age":32
    },
    {
        "name_first":"Geoff",
        "age":31
    }],
    "groupB":[
    {
        "id":001,
        "class":3
    },
    {
        "id":002,
        "class":1
    }]
}

or in the event of an error:

"status":"failure",
"errorcode":1404,
"errordesc":"invalid user id"

For sake of simplicity, suppose I want a simple table that displays the "name_first" and "age" properties of objects in groupA only. How would I do that, and handle the possible absence of the "data" array in the event of an error?

This question has an accepted answers - jump to answer

Answers

  • GoldenJoeGoldenJoe Posts: 6Questions: 2Answers: 0

    Currently, I am using an ajax dataFilter, as described in mboo2005's comment here:
    https://www.datatables.net/manual/server-side

    However, this seems to break search and ordering.

  • allanallan Posts: 61,765Questions: 1Answers: 10,111 Site admin

    To show data from one of the array's only use ajax.dataSrc (in this case data.groupA with columns.data set to be age and name_first for each column).

    Its only when you want to combine data from the two arrays that things would become a little bit more tricky. At that point you would need to use the dataFilter property (or ajax.dataSrc as a function that will combine the arrays).

    Allan

  • GoldenJoeGoldenJoe Posts: 6Questions: 2Answers: 0

    Thanks, allan. Why does using dataFilter break the search/sort?

    Also, how do you handle data source errors gracefully? As far as I can tell the table will just fail silently. I was thinking of just grabbing the data with jQuery.ajax and then setting the table's data in the success block, but I'm not sure how that would work with a large data set.

  • allanallan Posts: 61,765Questions: 1Answers: 10,111 Site admin

    Also, how do you handle data source errors gracefully?

    Return an error property which DataTables will show the value of, or listen for xhr and check for your error properties.

    Why does using dataFilter break the search/sort?

    I'd need a link to a page showing the issue to be able to debug it.

    Allan

  • GoldenJoeGoldenJoe Posts: 6Questions: 2Answers: 0

    Thanks again for your time allan. I did a lot of searching and figured a bit out:

    Why does using dataFilter break the search/sort?

    I had serverSide set to "true", which requires the server to handle sort/search functions. However, I can't find in the documentation exactly how to do that. Can you link me to the appropriate page or an example? For the small data set I am developing with, setting it to "false" restored sort and search functionality.

    Return an error property which DataTables will show the value of, or listen for xhr and check for your error properties.

    Return it from where? From the server? Given the error response format above, is my only option to use ajax.dataFilter to reconstruct the JSON, or is there another way to get the table to concatenate the errorcode and errordesc?

  • allanallan Posts: 61,765Questions: 1Answers: 10,111 Site admin
    Answer ✓

    I had serverSide set to "true", which requires the server to handle sort/search functions. However, I can't find in the documentation exactly how to do that.

    I don't have an example that uses dataFilter I'm afraid, but those show how server-side processing with the table is expected to work.

    Return it from where? From the server?

    Yes, as the documentation linked to above notes you can return an error property in the JSON (or construct suitable JSON using dataFilter in this case).

    Allan

This discussion has been closed.