Can I add an if-construction while rendering?
Can I add an if-construction while rendering?
arie0512
Posts: 18Questions: 5Answers: 0
I see some options for adding an If construction while rendering, see https://datatables.net/manual/data/renderers
But I can't figured it out how to add this one. I have now this render:
columns: [
{ data: null, render: (data) => data.client.gender + ' ' + data.client.firstname + ' ' + data.client.lastname
}
]
If the records hold this data:
Gender Firstname Lastname
M John Doe
Than I would like to have in the column Mister John Doe.
How to add the if-contruction into the render?
I have try to add ... => if (data.client.gender === 'M') { return Mister } but I get an error for using 'if'
Any idea how to achieve this?
Best regards,
Arie
This question has an accepted answers - jump to answer
Answers
Hi Aire,
You sure can, but your Javascript synax for the arrow function is slightly wrong. You need brackets around the function:
For more details on the Javascript syntax for arrow functions, the MDN reference is excellent.
Allan
Looks like you need to place quotes around
Misterlike this:If this doesn't help then please provide a link to a test case so we can see exactly what you have and the error you are getting so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Hi Allan and Kevin,
Tnx for getting back to me, it was the missing brackets who bracks it up.
The next 'problem' is that the first and last name isn't showing now, I get only the results from the IF construction but no first name and last name.
Any idea how to add the first and last name into the column?
Best regards,
Arie
Your if/else logic will never allow the return statement in line 9 to be executed. One option is to concatenate the first and last name to each return statement above, for example:
And remove line 9. Or do something like this:
Kevin
Hi Kevin, Tnx a lot for clearing this one out for me, it's working like a charm now!