Adding data to a cell's data

Adding data to a cell's data

CatCat Posts: 18Questions: 8Answers: 0

I'm still pretty new to webdevelopment but I've managed to get together a server-side-processing DataTable with most of the functions I need, but I'm missing something very important...

My DataTable lists content posted by my website's users along with their usernames in a username column, now I need to make it so that when you click a user's name in the DataTable it opens a link to their profile page. Turning their name into a link with appropriate target would work fine, but if I have to store the entire anchor tag with their link and username in the database it'll mess up the sorting and cause all manner of inconveniences, (and I have a few other entries I have to turn into links too)

I can store the address to their profile in another column, but the question is, how would I hook up the url to the username in the form of a link? I followed this example: http://datatables.net/examples/advanced_init/row_callback.html do add styles to various columns and entries already which works fine and looks like it could potentially do what I need, but I couldn't figure out how to do it. Every example I found of createdRow just adds a class, no custom strings or anything else. I need to add extra html code and data from another column to the username column, how would I do that?

Any help will be greatly appreciated, I suspect there's an easy solution to this that I just haven't found, thanks in advance

This question has an accepted answers - jump to answer

Answers

  • hhmhhm Posts: 24Questions: 3Answers: 2
    Answer ✓

    You're on the right track. See this example:
    http://datatables.net/reference/option/rowCallback

    In the line

    $('td:eq(4)', row).html( '<b>A</b>' );
    

    a little HTML is added. You can add what you want or is accessable, either fixed content, JavaScript variables or the content of other columns. Just concatenate everything like any string in JavaScript.

    Eg. putting in the content of column 5 (which holds the link) into column 4 could look like this

    $('td:eq(4)', row).html('<a href="' + data[5] + '">' + data[4] + '</a>')
    

    HTH, Hans

  • CatCat Posts: 18Questions: 8Answers: 0

    Ahh... perfect! Just what I needed, thanks a lot Hans! :)

This discussion has been closed.