How to use one column value in another column for hyperlink?

How to use one column value in another column for hyperlink?

dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

The primary key for Projects is the id.
Users need to be able to select a single project.
I want to use the project name as the link, but send the project id in the URL.
The link in the data: 'id' column works correctly.

In the code below, I need to replace
" [here I want to insert the 'id' value]"
with something like
data: 'id'
but I don't know the syntax.
Does anyone know how to work this? Thanks

var table = $('#example').DataTable( {
        dom: 'Bfrtip',
        ajax: {
            url: '/index.php/Ajax/Projects',
            type: "POST"
        },
        columns: [
            { data: 'id',
                render: function ( data, type, row, meta ) {
                return '<a href=/index.php/Project/viewSingleProject/' + data + '>'+data+'</a>';
            }
            },
            { data: 'projectname',
                render: function ( data, type, row, meta ) {
                return '<a href=/index.php/Project/viewSingleProject/ [here I want to insert the 'id' value]>'+data+'</a>';
            }
            },

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    The row parameter of the columns.render function contain the values for each item in the row. You would do something like this:

    return '<a href=/index.php/Project/viewSingleProject/ ' + row.id + '>'+data+'</a>';
    

    Kevin

  • dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

    Thanks,
    I tried this

                { data: 'projectname',
                    render: function ( data, type, row, meta ) {
                    return '<a href=/index.php/Project/viewSingleProject/' + data.id + '>'+data+'</a>';
    

    and the value of data.id is undefined
    Also tried data[0], data.0.id, etc

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    So where Kevin suggested "row.id", you went with "data.id"? Or am I missing something?

  • dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

    Sorry, I thought I tried that and it didn't work.
    Just tried it again and it works perfectly.
    Thanks to all!

This discussion has been closed.