Add extra column and set it's values from another column.render
Add extra column and set it's values from another column.render
Rawland_Hustle
Posts: 94Questions: 16Answers: 0
Hi!
I'm using the ajax option to fetch my data. My json objects have 8 nodes, but I've got 9 columns in my table because I'm using columns.render for an extra column. Now I want to add a 10th column, which values I want to set from the column I'm using columns.render on. How do I do that?
(I know I can add one more columns.render column, but I don't know how to reference it since it does not have an identifier.)
Thanks!
$('#myTable').DataTable( {
"ajax": "https://xxxxxxxxxxxxxx.json",
"pageLength": 50,
"columns": [
{ "data": "FirstName" },
{ "data": "LastName" },
{ "data": "Mail" },
{ "data": "Role" },
{ "data": "Registered" },
{ "data": "Gym" },
{ "data": "Country" },
{ "data": "Phone" },
{
"data": "Phone",
"render": function ( data, type, row, meta ) {
// HERE I WANT TO SET THE VALUE OF NEXT COLUMN!!!
}
}
]
} );
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
The
row
parameter, as noted in thecolumns.render
docs, contain the data for the row. You could do something like this:Does this answer your question?
Kevin
@kthorngren Thanks for your answer Kevin! I don't think you answered my question though. I know how to get data from other columns, but in this case I need to set data in a new column. My problem is that I don't know how to reference this new column. See the javascript comment in my code.
I just found out about the "columns.name" option so now I know how to reference the new column. But how do I set the value of it?
Are you trying to take a rendered value and use it in another column? There isn't a way to do that in
columns.render
. See ifrowCallback
ordrawCallback
will do what you want.Kevin
@kthorngren Where my JavaScript comment is in my question, I'm doing some calculations. Depending on the result of the calculations, I want to set the value of the next column.
@kthorngren Where my JavaScript comment is, I'm using row.Country to get the data of the column called Country. Instead of getting_data this way, how can i _set it?
Like I said there isn't a way to do this with
columns.render
. You can't set the value of another column nor can you retrieve rendered data from another column. Maybe you can also do the calculation in the "next" column to determine the value to set.Kevin
@kthorngren Okay, thank you!