Problems on ajax import data render only if column contains data
Problems on ajax import data render only if column contains data
I have am using ajax: to import json data. One field contains a URL and then I build a link from that. The problem is I want to display blank if the field is empty.
It does not work as expected. Some fields contain a URL and are not displayed while some fields are emtpy and still display a link which is then emtpy,
Example: https://biofokus.no/publikasjoner/
The problem field is "Rapport". I want to show a PDF icon with a link. You see many rows show a PDF with no link. These are empty. Still some rows are missing PDF icon and I know they have a link. I check if there is no value and if the link contains "http".
My code for the field:
{ "data": 3,
"defaultContent": '',
"className": "center",
"render": function ( data, type, row, meta ) {
if (typeof(data) !== 'undefined' && data.indexOf('http'))
{
return '<a href="'+ data + '" target="_blank" title="Åpne rapport i nytt vindu"><i class="fas fa-file-pdf fa-lg"></i></a>';
}
}
},
Answers
I think I solved it this way. Had to specify the data column again in the function. So instead of "data" I had to use "data[3]" . I thought the function would understand that what data I was referring to?
data
within the render function is the data for that column. Your column contains strings and data[3] will refer to the forth position in the string. If the string is null, which is the case, then typeof(data[3]) !== 'undefined' will be undefined which is why your if statement is working withdata[3]
. You can use console.log statements to see the actual data within the render function. Usingdata[3]
is not what you want to use it just works by accident.I grabbed an example of your data with a blank Rapport field and put this test case together:
http://live.datatables.net/jogulucu/1/edit
I have a console.log statement so you can see the data and I changed the if statement to
if (data.indexOf('http') >= 0)
. You will need to test to make sure this works for all your data.Kevin