DataTable Fundamentals Part 1

I’m a big fan of jQuery DataTables. It’s my new grid of choice for interfacing. I jumped on the bandwagon at at the release of 1.10.0 which was a major versioning change/re-working. The documentation was lacking a bit at the time so I had to do a lot of trial and error to figure things out. I used jqGrid for years before making the transition. I find DataTables to be cleaner (as far as object organization) and more understandable. I feel that any programmer that comes behind me will have an easier time deciphering code I’ve written for DataTables verses jqGrid. It also looks more like a table to me as opposed to some kind of widget. You can probably accomplish more advanced things with jqGrid, but a lot of that is unnecessary fluff that doesn’t lend itself to the real, business world.

Display Values

Some trivial features alluded me at first. Such as how do I modify the display without compromising the underlying data? Some record values don’t translate well for users. For instance, in a database record you may have a field with a value of stdenv, but you want to show it as Standard Envelope to the user. If your code continues to interact with the database you may need to retain that original value.

In jqGrid you would often use a formatter. To translate a date you may do something like this:
So how do you do this in DataTables? When working with this plugin I’m almost always using an ajax data source as objects. I thought I was translating this correctly by manipulating the ajax result via the ajax property but later found out that was changing the underlying data. Then I discovered I could change it in the createdRow property. That technique was okay but for every cell that renders there is two DOM writes (the write by the plugin, then my translated overwrite). Finally, I came across an elegant solution that makes me happy. I already have a columns property defined in my DataTable object:
To translate a value I use the render property. You can read about it here. It can be as simple as this:
You can also translate the data in these types of ways:
To set a default value use the defaultContent property. You can read about it here:

Sub-Sorting

Sometimes you want your grid to sub-sort related columns when sorting on a column. This is simple to declare as well using the orderData property. You can read about it here:
The column indexes are in the order they appear: name: 0, position: 1, date: 2. So clicking on position will sub-sort name. Likewise, sorting descending on position will sub-sort descending on name.