stackminu stackminu - 4 months ago 16
jQuery Question

jQuery datatables array of objects

I have a server side data as following for a jQuery datatable.

"data": [
{
"id": 1,
"title": 'Hello World"
"category": [
{
"id": 1,
"title": "Hello World"
},
{
"id": 1,
"title": "Foo Bar"
},
]
}
]


Now I needs to show these "categories" as a hyperlink in the category column cells of my datatable and it should be comma seperated like following

Source:
<a href="ID">Hello World</a>, <a href="ID">Foo Bar</a>


Example: Hello World, Foo Bar etc...

I already can render this as comma separated using
"render": "[, ].title"
but not this level as as a hyperlink. Can anyone help me?

EDIT

{ data: 'category',
render: function ( data, type, full, meta ) {
$.each(data.category, function( index, value ) {
return value.title;
});
}
},


This is what I have done so far. But still not working. All I needs to pass title and id into this loop. So I can use it to build a hyperlink.

Answer

I'm pretty blind here without more context, but...

{ data: 'category', 
    render: function ( data, type, full, meta ) {
        var result = '';
        $.each(full.category, function( index, value ) {
          result = result + '<a href="' + value.id + '">' + value.title + '</a>';
          if (index < full.category.length)
              result = result + ', ';
        });
        return result;
    }
},