jQuery UI Draggable Table Rows Gotchas


I ran into a few Gotchas while dragging table rows. One was an easy fix, the other took some guesswork.

Can’t drag individual TR elements

If you set a “TR“ element to “draggable()“, it will not work because we don’t know how to handle a moving TR element (TRs must be in a table to render properly).

To fix this, wrap the TR in a table temporarily while dragging via the helper option.

                helper: function(event) {
                    return $('<div class="drag-row"><table></table></div>')

Sortable / Droppable doesn’t work on TR elements, or TR elements disappear during drag

The second problem I ran into was a little more obscure. Your droppable and sortable selectors MUST be applied to a “<tbody>“ tag.

I noticed that when I had the normal “<table><tr><td>“ structure and dragged, jQuery created a <tbody> element to contain the rows.

 $("#my_selector tbody").droppable().draggable();

5 thoughts on “jQuery UI Draggable Table Rows Gotchas

  1. Can you post a complete implementation of this fix? It’s pretty vague just by putting a one or two liner answer and just leave the reader how the rest of the implementation was made.

    1. Thanks for the comment, Francis.

      I’m not sure what to say.. the one liners are exactly that. It’s not a snippet, it’s literally the whole draggable() call..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s