3

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?

1 Answer 1

5

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 ] }
],
...
Sign up to request clarification or add additional context in comments.

1 Comment

The aoColumnDefs will work if I give it a json object, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.