简体   繁体   English

如何拖放表格?

[英]How to Drag & Drop Table?

I followed this Ryan Bates tutorial . 我遵循了Ryan Bates教程

Awesome. 真棒。

I got it to work with lists, but I've been trying to make it work with tables, more specifically the tables rendered in a partial. 我可以将其与列表一起使用,但是我一直在尝试使其与表一起使用,更具体地说,是与部分表一起显示的表。

<table id="habits" data-update-url="<%= sort_habits_url %>">
  <% @habits.each do |habit| %>
    <tbody id="habit_<%= habit.id %>">
      # 2 tr's & 7 td's
    </tbody>
  <% end %>
</table>

With the above version the iteration multiples the tables because I am rendering this partial from the home page with <% rendered @habits %> so I shouldn't have to include the line <% @habits.each do |habit| %> 在上面的版本中,迭代是表格的倍数,因为我是使用<% rendered @habits %>从主页上渲染此部分的,所以我不必在行中添加<% @habits.each do |habit| %> <% @habits.each do |habit| %> . <% @habits.each do |habit| %>

So I tried this: 所以我尝试了这个:

<table id="habits" data-update-url="<%= sort_habits_url %>">
  <tbody id="habit_<%= habit.id %>">
    # 2 tr's & 7 td's
  </tbody>
</table>

But then only the first table can be dragged. 但是,只有第一个表可以拖动。 With the first example code only the first iteration of tables can be dragged and dropped. 使用第一个示例代码,只能拖放表的第一次迭代。

habit-sort.js.coffee habit-sort.js.coffee

jQuery ->
  $('#habits').sortable(
    axis: 'y'
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize'))
  );

If are trying to handle multiple DOMs, you cannot use the DOM ID. 如果要处理多个DOM,则不能使用DOM ID。 So you'll need to replace the id by class. 因此,您需要按类别替换ID。

Working with multiple tbodys and sorting TRs inside each tbody: http://jsfiddle.net/aron_aron/1jjhdtj2/2/ 处理多个图元并在每个图元中对TR进行排序: http : //jsfiddle.net/aron_aron/1jjhdtj2/2/

Also, if you bind <table id="habits" , the sortable content will be the entire tbody html content. 另外,如果您绑定<table id="habits" ,则可排序内容将是整个tbody html内容。

Update 更新

Let me try to do a simple explanation on how sortable works. 让我尝试对可排序的工作方式做一个简单的解释。

When you call $( "#item" ).sortable() , you will be able to sort(change the position) of every children in the root of the #item. 当您调用$( "#item" ).sortable() ,您将能够对#item根目录中的每个子级进行排序(更改位置)。

What does it mean? 这是什么意思?

If you have the following html: 如果您具有以下html:

<div class="sortable_tables">
    <table class="sortable_bodys">
        <tbody class="sortable_trs">
          <tr class="sortable_tds">
              <td>ID 1</td>
              <td>Item Name 1</td>
              <td>Action</td>
          </tr>
          <tr>

So you can call the .sortable() at any level, depending on the expected behavior. 因此,您可以根据期望的行为在任何级别调用.sortable()

For example, if you want to sort a entire tables, just call $( ".sortable_tables" ).sortable() , so you will be able to change the position of any html element created inside <div class="sortable_tables"> 例如,如果要对整个表进行排序,只需调用$( ".sortable_tables" ).sortable() ,这样便可以更改在<div class="sortable_tables">内创建的任何html元素的位置。

But let's say that you want to sort the rows in the table. 但是,假设您要对表中的行进行排序。 So, to sort TRs, you need to call the parent html .select() . 因此,要对TR进行排序,您需要调用父html .select() TR's parent is a tbody, so $( ".sortable_trs" ).sortable() you will be able to sort TRs inside each tbody with the sortable_trs class TR的父级是一个tbody,因此$( ".sortable_trs" ).sortable()您将能够使用sortable_trs类对每个tbody中的TR进行sortable_trs

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

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