rivaldid
rivaldid

Reputation: 85

datatables sort by column name

Shortly I need having column name instead of column number when apply sorting or searches. I know it's not implemented and the developer has said more times that he will be support it in the next version but I'm thinking about now. I'm using server side pagination so I'm using ajax.data. I would simply add new value to d.order[0] object sent by datatable. This is my pseudo:

vat table = $('#foo').DataTable({
"processing": true, 
"serverSide": true,
"ajax": {
 "url": uri_api,
 "method": "POST",
 "data": function(d) {
   if len(table.columns(0).data.tfoot.placeholder)>0 than d.order[0]['col_name']='col0name'
   if len(table.columns(1).data.tfoot.placeholder)>0 than d.order[1]['col_name']='col1name'
   return json.stringify(d);
 }
"columns": [
  { "data": "bar" },
  { "data": "baz" }
 ]
});

This is pure pseudocode, I don't know how to code it. Also with pure js. If in ajax part I use $(this) it uses the whole jquery library. Any hints?

Upvotes: 0

Views: 2655

Answers (1)

rivaldid
rivaldid

Reputation: 85

Many thanks to andrewjames I FIXED my issue, and works very fast too! Based on his tips I searched "datatables columns name with ajax data" and I found this link: Using column names with DataTables with AJAX data source Basically I don't followed the solution with green flag, that solution didin't work for me. I used ahmeti answer, I have added this lines:

fnServerParams: function(data) {
    data['order'].forEach(function(items, index) {
        data['order'][index]['column_name'] = data['columns'][items.column]['data'];
  });
},

Regards.

Upvotes: 1

Related Questions