开发者

Rearrangeable ordered list with Ruby on Rails

I want to implement a kind of favorites list. Lets take a play list for this example:

Song A
Song D
Song B
Song C

The order of songs should be saved and the user should be able to rearrange this o开发者_如何学编程rder and insert new elements at an arbitrary point in the list.

My idea is to use a field "position" and reset it for all elements of the list, when the list is saved. I think that is what Javas Hibernate does.

Another idea is to use position numbers with a big difference, say 1000, so that there is still room for modifications:

Song A 1000
Song D 2000
Song B 3000
Song C 4000

Song C will be inserted between A and D with position number 1500:

Song A 1000
Song C 1500 
Song D 2000
Song B 3000

Any better ideas?


Why don't use acts_as_list?


If you want to roll your own, the answers to this question might be helpful:

Best way to save a ordered List to the Database while keeping the ordering

Seems like the consensus is to just use an order field and re-order them all on save. You can mitigate the amount of database work if you allow the user to do the reordering interactively client side, and just send the final list to save back to the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜