How to post-process column values in jQuery DataTables

condor888
condor888 used Ask the Experts™
on
 var table = $('#data-table').DataTable(
                {
                    ajax: {
                        type: 'GET',
                        url: '/temperature'

                    },
                    bFilter: false,
                    "bLengthChange": false,
                    aaSorting: [],
                    columns: [
                        {data: "t"},
                        {data: "c"}

                    ]
                }
            );

Open in new window

 
I have the above code to consume some JSON data to populate a table using DataTables (https://datatables.net/). The JSON format is like below:
   
{data: [{t:1459192455326, c:2},{t: 1459192455326, c:3}]}

Open in new window

.

So how can I do some post processing of the returned JSON (convert the timestamp to human readable form) in order to render the table?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
If you have the chance, do the processing on the server-side - i.e. in your temperature script and then return the data as you need it.

Author

Commented:
But the thing is that the client's browser has a better idea of the timezone of the client, isn't?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Ah Ok.  You could always pass the client's timezone into the ajax request
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
That's a good idea.
Top Expert 2004

Commented:
Sending the timezone in the request is a good idea...your server can handle the conversions in a more consistent way.

That said, you do have post-processing options in DataTables.  See the list of events you can hook into.  You're probably looking for xhr.dt or processing.dt.

Author

Commented:
Another question, if i18n is taken into consideration, should we do it on server side or client side?
Top Expert 2004

Commented:
I18n functionality in DataTables consists of setting a language object with the appropriate text strings.  It is more like token replacement rather than true internationalisation.  See what the manual says about I18n.  You have the choice of setting the strings locally, or storing sets of string on the server and having DT load them as needed.  I prefer the second method.
Top Expert 2010
Commented:
You can use column rendering to format the date before displaying in the datatable.

Here is the render function definition.

Author

Commented:
I think zephyr_hex's answer is the best. But thanks others for the great input!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial