1

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.

9
  • render also accepts a function render: function ( data, type, row ) so just build the list of hyperlinks there. Commented Jul 26, 2016 at 17:07
  • Possible duplicate of How to display a hyperlink in a cell with jQuery DataTables Commented Jul 26, 2016 at 17:07
  • @PaulAbbott: I used it as a function, I'm able to turn this into a comma separated hyperlinks for the first level, but not in this category level, I'm having a trouble retrieving title and id into this function. I know data.category.title is the trick but it is not working for this second level. Commented Jul 26, 2016 at 17:11
  • @dsh: No. This is not a duplicate of the question you mentioned. This is not just a hyperlink in a cell, this is slightly different because i'm asking about a array object. Not just an array. Commented Jul 26, 2016 at 17:14
  • data.category is an array so data.category.title is not going to work. You need to loop over data.category. Commented Jul 26, 2016 at 17:14

1 Answer 1

1

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;
    }
},
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I just tried this. Getting an eror Uncaught TypeError: Cannot read property 'length' of undefined.
As I said you were a lifesaver. I just changed data to full and everything works fine. \m/ Thanks a lot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.