Handling response JSON which does not contain required field all the time
Handling response JSON which does not contain required field all the time
Hi,
I am using datatables with server side enabled.
I am dealing with a situation where the returned JSON will not contain the field that i need to display in datatable column.
eg:
Case 1:
"aliases":[{alias:value}]
Case 2:
"aliases":[]
In my datatable i use
"columns": [{"data":"id"},{"data":"aliases.alias"}]
which will obviously fail for case 2 since alias is not present. I can think of two ways to solve this problem:
Handle it on server side code
Handle it on client side, i.e to check to see is 'aliases' is null if it is put some empty value in column or else put 'aliases.alias' value in the column.
Do you think we can handle this kind of scenario in client side?
Thanks!
This question has an accepted answers - jump to answer
Answers
Use
columns.defaultContent
.Allan
@allan,
I used
But for all the cells it is defaulting. Am i missing something?
I would have thought you need a "columns.render" function here.
if ( data is empty ) return "something"
else return "something else"
columns.render
could work - butcolumns.defaultContent
should also work.@kvn9328: Could you link to the page in question so I can take a look and debug it?
Allan
@allan,
I am trying to recreate problem using jfiddle, but i am not able to generate required json.
Any help would be appreciated. I am sharing link here
http://jsfiddle.net/QsHw4/6094/
If i can make this sample json work with datatables, i can recreate my problem and share it here in jfiddle.
Thanks!
I've made a bunch of changes to allow it to work in jsfiddle: http://jsfiddle.net/QsHw4/6097/ . Note that I removed
serverSide
since your JSON wasn't returning valid server-side processing information.Allan