Set column render in events (preInit.dt, init.dt)
Set column render in events (preInit.dt, init.dt)
TinkerBug
Posts: 6Questions: 1Answers: 0
in DataTables
Is there a way to set the column render in some events?
I tried in preInit with this but the render is not used
var columns = $(e.target).DataTable().settings().init().columns;
for (var i = 0; i < columns.length; i++) {
if (columns[i].render === undefined) {
columns[i].render = $.fn.dataTable.render.text();
}
}
This question has an accepted answers - jump to answer
Answers
columns.render
runs as needed for rendering the column data for sorting, searching and display. It is only configured in thecolumns
orcolumnDefs
options.What are you trying to do?
Kevin
To avoid Cross-Site Scripting I'm trying to set a default renderer ($.fn.dataTable.render.text) instead of changing options to all the tables.
Have you seen the setting default options docs? Maybe that will work for you.
Kevin
Yes, I have other defaults set in that way but I cannot see any default render option.
I tried with
but it dosen't seem to work
Try using
columnDefs
instead with thecolumnDefs.targets
set to the appropriate columns. Read the Conflict resolution section to learn which options take priority for overlapping options.Kevin
Got it.
It's column.render, not columns.render
https://datatables.net/reference/option/columns.render
Many thanks for your help
No, there is no option
column
. Its not doing anything for you as its not being applied. UsecolumnDefs
.Kevin
Strange because it works.
And in datatables source code there's this (I'm using version 1.10.18)
It does seem to work, Learn something new everyday!
Maybe its an undocumented feature @colin or @allan is using
column
a documented somewhere for default options? If not it should be - seems very useful.Kevin
BTW also this works
Example
columnDefs
would be the way to go, that's officially supported and documented, the other option is probably something that really shouldn't be there and may get removed in the future,Colin