How to use colomn name in render function
How to use colomn name in render function
Hey guys,
I have a question that I think has a easy answer but I cant find how to do it on the forum..
The folowing code is used to send info to a ajax script when checkbox is checked or unchecked. What I now only sent the the data from kwartaal_id to the script. But I also need the colomn name to be send to the ajax script. So I need the colomn name the same as I do with the row.kwartaal_id. How do I do that? I imagine it would be like this..
"columns": [
{data: 'kwartaal_klant',
render: function ( data, type, row, meta ) {
return ''+data+'';
} } ,
{data: 'kwartaal_notes',
render: function ( data, type, row, meta ) {
return ''+data+'';
} } ,
{data: 'kwartaal_1'},
{data: 'kwartaal_2'},
{data: 'kwartaal_3'},
{data: 'kwartaal_4'},
{data: 'kwartaal_user',
render: function ( data, type, row, meta ) {
return ''+data+'';
} } ,
],
Before:
{targets: [2,3,4,5],
render: function ( data, type, row ) {
if ( type !== 'display' )
return ""; //Empty cell content
else { //if column data is 1 then set attr to checked, use row id as input id (plus prefix)
return '<label class="switch"><input type="checkbox" ' + ((data == 1) ? 'checked' : '') + ' value="' + row.kwartaal_id + '" class="active" /><div class="slider round"><span class="on">ON</span><span class="off">OFF</span></div></label>';
}
return data;
},
After: (added the ''name'' atribute)
{targets: [2,3,4,5],
render: function ( data, type, row ) {
if ( type !== 'display' )
return ""; //Empty cell content
else { //if column data is 1 then set attr to checked, use row id as input id (plus prefix)
return '<label class="switch"><input type="checkbox" ' + ((data == 1) ? 'checked' : '') + ' value="' + row.kwartaal_id + '"
class="active" name="' + columnName + '" /><div class="slider round"><span class="on">ON</span><span class="off">OFF</span></div></label>';
}
return data;
},
Answers
Are you referring to the
columns.name
option? If yes thencolumns.render
probably won't work. There is nothing builtin to get thecolumns.name
withincolumns.render
.I guess what you are asking about is this
class="active" name="' + columnName
. If yes, what are you expecting thecolumnName
to be? The column header title? You can use thecolumn().header()
API to get the title. This example show how to usecolumns.createCell
to get the column header title.The best way to get help is to build a test case showing what you have with details of what you want. You can update my test case.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin