0

I am using jQuery UI Sortable widget for implementing drag and drop functionality in different rows of a table. I wish to change the id attribute of the row dragged (say row4) to that of the row that it displaces down(say row2). I can get the id of the object being dragged from the helper object that is passed to various callback functions, but how can I get the id of the row it finally displaces ?

There are certain rows between which some rows cannot be dragged and placed, and the check has been implemented using JavaScript which uses the id of the rows(and its various td*s*) to determine it. Its, therefore, very important that the ids of rows(and, consequently, its td*s*) reflect their new position.

Daud
  • 7,429
  • 18
  • 68
  • 115

2 Answers2

1

Here are some possible approaches:

  1. If the ids are consecutive you can just loop through the rows to create the new ids on drop callback.

  2. The row displaced will either be below or above when a drop is done. Therefore you should be able to use JQuery next() or perv() to get the old id for you current 'drop' position. As for how you know if you move up or down you can take a look here.

  3. Take a look at this it seem helpful.

Community
  • 1
  • 1
solartic
  • 4,249
  • 3
  • 25
  • 26
1

You may reorder your ids after sorting like this:

$('table tr').each(function(i){
  $(this).attr('id', 'row_'+i);
});

And you may implement rows which are not draggable (or between which) using jquery ui sortable too.

ValeriiVasin
  • 8,628
  • 11
  • 58
  • 78