sorting with column name

sorting with column name

rivaldidrivaldid Posts: 55Questions: 6Answers: 2

I know the rule about the test case but it's a serverside implementation so it's hard for me make it.
I just explain a simple question: I know there are tons of question about this but I don't understand how to do it.
Shortly I need having column name instead of column number when apply sorting or searches.
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.
When serverside is true the backend don't handle it but just answer. To do it server needs share a table structure because datatable send cardinal integers to filter or sort it's data. That is a big limit because I can't hide or move columns because it will corrupt the table structure shared with server previously.
Because if I have three columns in db they are 0 foo, 1 bar, 2 baz. Datatable instead know 1 foo 2 bar 3 baz, so if I move foo after bar the new table structure will be 0 bar 1 foo 2 baz and this is ONLY in the frontend. The backend doesn't know about this change, the backend will keep know 0 foo 1 bar 2 baz.

This question has an accepted answers - jump to answer

Answers

  • rivaldidrivaldid Posts: 55Questions: 6Answers: 2
    Answer ✓

    Ok I solved with column[i][name] and this code in datatable definition:
    fnServerParams: function(data) {
    data['order'].forEach(function(items, index) {
    data['order'][index]['column_name'] = data['columns'][items.column]['data'];
    });
    },
    Bye bye

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    Super - thanks for posting back with your solution.

    Allan

This discussion has been closed.