Default Sort Order Using Null Column in SSP

Default Sort Order Using Null Column in SSP

JavaScriptDudeJavaScriptDude Posts: 2Questions: 0Answers: 0

I discovered an issue related to server-side processing and default sort order that triggers an error Unknown field: (index 0).

To replicate, alter DataTables/example/simple/simple.html $('#example').DataTable( { declaration by adding:
dom: "Bfrtip",
ajax: { url: "<url_to_use>", "type": "POST" },
serverSide = true,
...

In this example, the sort sent to the server is defaulted to sort: [[0, "asc"]] but since the first column is derived with data = null, the server side code in .NET library fails with Unknown field: (index 0) error. I did not test any other server side libraries.

One way to resolve this, is to add an explicit order declaration like order: [[1, 'asc']],

Maybe the core library should do an inspection of the data property of each column and default the sort to be the first column with a non-null data property.

On a side note, the .NET library requires the ajax POST as it ignores the query string. Not sure if this is an oversight but it was a code trap for me that was not trivial to debug.

Hope this helps others.

Replies

  • JavaScriptDudeJavaScriptDude Posts: 2Questions: 0Answers: 0
    edited January 2022

    Another, better work around for sorting issue is to do add the first_name and last_name columns to the table as hidden columns and then define orderData property for the derived Name column to use the the two new hidden columns.

    { data: "first_name", visible: false},
    { data: "last_name", visible: false},
    { data: null, render: function ( data, type, row ) {
        // Combine the first and last names into a single table field
        return data.first_name+' '+data.last_name;
    }, orderData: [0,1] },
    { data: "position" },
    ...
    
  • allanallan Posts: 61,663Questions: 1Answers: 10,095 Site admin

    Do you mean this example? If so, then yes, I would expect server-side processing to fail on the first column since it has data: null in the column declaration. The renderer is a client-side construct, so the server can have no way to know what column in the database you want to have it sort on.

    One solution is to set data: 'first_name' which will instruct the server to sort on that column. It falls apart if you have multiple people with the same name - for that you could use the columns.orderData option along with hidden columns as you have done.

    Regards,
    Allan

Sign In or Register to comment.