Column Render combined with non-fixed columns
Column Render combined with non-fixed columns
Hi all,
Here is a problem I can't seem to figure out by myself. My question is about the column render option, but I need to point out the following first.
In my application the columns are non-fixed (generated by userdata). So what I do for example is this:
var col = {};
col.data = "example";
col.title = "Example Title";
col.visible = output == 1;
col.orderable = orderable == 1;
col.searchable = searchable == 1;
col.width = "200px";
columns.push(col)
This code is an example. This will be looped for multiple columns.
With this in mind I found out earlier, the DataTable could be initialized like this:
$('#example').dataTable( {
dom: 'rtp',
columns: eval(JSON.stringify(columns, null, 2))
});
HERE IS THE PROBLEM:
With fixed columns I would do this:
data: 'example', title: 'example', orderable: false, width: "120px",
render: function (data, type, row) {
if (type === 'display') {
return '<input type="checkbox" class="editable">';
}
return data;
}
How can I add a render option to one column?
Hope you can help me out! Thanks!
Answers
Hi,
Quick point first:
You shouldn't need to stringify it and then eval - simply
columns: columns
should work.I think the main issue you are running into is that JSON cannot represent a function - that would be a potential security issue. If you make the change I suggested above then you won't be stringifying it, so it should work without any issue as a function.
Allan
Thanks Allan,
Unfortunately, without stringifying my code doesn't seem to run well.
I found out about the createdRow callback function! Great, because this helped me solve the problem in a very simple way.
But now I have a new problem. The createdRow isn't triggerd when a new row is inserted using:
table.row.add(data).draw();
Do you have any ideas??
It should be!
createdRow
is executed whenever thetr
element for the row is created. So perhaps you havedeferRender
enabled and it hasn't been created yet?I'd need a test case showing the issue for that to be able to give a firm answer.
Allan