jQuery Datatables reorder columns from ajax source

datatables, jquery

Solution

Not entirely sure I've understood the question, but this might be of use:

(1) DataTables has a ColReorder plugin.

http://datatables.net/extensions/colreorder/

In the link above, you can select a column with your mouse and then drag the column to a different position in the table.

In another example of using the ColReorder plugin, you can predefine your column order, as stated on the datatables site:

http://datatables.net/release-datatables/extensions/ColReorder/examples/predefined.html

ColReorder provides the ability to specify a column ordering which is not that of the HTML (which typically you will want) through the parameter oColReorder.aiOrder. This is an array of integers with the column ordering you want.

(2) I believe you could also make use of aoColumnDefs and mDataProp if your data was an array of objects. (also see http://datatables.net/usage/columns)

...
"aoColumnDefs": [
    { "mDataProp": "FirstName", "aTargets": [ 0 ] },
    { "mDataProp": "LastName", "aTargets": [ 1 ] },
    { "mDataProp": "LicenseNumber", "aTargets": [ 2 ] }
],
...

Problem

I am using the jQuery plug-in Datatables, is it possible to change the order of the columns without changing the source data? Here is an Example of what I have now. Currently it is LicenseNumber, FirstName, LastName I would like it to be FirstName, LastName, LicenseNumber Is there a parameter I can add to the datatable setup?

Original source