Serverside swaps columns after drag-n-drop reordering.

Serverside swaps columns after drag-n-drop reordering.

mahussmahuss Posts: 23Questions: 6Answers: 0
edited September 2023 in Free community support

Hello,

I am really curious if I do something wrong.
When data are delivered to datatables "server-side", and then columns are drag-n-drop -way replaced, the operations of searching and sorting put the values in the original order. What strange, the column indexes in GET are kept originally so it seems the json fields should be mapped properly.
As the logic is server-side it may be difficult to share the code, but please look at the attached gif, maybe someone had similar issue and already have managed it.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    Answer ✓

    Use colReorder.transpose() to get the original column index. See the example in the docs. Here is a running example:
    https://live.datatables.net/qoliyehi/60/edit

    Also take a look at this ColReorder example with server side processing. See this note in the example:

    It is recommend that you use object based data with server-side processing and ColReorder, as this provides easily understandable mapping between the the columns and the data relation on the server, otherwise you need to work out array indexes on each call!

    Kevin

  • mahussmahuss Posts: 23Questions: 6Answers: 0

    Thank you Kevin!
    Applying object based data solves the problem. colReorder.transpose() - also really useful. Thank you for the help. Now everything works perfectly!

  • mahussmahuss Posts: 23Questions: 6Answers: 0

    Little additional question... I was really happy until I tried to sort by column (no additional button, just native Datables column-header sort). It seems in this case GET messages still sends a value which is not transposed.
    So we have in GET message:
    'order[0][column]': ['0'],
    even if the first column was swapped with another - unfortunately that is the value I use to sort in server-side script.
    However In your example everything looks fine. So Is this possible to send (somehow ) transoposed column number to backend in GET message?
    BTW - I see in your example POST is in use, maybe this is crucial?

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    Answer ✓

    Take a look at the ssp.class.php script, which is used by the server side examples. Find the static function order() function - line 123. In the function you will see this bit of code that finds the columns.data defined for that column:

                    // Convert the column index into the column data property
                    $columnIdx = intval($request['order'][$i]['column']);
                    $requestColumn = $request['columns'][$columnIdx];
    
                    $columnIdx = array_search( $requestColumn['data'], $dtColumns );
    

    Assuming you aren't using a Datatables supplied server side processing script you will need to do something similar to map to the column for the DB query. The mapping should work whether the column as been moved or not. The ssp.class.php script uses the $columns defined in the server side script. Click the Server-side script tab of this example to see an example of $columns.

    Whether you use GET or POST is dependent on what type of request the server script is expecting.

    Kevin

  • mahussmahuss Posts: 23Questions: 6Answers: 0

    Yes, I did not take into consideration that in the object-related json we have back-end column name so we can easily use it for this purpose. Once again thank you for the help! Now my backend logic works perfectly fine!

Sign In or Register to comment.