proper way to deal with null data returned in server-side search processing

proper way to deal with null data returned in server-side search processing

rldean1rldean1 Posts: 141Questions: 66Answers: 1

Sometimes in MS SQL, I can't (or don't want to) send back a properly-formatted string, such as {"data":[]}, when there is no data to return. Sometimes, if I do, I might send this {"data":null} or this []. DT will recognize that, and say, "No matching records found".

So, if I don't send anything back, is there some way inside of DT for me to tell it, "Hey, there's no records" if the structure is missing?

OR, does DT always expect some sort of structure, even when there are no records? (Some of the SQL queries are really hard to do)

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,447Questions: 26Answers: 4,974

    Not sure if this is answering your question but if your JSON response doesn't always have all the columns you can use defaultContent to set a default for the missing data objects.

    Kevin

  • rldean1rldean1 Posts: 141Questions: 66Answers: 1

    I was thinking something like that, but for when the entire data array was not returned. I recently send back {"draw":1,"recordsTotal":137921,"recordsFiltered":null}, and my webapp crashed. ("data":[] was missing)

    Long story short, in retrospect, it's probably just better for me to always send back the expected structure :smile:

  • kthorngrenkthorngren Posts: 21,447Questions: 26Answers: 4,974
    Answer ✓

    Yes, you will want something like this:

    {
      "draw": 3,
      "recordsTotal": 137921,
      "recordsFiltered": 0,
      "data": []
    }
    

    Kevin

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Yup - DataTables always expects data to be an array - no exceptions :). I've actually committed a change to the 2 branch to handle null there, but v2 is a good long way for release....

    Allan

This discussion has been closed.