Sorting function doesn't work with ColReorder

Sorting function doesn't work with ColReorder

marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0
edited May 2017 in Free community support

Hello guys,

I need your help, I've tried to find everything on the internet regarding this issue and I couldn't get a solution yet.. I'm going mad with it and it's time to ask you, at the end I'm not an expert.

So, at first place, I can make work Datatables without problems, as well for ColReorder plugin, but I'm having troubles. You can have a look to my testing enviroment and reproduce problems following these steps:

1-. Go to my testing enviroment.
2-. Drag last column, MinDeposit, to first position, before ID column.
3-. Try to sort MinDeposit column but you will be sorting ID column, as ID column was in position 0.

It means that, at initial state, you can sort perfectly any column, but once you move a column, sorting will be applied to initial column order.

DataTables debugger: https://debug.datatables.net/iwufaq

I hope I was clear enough for you!

This question has an accepted answers - jump to answer

Answers

  • marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0

    Push up.

  • marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0

    Anyone?

  • allanallan Posts: 61,657Questions: 1Answers: 10,094 Site admin

    Hi,

    You are using server-side processing, so it is up to your ajax_list script to sort the data correctly. What code are you using in that script? Does it take account of the fact that the columns can be reordered? If not, that's the issue.

    Allan

  • marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0

    Hello Allan,

    Thank you very much for your reply but I don't understand your point of view.

    Script is just passing data from db to datatables. Everything is working as expected but it's not working sorting functionality, allow me to explain.

    If you don't move columns, it's sorting right.

    If you move columns, it will sort as well but main column position. As an example: If I click on first title, although I've moved first column to 5th position, first column will be sorted, as it was in first position at original state.

    I think it's a question related to sorting mapping or something like this.

  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    Allan's explanation is correct. Your server-side script is responsible for sorting.
    If it has no idea that columns may be re-ordered, then that is the issue.

  • marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0

    I know that this explanation is correct, but this scenario is different, so this is not the right solution as it's not the problem guys..

    I'll try to explain it one more time:
    -. There are 7 columns (ID, AVATAR, COMPANYNAME, CURRENCYCODE, CURRENCYNAME, COUNTRY and MINDEPOSIT)
    -. As an example, steps to reproduce:
    1-. Move ID column, from position 0, to AVATAR column, position 1, then AVATAR column will be at position 0 and ID column at position 1.
    2-. Click to sort on AVATAR column, at position 0, it will sort column in position 1, ID column, as it was before in original position 0.
    3-. So, if you click to sort on ID column, at position 1, it will sort column in position 0, AVATAR column, as it was in original position 1.

    Maybe you understood this situation from beginning but your answers aren't enough clear for me. Could you give an example or something like that?

  • allanallan Posts: 61,657Questions: 1Answers: 10,094 Site admin

    Hi,

    Yup - no problem. The key thing you have to remember with ColReorder is that is is actually reordering your columns - its not just happening on the visual layer but also at the data layer.

    So when you load the page and ask the server to sort by column index 0 (i.e. the first column) it will sort by the ID column.

    Then change MinDeposit to be the first column and sort by index 0. This time the DataTable isn't asking you to start by the ID, but rather by MinDeposit - since that is what is in index 0. The DataTable just tells the server - I want to order by column index x.

    You have you use the information sent in the columns property that the server-side processing protocol defines to determine what column in your SQL database that is.

    That's easy with a static order, and my guess is that is what your script is assuming. But that isn't true when you have server-side processing and ColReorder.

    Btw - its only really worth using server-side processing when you have tens of thousands of rows.

    Allan

  • marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0

    Thank you Alan for your time.

  • marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0

    Thank you tangerine.

  • marcvidalimmarcvidalim Posts: 8Questions: 1Answers: 0

    How can I delete this discussion? I'd like to delete it or edit, deleting my enviroment url. Thanks.

  • allanallan Posts: 61,657Questions: 1Answers: 10,094 Site admin
    Answer ✓

    I've removed your url now. It wouldn't be appropriate to delete the discussion since its a question that has been answered and others might find it useful in future.

    Allan

This discussion has been closed.