columns.render does not allow to reference the column of data

columns.render does not allow to reference the column of data

fbi1970fbi1970 Posts: 1Questions: 0Answers: 0
edited March 2014 in DataTables 1.10
at doc page: http://next.datatables.net/reference/option/columns.render
is stated the proto of: function render( data, type, row )

data contains data of cell, type is 'display'/'filter'/etc. and row is reference to full row data, but it's not possible to get the column where data is.

I think you should add an argument with column index or column reference.

Thanks in advance

Replies

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin
    What's the use case for it? I've thought about it before but never really saw the need for it (the data reading function doesn't actually know what column it is reading from, just what data it should read, so it would add a surprising amount of code - only 100 bytes perhaps, but I'm already 700 bytes over my limit for 1.10 and need to find something to cut it down - struggling at the moment!).

    If you want a dynamic function that can be used for multiple columns, then a simple closure that returns a function could be used.

    Thanks,
    Allan
  • DabooDaboo Posts: 6Questions: 1Answers: 0
    At some point I really had to render all cells with the same function in a table with many columns , but the function was supposed to know which data field (column "name" index) it was rendering. Here is one way of doing it easily avoiding to manually write all closures:

    [code]
    var renders = {};
    renders.base = function(data, type, full, name) { /* ... */ }
    $.each(['col1','col2'], function( i, v ) {
    renders[v] = function(d,t,f){ return renders.base(d,t,f,v) }
    });

    // columns for datatable init:
    [
    { data: "col1", render: renders.col1 },
    { data: "col2", render: renders.col2 }
    ]
    [/code]

    Hope it helps
  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin
    Thanks for sharing that with us. Another option:

    [code]
    function render ( i ) {
    return function ( data, type, full ) {
    return 'whatever :-)';
    };
    }

    // columns
    [
    { data: "col1", render: render( 1 ) },
    { data: "col2", render: render( 2 ) }
    ]
    [/code]

    Allan
This discussion has been closed.