Link to home
Start Free TrialLog in
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?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER

But the thing is that the client's browser has a better idea of the timezone of the client, isn't?
Ah Ok.  You could always pass the client's timezone into the ajax request
That's a good idea.
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.
Another question, if i18n is taken into consideration, should we do it on server side or client side?
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
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think zephyr_hex's answer is the best. But thanks others for the great input!