简体   繁体   English

jQuery在多个tbobody之间拖动(重新排序)表行

[英]jQuery dragging (reordering) table rows between multiple tbodies

I'm great with CSS but I largely suck at jQuery. 我对CSS非常满意,但在很大程度上我对jQuery很感兴趣。 We all have our limitations... 我们都有局限性...

OK with that admission out of the way I'll begin. 好的,那是我开始的那一步。 We have a "favourite products" table, which you can sectionalize into categories (eg, Booze, Sheep, Spoons). 我们有一个“收藏的产品”表,您可以将其划分为几类(例如,酒类,绵羊,勺子)。 Each category is a . 每个类别都是一个。 Each row (excluding the ) is a product. 每行(除外)都是一个产品。

I want to enable a user to drag and drop (reorder) the table rows, even between tbodies. 我想让用户甚至在tbobody之间也可以拖放(重新排序)表行。 I've looked at Table Drag and Drop (DnD) plugin for jQuery but was disappointed to find that it doesn't really support dragging between tbodies - and hasn't been updated for quite some time. 我看过jQuery的Table Drag and Drop(DnD)插件,但是很失望地发现它并不真正支持在tbody之间拖动,并且已经有一段时间没有更新了。

Here is the basic table structure: 这是基本的表结构:

<table>
  <thead>
    <tr>
      <th>Favourite</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Spoons</th> <!-- secion title, not draggable -->
    </tr>
    <tr>
      <td>Wooden Spoon</td>
    </tr>
    <tr>
      <td>Metal Spoon</td>
    </tr>
    <tr>
      <td>Plastic Spoon</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <th>Nuclear Bombs</th> <!-- seciont title, not draggable -->
    </tr>
    <tr>
      <td>US Nukes</td>
    </tr>
    <tr>
      <td>Soviet Nukes</td>
   </tr>
 </tbody>
</table>

I'm going to need to turn of the "dragability" of certain rows such as the first row in the - this being the section title. 我将需要关闭某些行的“可拖动性”,例如-中的第一行-这是本节的标题。

Is there a better version of a jquery plugin out there that can do what I need? 有没有更好的版本的jQuery插件可以满足我的需要? If you can find a better plugin please share and win my "favourite person of the week" award. 如果您可以找到更好的插件,请分享并赢得“本周最喜欢的人”奖。

Thank you. 谢谢。

Your going to want to use jQueryUi's sortable plugin and then use the option 您将要使用jQueryUi的可排序插件 ,然后使用选项

connectWith: '.connectedSortable'

to connect the different table bodies. 连接不同的表主体。

the plugin is designed for lists, but it does work on tables, mostly. 该插件专为列表而设计,但大多数情况下都适用于表。 I've used it with success on a table before. 我以前在桌子上成功使用过它。

here's the link to the documentation on jquery ui's sortable: 这是jquery ui的sortable文档的链接:

http://jqueryui.com/demos/sortable/#connect-lists http://jqueryui.com/demos/sortable/#connect-lists

EDIT: 编辑:

you'll also want to use the items option to specify that certain rows are sortable 您还需要使用items选项来指定某些行是可排序的

items: 'tr:not(.dontIncludeMe)'

Have you looked into jQuery UI's droppable widget ? 您是否研究过jQuery UI的droppable小部件 I'm wondering (since I haven't played around with your specific problem yet) if you could set your tbody as droppable and your tr as draggable. 我想知道(因为我还没有解决您的特定问题)您是否可以将tbody设置为可放置,将tr为可拖动。 Or possibly add some classes to each to be able to specify that certain rows (non-title rows) are draggable... 或者可能为每个类别添加一些类,以指定某些行(非标题行)是可拖动的...

UPDATE : more research turned up this SO question , which links to this other article about dragging table rows. 更新 :更多的研究提出这个SO问题 ,该问题链接到有关拖动表行的其他文章

you can turn off the dragability in case od table dnd plugin by specifying 您可以通过指定od表dnd插件来关闭可拖动性

<tr class ="nodrag nodrop">
  <th>Favourite</th>
</tr>

class nodrag nodrop will not allow drag and drop 类nodrag nodrop不允许拖放

and

class nodrag will not allow it to be dragged i hope it helped. 班级nodrag不允许拖动它,希望对您有所帮助。

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

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