Can I use a function when working with nested object data?

Can I use a function when working with nested object data?

dsnapdsnap Posts: 23Questions: 3Answers: 0

Looking at this, http://datatables.net/examples/ajax/objects_subarrays.html.
Using 1.10.3.

Instead of separating a nested array with commas I want to make a link.
<a href="linking/to/here/#{multiple.account_hash}">{multiple.name}</a>

var ajax = {
  "aaData":
     [ {
      "id":"1",
      "multiple": [ 
        [
           "name":"New 1",
           "account_hash":"826ab3413acb4b03af40d75412d21aaf"
        ]
      ]
    }]
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    edited October 2014 Answer ✓

    Sure - use columns.data to point to multiple and columns.render to create the link based on the data:

    {
      data: 'multiple',
      render: function ( data, type, row ) {
        return type === 'display' ?
          '<a href="/whatever/'+data.account_hash+'">'+data.name+'</a>' :
          data.name;
      }
    }
    

    edit - you don't have to handle the display type specifically - DataTables would strip the HTML for sorting and filtering, but I think it is probably good practice.

    Allan

  • dsnapdsnap Posts: 23Questions: 3Answers: 0

    Threw you some money!

  • dsnapdsnap Posts: 23Questions: 3Answers: 0

    Hmm, what if I also wanted "id" in the link?
    I tried turning

    data: 'multiple'
    

    into

    data: { 'multiple':'multiple', 'id':'id'}
    

    but that seemed to put the entire response in each row...

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin

    The row parameter (3rd one passed in) provides access to the full data source object, you could use row.id.

    Equally, you could actually use row.multiple.account_hash if you wanted... :-)

    Allan

  • dsnapdsnap Posts: 23Questions: 3Answers: 0

    Thanks, Allan! Computers are hard!

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin

    Its keyboards I have a problem with - when my head connects with them :-)

    Allan

This discussion has been closed.