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.
$(".tr_row").draggable({
helper: function(event) {
return $('<div class="drag-row"><table></table></div>')
.find('table').append($(event.target).closest('tr').clone()).end();
},
});
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();
Thank you for this topic, that’s really helped me
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.
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..
Is this still considered an issue? I’ve been working with this a while (mostly in Chrome and Windows 7), and I haven’t seen any of these issues.
I was able to easily fix the first issue you had by adding: appendTo:”body”