How to keep 'order' column in sequence even after an edit of the order of records?

0

I have a table with n fields, one of which is the 'order' (obviously it is sequential: 1, 2, 3, n ...):

id  |  ordem  | nome
------------------------
1   |  1      | Fabio
3   |  2      | Gilmar
2   |  3      | Rodrigo
9   |  4      | Marcos  

I'm developing a drag and drop table to change this order, so how do I keep this sequence always without skipping any numbers and repeating them?

To make it difficult, there will also be manual order, that is, the user accesses the registry and selects the desired position. Therefore, all records below this should be changed.

Note: My system provides 4 parameters after dragging: id, direction (forward / back), fromPosition and toPosition that could be used in the query.

    
asked by anonymous 28.03.2014 / 17:32

4 answers

3

Ideally you would somehow know the new order of the item in question, and do 2 updates :

update TABELA set ordem = ordem + 1 where ordem > 7 (para acrescentar 1 para os itens acima da nova ordem)

update TABELA set ordem = ordem - 1 where ordem < 7 and ordem <> 0 (para decrescer 1 para os itens abaixo da nova ordem)

With that order 6 would be free ... something of the sort. You have to go more or less on this side.

    
15.04.2015 / 16:23
3

My suggestion would be that the ordem column is not sequential, but rather a field that normally increments by 10.

For example:

id  |  ordem  | nome
------------------------
1   |  10     | Fabio
3   |  20     | Gilmar
2   |  30     | Rodrigo
9   |  40     | Marcos 

To change the position, increase the target position by 5:

id  |  ordem  | nome
------------------------
1   |  10     | Fabio
3   |  25     | Gilmar
2   |  30     | Rodrigo
9   |  40     | Marcos 

Then move the home position to the position that was changed:

id  |  ordem  | nome
------------------------
1   |  10     | Fabio
9   |  20     | Marcos <-- update de ordem para 20
3   |  25     | Gilmar
2   |  30     | Rodrigo

Then just rebalance the other lines:

id  |  ordem  | nome
------------------------
1   |  10     | Fabio
9   |  20     | Marcos
3   |  30     | Gilmar
2   |  40     | Rodrigo
    
28.03.2014 / 18:14
2

I understand you're dragging names from one location to another. In this case you would only need one pair (Id, Order) Source and (Id, Order) Destination.

Following your example:

id  |  ordem  | nome
------------------------
1   |  1      | Fabio
3   |  2      | Gilmar
2   |  3      | Rodrigo
9   |  4      | Marcos 

When you drag Mark to Fabio you would do a couple of updates by changing the positions

update Tabela set Ordem = 4 where Id = 1
update Tabela set Ordem = 1 where Id = 9

Resulting in:

id  |  ordem  | nome
------------------------
1   |  4      | Fabio
3   |  2      | Gilmar
2   |  3      | Rodrigo
9   |  1      | Marcos

As BD tables do not have intrinsic order you (should always) declare the ordering.

Select Id, Ordem, Nome from Tabela order by Ordem

Your result will always be returned correctly.

EDIT

In case you want to drag a name to a position and keep others in their relative positions is a bit more complicated.

Following the example of dragging Mark into Fabio's position. You would have to set Marcos to order 1 and everyone between 1 and Marcos's old position increase from 1 (why you dragged from bottom to top). If similar way to drag Fabio to the position of Marcos you would have to set the Fabio to 4 and decrease the position of everyone under Fabio until the Marcos

    
02.04.2014 / 13:22
1

You can make a swap ... get the value fromPosition, use this value to change the value of the registry order that is in toPosition, then change the registry that was actually changed ... so you has the records simply "swapped" for each other, without losing its sequence.

    
28.03.2014 / 20:34