Wondering how to use the built in date render (or number) from a custom render function

Wondering how to use the built in date render (or number) from a custom render function

funk16funk16 Posts: 7Questions: 2Answers: 0

I have a function defined in the render attribute of a column, which will go through some processing to determine the data type... Long story short.. I don't know the data type ahead of time, this function will determine if I will display a date, a boolean value, string, etc...
I saw in the release notes that date and number rendering are now built in. So I was hoping to alter my code to use the built in render once I determined the column is a date for example...

In the example documentation I can see that from the render attribute I could simply set render to DataTable.render.date().

however in my case I have a function defined... how do I call the render.date() function from within my own function... or can I not do that?

I tried accessing it via passing the render params in my function like so:

$('#tableId').DataTable.render.date(data, type, row, meta)... but this yielded a not a function error...

So is there a way at the point in my function that I determine the cell will hold a date, to just return the result of that render.date() helper?

Answers

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    Hi,

    The way the date / time renderer works is to return a function, so you could do something like:

    DataTable.render.date( from, to, locale, def )( data );
    

    Where the arguments for DataTable.render.date() are the same as in the documentation for it and data being the value you want to render.

    More generally, the renderers are allowed to return objects as well as functions. We have a utility method internally to handle that sort of thing, and I'm wondering if we should expose it to make use of the renderers externally easier...

    Allan

  • zexelonzexelon Posts: 1Questions: 0Answers: 0

    For what its worth the current version of DataTables that I have requires the following format:

    var price = DataTable.render.number( ',', '.', 2, '$').display(12.5);
    

    Using only DataTable.render.number() returns an object, which is rather handy if you want to use the same format multiple times but does not actually render a value and causes an error if done DataTable.render.date( from, to, locale, def )( data );.

Sign In or Register to comment.