6

I've a multiple number of rows which is generated by a for condition, with 2 icons up and down. On click of up the selected row should move one step up, and on click of down the selected row should move one step down

<html>
<head>
<body>
<form name="myForm">

 <table id="mainTable" border="1" width="100%">

   <script>
     document.write('<tr>')
     document.write('<td>')
     document.write('row1')
     document.write('</td>')
     document.write('</tr>')

     document.write('<tr>')
     document.write('<td>')
     document.write('row2')
     document.write('</td>')
     document.write('</tr>')
     document.write('</table>')

     document.write('<table>')
     document.write('<tr>')
     document.write('<td>')
     document.write('<input type="button" value=" move UP " onClick="swapRowUp(getRowIndex(this))"</input>')>
     document.write('<input type="button" value="move DOWN" onClick="swapRowDown(getRowIndex(this))"</input>')>
     document.write('</td>')
     document.write('</tr>')
     document.write('</table>')
</script>
 </table>

</form>
</body>
</head>
</html>
<script>
var mainTable = document.getElementById("mainTable"); 
function getRowIndex(el)
{
while( (el = el.parentNode) && el.nodeName.toLowerCase() !== 'tr' );

   if( el ) 
        alert(el.rowIndex);
        return el.rowIndex;
}

function swapRowUp(chosenRow) {
 if (chosenRow.rowIndex != 0) {
   moveRow(chosenRow, chosenRow.rowIndex-1); 
 }
} 
function swapRowDown(chosenRow) {
 if (chosenRow.rowIndex != mainTable.rows.length-1) {
   moveRow(chosenRow, chosenRow.rowIndex+1); 
 }
} 

function moveRow(targetRow, newIndex) {
if (newIndex > targetRow.rowIndex) {
   newIndex++; 
 }

 var mainTable = document.getElementById('mainTable'); 

 var theCopiedRow = mainTable.insertRow(newIndex); 


 for (var i=0; i<targetRow.cells.length; i++) {
   var oldCell = targetRow.cells[i]; 
   var newCell = document.createElement("TD"); 
   newCell.innerHTML = oldCell.innerHTML; 
   theCopiedRow.appendChild(newCell); 
   copyChildNodeValues(targetRow.cells[i], newCell);
 } 
//delete the old row 
 mainTable.deleteRow(targetRow.rowIndex); 
} 


function copyChildNodeValues(sourceNode, targetNode) {
 for (var i=0; i < sourceNode.childNodes.length; i++) {
   try{
     targetNode.childNodes[i].value = sourceNode.childNodes[i].value;
   }
   catch(e){

   }
 }
}

</script>

i'm unable to perform the swap operation, i get cellIndex as null

3
  • 2
    So, what's the question? Commented Jan 14, 2012 at 11:48
  • 2
    Deleting your question after it got downvoted (stackoverflow.com/questions/8861893/…) and asking the same question really isn't the way to go. As I said before, show us what you've tried so far - "please write all my code for me" questions tend to get frowned on here =) Commented Jan 14, 2012 at 15:02
  • i've posted the sample i tried out, can u help me out, Commented Jan 14, 2012 at 17:32

1 Answer 1

14

You can use some sensible JavaScript

up.addEventListener("click", function moveRowUp() {
    var row = this.parentNode.parentNode,
        sibling = row.previousElementSibling,
        parent = row.parentNode;

    parent.insertBefore(row, sibling);
});

for some sensible HTML

<table>
    <tbody>
        <tr> 
            <td> 1 </td> 
            <td> filler </td> 
        </tr>
        <tr> 
            <td> 2 </td> 
            <td> <button id="up">up</button> </td> 
        </tr>
    </tbody>
</table>

Live Example

Sign up to request clarification or add additional context in comments.

5 Comments

Are two replaceChild operations faster than one insertBefore operation..?
The third argument of addEventListener is not optional in some less-recent-but-not-ancient browsers.
@RobW two replaceChild operations don't actually work. I like your single insertBefore that's very elegant.
@ThiefMaster Meh legacy browser compliance, if your going to do it, do it properly, also handle emulating DOM2 events in IE, and handle previousElementSibling. The only browser you should care about where addEventListener fails without the third argument is FF3.6
@ClarkeyBoy no, don't clone the dom node, that's a waste of computation :\. Besides I already fixed it so it uses a single insertBefore

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.