使用 jQuery 和 Rails 3 实现 ajax 可排序列表
有很多类似的问题,但我找不到一个完全满足我的需求。
您可能会认为这是一个非常常见的问题,应该有一个完善的解决方案。
我想使用 ajax 重新排序列表并将每个项目的位置保存到数据库中,非常类似于 Basecamp 列表中的待办事项。虽然能够将嵌套列表中的项目从一个级别移动到另一个级别会很好,但我根本不需要该功能。
我正在使用 Rails 3.1、jQuery。将解决方案与可排序 jQuery 插件集成是有意义的,但我对任何解决方案持开放态度。
如果您不知道任何现成的解决方案,您能给我指导如何去做吗?
我的应用程序曾经使用acts_as_category插件,但它没有得到维护,我已经手动实现了所有其他树功能。
There are many similar questions but I can't find one that exactly meets my needs.
You'd think this is a very common problem and there should be a polished gem solutions out there.
I want to reorder list and save position of each item to the database with ajax very much like the to-do items in a list in Basecamp. Although it would be nice to be able to move items from one level to another in nested lists I don't really need that feature at all.
I'm using Rails 3.1, jQuery. It would make sense to integrate the solution with the sortable jQuery plugin but I'm open to any solutions.
If you don't know of any ready solutions, can you give me pointers on how to go about it.
My apps used to use the acts_as_category plugin but it's not being maintained and I have implemented all the other tree features manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一篇不错的博客文章:
是
webtempest.com/sortable-list-in-ruby-on-rails-3-almost-unobtrusive-jquery/
仍然显示在 http://web.archive.org/web/20120315004343/http://webtempest.com/sortable-list-in-ruby-on-rails-3-almost-unobtrusive-jquery
本质上:
这些看起来都相当强大,并且我能够实现概述的基本功能的变体,并在上创建新项目相同的屏幕和一些CSS 3 个附加功能(只需适当设置
.your-class.ui-sortable-helper
样式即可),无需大惊小怪。我还没有跨浏览器进行广泛的测试,但在 WebKit 和 Firefox 中似乎很满意。博客上的示例并没有真正使用acts_as_list – 它只是使用jQuery序列化对象ID,然后直接在控制器中迭代它们 – 但我想如果您需要自动进行更改,那么在后端使用这些函数会很有用由于某种原因。
博客文章中的关键代码:
Javascript:
视图:
这包括一个像
book_5
这样的 id,它允许$('#books').sortable('serialize')
Javascript 创建 Rails 可以解析的查询参数。控制器:
这可能不合适,具体取决于模型的作用域/访问控制方式。在我自己的解决方案中,我迭代了 params['book'],并包含一些检查/错误处理以确保只接受有意义的值。
(PS,这与 Ryan Bates 在他的付费墙视频广播中针对同一主题提供的方法非常相似。)
(PPS 我知道这是一个老问题,但是,正如经常发生的那样通过 StackOverflow,Google 将我带到了这里,所以我想我应该记录一下我所做的事情。)
There is a decent blog post on this here:
was
webtempest.com/sortable-list-in-ruby-on-rails-3-almost-unobtrusive-jquery/
Still shows at http://web.archive.org/web/20120315004343/http://webtempest.com/sortable-list-in-ruby-on-rails-3-almost-unobtrusive-jquery
Essentially:
These both seem reasonably robust, and I was able to implement a variation on the basic functionality outlined, with new item creation on the same screen and some CSS 3 bells and whistles (just style the
.your-class.ui-sortable-helper
appropriately) without much fuss. I haven't tested extensively across browsers, but it seems happy in WebKit and Firefox.The example on the blog doesn't really use acts_as_list much – it just serialises the object IDs using jQuery and then iterates over them in the controller directly – but I guess it's useful having those functions on the back end if you need to automate changes from there for some reason.
Key code from the blog post:
Javascript:
View:
This includes an id like
book_5
, which allows$('#books').sortable('serialize')
in the Javascript to create a query parameter which Rails can parse.Controller:
This might not be appropriate, depending on how your model is scoped / access-controlled. In my own solution I iterated over the
params['book']
instead, and included some checking/error-handling to ensure only meaningful values would be accepted.(P.S. this is quite similar to the approach Ryan Bates gives in his, paywalled, videocast on the same topic.)
(P.P.S. I am aware this is an old question, but, as so often with StackOverflow, Google got me here so I thought I would document what I did.)