Nested Json

Nested Json

al_1976al_1976 Posts: 9Questions: 3Answers: 0
edited January 2015 in Free community support

Hi

Excuse the newbie question but its driving me up the wall and any help would be much appreciated. I have a nested JSON object called activity. If this has values I want to display them (ideally wrapped in html). If they are no values I just want to display some default text.

Hopefully this simple example will make it clear http://live.datatables.net/radabaye/1/

Many thanks

This question has an accepted answers - jump to answer

Answers

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

    activity is an array so you would need to use array syntax ("activity[].timestamp" for example) - see column.data for more information about this. Or use column.render if you want to format them with some HTML.

    Allan

  • al_1976al_1976 Posts: 9Questions: 3Answers: 0
    edited January 2015

    That's great thanks for your help. I'm still have a little trouble getting my head around it . I have updated the link to my latest work. What I really what to do is take the data in activity array and create another table from it within the current cell, is that possible?

    Thanks again for you time I really appreciate it

    http://live.datatables.net/radabaye/1/

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

    What I really what to do is take the data in activity array and create another table from it within the current cell, is that possible?

    You are going to need to explain that a bit further for me please. Do you simply want the data in the array shown in a <table> inside the DataTable cell?

    You could use columns.render to produce the HTML needed for that table if so.

    Allan

  • al_1976al_1976 Posts: 9Questions: 3Answers: 0
    edited January 2015

    Hi Allan

    That's exactly what I want to do. If the activity array has data I want to create a table using that data inside the DataTable cell. I have looked at columns.render but I can't seem to get it to work. No errors it just doesn't display anything.

    Could I be really cheeky and ask for some example to code to push me in the right direction please? I must be doing something stupid

    Many Thanks

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

    Something like:

    {
      data: 'activity',
      render: function ( data, type, row ) {
        // `data` is the `activity` array!
        var s = '';
    
        for ( var i=0, ien=data.length ; i<ien ; i++ ) {
          s += data[i].timestamp+' ';
        }
    
        return s;
      }
    }
    

    It isn't a table I know - but hopefully you can fill in the blanks using the above :-)

    Allan

  • al_1976al_1976 Posts: 9Questions: 3Answers: 0

    Thats briliant thanks

This discussion has been closed.