Handling response JSON which does not contain required field all the time

Handling response JSON which does not contain required field all the time

kvn9328kvn9328 Posts: 18Questions: 7Answers: 0
edited December 2014 in Free community support

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:

  1. Handle it on server side code

  2. 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.

@allan,

Do you think we can handle this kind of scenario in client side?

Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin
  • kvn9328kvn9328 Posts: 18Questions: 7Answers: 0

    @allan,

    I used

    "columns" :[{"data":"aliases.alias", "defaultContent":"NONE"}]
    

    But for all the cells it is defaulting. Am i missing something?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I would have thought you need a "columns.render" function here.

    if ( data is empty ) return "something"

    else return "something else"

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin

    columns.render could work - but columns.defaultContent should also work.

    @kvn9328: Could you link to the page in question so I can take a look and debug it?

    Allan

  • kvn9328kvn9328 Posts: 18Questions: 7Answers: 0
    edited December 2014

    @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!

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin
    Answer ✓

    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

This discussion has been closed.