Function expression usage in columns.render

Function expression usage in columns.render

Hello everyone,

New to Datatables and I love it. I had a question.
The way that I controlling datatables initialization for various pages in my code is via server side. from the server I get a fully constructed JSON string options which I pass on the datatables contructor.

For one rendering requirement of a date column I have to use a function expression which is declared elsewhere in my javascript code. as an example, on the page I get following JSON string contructed where 'datatableconvertdate' is actually a function expression. But datatable throws an error when I start adding rows / data ( is in javascript object) via ajax call. the debug is at http://debug.datatables.net/uluvez

{"columns":[{"data":"id"},{"data":"file.fileName"},{"data":"file.orgname"},{"data":"file.fileNamePattern"},{"data":"processStatus"},{"data":"triggerredOn","render":"datatableconvertdate"},{"data":"completedOn","render":"datatableconvertdate"}]}

If I change my above JSON string with following then the code works fine.

{"columns":[{"data":"id"},{"data":"file.fileName"},{"data":"file.orgname"},{"data":"file.fileNamePattern"},{"data":"processStatus"},{"data":"triggerredOn","render":function ( data, type, row, meta ) {if ( type === 'display' || type === 'filter' ) {return (moment(data).format("ddd MM/DD/YYYY (HH:mm)"));}return data;}},{"data":"completedOn","render":function ( data, type, row, meta ) {if ( type === 'display' || type === 'filter' ) {return(moment(data).format("ddd MM/DD/YYYY (HH:mm)"));}return data;}}]};

I do not want javascript code to be sitting on my server code which is why I was looking for using function expressions. is there a way out?

This question has an accepted answers - jump to answer

Answers

  • gunjan.varshney@gmail.comgunjan.varshney@gmail.com Posts: 5Questions: 2Answers: 1
    Answer ✓

    I solved the issue by replacing on server "datatableconvertdate" string with following function:

    function ( data, type, row, meta ) { return window['datatableconvertdate'].apply(null, arguments);}

    using above I was able to still create function expression with variable name "datatableconvertdate" rather then in my options object for datatables

This discussion has been closed.