electricphp
Programmer
I have this script that allows me to move a table row up and down in a table.
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
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