Column Render combined with non-fixed columns

Column Render combined with non-fixed columns

h.nijkamp17h.nijkamp17 Posts: 10Questions: 3Answers: 0
edited May 2017 in Free community support

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

  • allanallan Posts: 61,771Questions: 1Answers: 10,112 Site admin

    Hi,

    Quick point first:

    columns: eval(JSON.stringify(columns, null, 2))

    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

  • h.nijkamp17h.nijkamp17 Posts: 10Questions: 3Answers: 0

    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??

  • allanallan Posts: 61,771Questions: 1Answers: 10,112 Site admin

    It should be! createdRow is executed whenever the tr element for the row is created. So perhaps you have deferRender 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

This discussion has been closed.