Avatar of condor888
condor888
 asked on

How to post-process column values in jQuery DataTables

 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?
jQueryJavaScriptAJAXJSONWeb Development

Avatar of undefined
Last Comment
condor888

8/22/2022 - Mon
Chris Stanyon

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.
condor888

ASKER
But the thing is that the client's browser has a better idea of the timezone of the client, isn't?
Chris Stanyon

Ah Ok.  You could always pass the client's timezone into the ajax request
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
condor888

ASKER
That's a good idea.
Steve Bink

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.
condor888

ASKER
Another question, if i18n is taken into consideration, should we do it on server side or client side?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Steve Bink

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.
ASKER CERTIFIED SOLUTION
zephyr_hex (Megan)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
condor888

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