最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery dragging (reordering) table rows between multiple tbodies - Stack Overflow

programmeradmin2浏览0评论

I'm great with CSS but I largely suck at 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. 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.

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? If you can find a better plugin please share and win my "favourite person of the week" award.

Thank you.

I'm great with CSS but I largely suck at 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. 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.

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? If you can find a better plugin please share and win my "favourite person of the week" award.

Thank you.

Share Improve this question asked Jul 26, 2010 at 16:16 Adam CAdam C 3,9112 gold badges23 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Your going to want to use jQueryUi's sortable plugin and then use the option

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:

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

EDIT:

you'll also want to use the items option to specify that certain rows are sortable

items: 'tr:not(.dontIncludeMe)'

Have you looked into jQuery UI's droppable widget? 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. 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.

you can turn off the dragability in case od table dnd plugin by specifying

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

class nodrag nodrop will not allow drag and drop

and

class nodrag will not allow it to be dragged i hope it helped.

发布评论

评论列表(0)

  1. 暂无评论