简体   繁体   English

允许用户拖放表行以更改其顺序的最简单方法是什么?

[英]What is the simplest way to allow a user to drag and drop table rows in order to change their order?

I have a Ruby on Rails application that I'm writing where a user has the option to edit an invoice. 我有一个Ruby on Rails应用程序,我正在编写一个用户可以选择编辑发票的应用程序。 They need to be able to reassign the order of the rows. 他们需要能够重新分配行的顺序。 Right now I have an index column in the db which is used as the default sort mechanism. 现在我在db中有一个索引列,用作默认排序机制。 I just exposed that and allowed the user to edit it. 我刚刚曝光并允许用户编辑它。

This is not very elegant. 这不是很优雅。 I'd like the user to be able to drag and drop table rows. 我希望用户能够拖放表格行。 I've used Scriptaculous and Prototype a bit and am familiar with them. 我已经使用了Scriptaculous和Prototype,并熟悉它们。 I've done drag and drop lists, but haven't done table rows quite like this. 我已经完成了拖放列表,但没有像这样完成表行。 Anyone have any suggestions for not only reordering but capturing the reorder efficiently? 任何人都有任何建议,不仅要重新排序,还要有效地捕获重新订购?

Also, the user can dynamically create a new row in JS right now, so that row has to be reorderable as well. 此外,用户现在可以在JS中动态创建新行,因此该行也必须可重新排序。

Bonus points if it can be done with RJS instead of direct JavaScript. 如果可以使用RJS而不是直接JavaScript来完成加分。

I've used the Yahoo User Interface library to do this before: 我之前使用过Yahoo User Interface库来执行此操作:

http://developer.yahoo.com/yui/dragdrop/ http://developer.yahoo.com/yui/dragdrop/

MooTools sortables are actually better than script.aculo.us because they are dynamic; MooTools sortables实际上比script.aculo.us更好,因为它们是动态的; MooTools allows the addition/removal of items to the list. MooTools允许在列表中添加/删除项目。 When a new item is added to a script.aculo.us sortable, you have to destroy/recreate the sortable to make the new item sortable. 将新项目添加到script.aculo.us sortable时,您必须销毁/重新创建可排序项以使新项目可排序。 There'll be a lot overhead in doing so if the list has many elements. 如果列表包含许多元素,那么这样做会有很多开销。 I had to switch from script.aculo.us to the more lightweight MooTools just because of this limitation and ended up being extremely happy with that decision. 由于这种限制,我不得不从script.aculo.us切换到更轻量级的MooTools,最终对这个决定非常满意。

The MooTools way of making a newly added item sortable is just: MooTools使新添加的项目可排序的方法就是:

sortables.addItems(node);

Okay, I did some more scouring and figured out something that seems to mostly be working. 好吧,我做了一些淘洗,并想出了一些似乎主要起作用的东西。

edit.html.erb: edit.html.erb:

...
<table id="invoices">
   <thead>
     <tr>
      <th>Id</th>
      <th>Description</th>
     </tr>
   </thead>
   <tbody id="line_items">
      <%= render :partial => 'invoice_line_item', :collection => @invoice.invoice_line_items.sort %>
   </tbody>
</table>

<%= sortable_element('line_items', {:url => {:action => :update_index}, :tag => :tr, :constraint => :vertical}) -%>
...

app/controllers/invoices.rb 应用程序/控制器/ invoices.rb

...
def update_index
  params["line_items"].each_with_index do |id, index|
    InvoiceLineItem.update(id, :index => index)
  end
  render :nothing => true
end
...

The important part is :tag => :tr in "sortable_element" and params["line_items"] -- this gives the new list of ids and is triggered on the drop. 重要的部分是:“sortable_element”中的tag =>:tr和params [“line_items”] - 这给出了新的id列表并在drop上触发。

Detriments: Makes the AJAX call on drop, I think I'd prefer to store the order and update when the user hits "save". 损害:让AJAX调用掉线,我想我更喜欢存储订单并在用户点击“保存”时更新。 Untested on IE. 在IE上未经测试。

I like jQuery http://docs.jquery.com/UI/Sortables 我喜欢jQuery http://docs.jquery.com/UI/Sortables

$("#myList").sortable({}); $( “#myList中”)排序({})。

You will need to write some code to persist it but it isn't that tough. 你需要编写一些代码来坚持它,但它并不那么难。

雅虎的界面比我想象的要容易,在不到四个小时的时间内做了一些时髦的工作。

Scriptaculous的sortables好像去,因为它内置的方式。 http://github.com/madrobby/scriptaculous/wikis/sortable

With Prototype and Scriptaculous : 使用PrototypeScriptaculous

Sortable.create('yourTable', {
    tag: 'tr',
    handles: $$('a.move'),
    onUpdate: function() {
        console.log('onUpdate');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM