Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add new row in move table row script

Status
Not open for further replies.

electricphp

Programmer
Feb 20, 2008
71
US
I have this script that allows me to move a table row up and down in a table.

Code:
var activeRow = 0;

function setActiveRow(el) {
  var rows = document.getElementById('movingTable').rows;
  for(var i = 0; i < rows.length; i++) {
    if(rows[i] == el) {activeRow = i;} 
  }
}

function moveActiveRow() {

// passing value of droptown spscifying how many rows up or down the row needs to move (-2, or +3 for example)  

 var el=document.getElementById('testsl');
  var movenew = activeRow + parseInt(el.options[el.selectedIndex].value);

// perform the actual movement

  var rows = document.getElementById('movingTable').rows;
  var oldRow = rows[activeRow].innerHTML;
  var newRow = rows[movenew].innerHTML;
  rows[activeRow].innerHTML = newRow;
  rows[movenew].innerHTML = oldRow;
  setActiveRow(rows[movenew]);
}

The problem is that if the script moves the row 2 rows up so say from index 6 to index 4, it takes the row in index 4 and moves it to index 6. so in essence it's swapping the 2 rows. I need the rest of the table to stay the same, so no swapping to occur.

So I guess the row initially in index 4 should be bumped to index 5, and the one in index 5 should be bumped to index 6, and everything else should stay the same.

I also need this to work backwards, so if I'm moving a row from index 7 to index 9, index 8 should move to index 7, and index 9 should move to index 8, then obviously index 7 will replace index 9 and everything else stays the same.


Please help
 
See my penultimate post in thread216-1574388 - specifically the "insertBefore" command. This should be all you need to use, as all browsers will move DOM nodes that already exist.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top