Fetch value from other field in a render: function

Fetch value from other field in a render: function

asleasle Posts: 96Questions: 28Answers: 0

On this page https://dev.biofokus.no/prosjekter/ I have a column that I want to filter. If the value is greater than 0 it prints a PDF icon with a link to the PDF. The problem is that the html is printed but I can not get the other value into the function. Am I doing it wrong or should I be using a "rowcallback" ?

E.g. the row (data:12) shows "3". But I want to link to data[9]. It just shows "undefined". So if the field is greater than 0 the link is always like this: **https://dev.biofokus.no/prosjekter/?q=undefined**.

<td class=" center"><a href="/publikasjoner/?q=undefined" target="_blank" title="Åpne rapport i nytt vindu"><i class="fas fa-file-pdf fa-lg"></i></a></td>

Here is the datatables code:

{ "data": 12,
               "defaultContent": '',
               "className": "center",
               "render": function ( data, type, row, meta ) {
                 if (typeof(data) !== 'undefined' && data > 0)
                    {
                        return '<a href="/publikasjoner/?q='+ data[9] + '" target="_blank" title="Åpne rapport i nytt vindu"><i class="fas fa-file-pdf fa-lg"></i></a>';
                    }
                    }
}

Answers

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    The data parameter is the current column/ . To access the data in other columns in the row you would use the row parameter. The columns.render docs explain what each parameter is for. There is also an example of creating links in the doc page.

    Kevin

  • asleasle Posts: 96Questions: 28Answers: 0

    This works, in case someone is wondering. I check if this field (12) is larger than 0. If so I create a link to another page using the value from column 9 from the json array. So fetch field 9 in the current row.

    { "data": 12,
                   "defaultContent": '',
                   "className": "center",
                   "render": function ( data, type, row, meta ) {
                     if (typeof(data) !== 'undefined' && data > 0)
                        {
                            return '<a href="/publikasjoner/?q='+ row[9] + '" target="_blank" title="Åpne rapport i nytt vindu"><i class="fas fa-file-pdf fa-lg"></i></a>';
                        }
                        }  
                },
    
This discussion has been closed.