Function expression usage in columns.render
Function expression usage in columns.render
![gunjan.varshney@gmail.com](https://secure.gravatar.com/avatar/6d02f57dcce147f1ddf97adeb8518ba8/?default=https%3A%2F%2Fvanillicon.com%2F6d02f57dcce147f1ddf97adeb8518ba8_200.png&rating=g&size=120)
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
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