0

I created a question yesterday to help me find the new index of a <tr> or a <li> once it has been moved using Jquery UI drag & drop functionality, and this works perfectly :)

Something I did not think about however and have been trying to figure out this morning, is... How would I find the index of the <tr> or <li> that is being shifted up or down.

EG: Given the below table, if I Move "email" into position 1, "other" will be shifted into index of 2. How would I alert this value and its RefID ?

+--------+------------------+
| Ref ID | Issue relates to |
+--------+------------------+
|   9342 | Other            |
|   9392 | CRM              |
|   9394 | CRM              |
|   9308 | eMail            |
|   9365 | CRM              |
+--------+------------------+

Here is a FIDDLE I have been playing with.

Thanks you in advance guys.

Mike

EDIT: So, I was just moving them around a bit more, and if you move another row above the ones we just moved, "Other" will now be in index 3. Yikes!! Getting more complicated for me as I look at this thing.

Community
  • 1
  • 1
Fizor
  • 1,480
  • 1
  • 16
  • 31

3 Answers3

0

You need to use :eq() selector to target only required tr element.

var RefID = $('tr').eq(Newpos).find('td:first').html();
var RefName = $('tr').eq(Newpos).find('td:eq(1)').html();

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

You are always getting the html from the first row , regardless of which row was moved.

Since ui.item is the <tr> that was moved , you can access the td by looking within ui.item

var RefID = ui.item.find('td:first').html();

DEMO

charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

So I probably didn't phrase my question correctly, but I managed to get what I wanted by just checking the index and RefID of all the rows after moving them.

My code now looks like this:

 $(document).ready(function () {

     $('tbody').addClass("DragMe");

     $('.DragMe').sortable({
         disabled: false,
         axis: 'y',
         items: "> tr:not(:first)",
         forceHelperSize: true,
         update: function (event, ui) {
             var Newpos = ui.item.index();
             var RefID = $('tr').find('td:first').html();


             //alert("Position " + Newpos + "..... RefID: " + RefID);
             $("#GridView1 tr:has(td)").each(function () {
                 var RefID = $(this).find("td:eq(0)").html();
                 var NewPosition = $("tr").index(this);
                     alert(RefID + " " + NewPosition);

             });

         }
     }).disableSelection();
 });

And here is the working FIDDLE

Thank for your help!

Fizor
  • 1,480
  • 1
  • 16
  • 31