Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change column order with jQuery DataTables

My code is like this : http://jsfiddle.net/oscar11/ebRXw/805/

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "responsive": true
    } );
} );

In my example:

  • Column 0 = Name
  • Column 1 = Position
  • Column 2 = Office

I want to change it to be like this without changing the HTML:

  • Column 0 = Salary
  • Column 1 = Start date
  • Column 2 = Age
like image 529
moses toh Avatar asked Jan 08 '23 08:01

moses toh


1 Answers

SOLUTION

Use columns.data to set data source index for each column.

var table = $('#example').DataTable( {
    "responsive": true,
    "columns": [
        { "data": 5 },
        { "data": 4 },
        { "data": 3 },
        { "data": 0 },
        { "data": 1 },
        { "data": 2 }
    ]
} );

Don't forget to adjust table headings in thead section accordingly.

DEMO

See this jsFiddle for code and demonstration.

like image 91
Gyrocode.com Avatar answered Mar 02 '23 05:03

Gyrocode.com