当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Rails sort listing using up and down arrow

问题描述:

I want to sort my listing using up and down arrow means with every row record there will be two arrow up and down arrow .and user can sort his listing using arrow and this order should be get saved in data base .

Help me with some code if any one has already done this.

网友答案:

You will probably want to add some method of storing configurable ordering in the DB, and then implement some JS to manage requests to change the order (and maybe update the view too).

Check out the acts_as_list gem [1], which will add a position column to your orderable model and give you functionality to move items in the list.

Then look at using AJAX to manage the calls to move up/down. There's lots of tutorials out there (this one from 2009, so a little old now, but the principle is the same [2])

[1] https://github.com/swanandp/acts_as_list

[2] http://railscasts.com/episodes/147-sortable-lists

网友答案:

You need to add ajax to your arrows, which calls a move function to sub 1 from the "position column" and adds one to the one you have above. Like this:

YourModel.find_by(position: your-position-1).update(position: position+10101010) #10101010 is just a big number which I of, that your table isn't that long.
YourModel.find_by(position: your-position).update(position: your-position-1)
YourModel.find_by(position: position+10101010).update(position: your-position)

This is a simple exchange algorithm. The next step is just to update your Table with Ajax.

分享给朋友:
您可能感兴趣的文章:
随机阅读: