在不点击每个条目的情况下更新事物列表

2022-01-17 00:00:00 sql-update sql mysql

我在数据库中有一个用户应该能够订购的列表.

I have a list in a database that the user should be able to order.

itemname|  order value (int)
--------+---------------------         
salad   |  1
mango   |  2
orange  |  3
apples  |  4

从数据库加载时,我只是按 order_value 排序.

On load from the database, I simply order by order_value.

通过拖放,他应该能够移动apples,使其出现在列表顶部..

By drag 'n drop, he should be able to move apples so that it appears at the top of the list..

itemname|  order value (int)
--------+---------------------         
apples  |  4
salad   |  1
mango   |  2
orange  |  3

好的.所以现在在内部我必须更新每个列表项!如果列表有 20 或 100 个项目,那么对于简单的拖动操作来说就需要很多更新.

Ok. So now internally I have to update EVERY LIST ITEM! If the list has 20 or 100 items, that's a lot of updates for a simple drag operation.

itemname|  order value (int)
--------+---------------------         
apples  |  1
salad   |  2
mango   |  3
orange  |  4

我宁愿只更新一次.我想到的一种方法是内部订单"是否是 double 值.

I'd rather do it with only one update. One way I thought of is if "internal Order" is a double value.

itemname|  order value (double)
--------+---------------------         
salad   |  1.0
mango   |  2.0
orange  |  3.0
apples  |  4.0

所以在拖放操作之后,我指定 apples 的值小于它要出现在前面的项目:

SO after the drag n' drop operation, I assign apples has a value that is less than the item it is to appear in front of:

itemname|  order value (double)
--------+---------------------         
apples  |  0.5
salad   |  1.0
mango   |  2.0
orange  |  3.0

.. 如果一个项目被拖到中间某处,它的 order_value 比它之后出现的那个大.. 这里我将 orange 移动到 <代码>沙拉和芒果:

.. and if an item is dragged into the middle somewhere, its order_value is bigger than the one it appears after .. here I moved orange to be between salad and mango:

itemname|  order value (double)
--------+---------------------         
apples  |  0.5
salad   |  1.0
orange  |  1.5
mango   |  2.0

对更好的方法有什么想法吗?

Any thoughts on better ways to do this?

推荐答案

我最终使用了一个 邻接表.我当时并不知道.

I ended up using an adjacencies table. I didn't know about it at the time.

相关文章