Archive for the ‘swap table rows’ tag
Moving table rows up and down with JavaScript
While there a lot complicated javascript code snippets to move table rows up and down, this one is very easy provided you can use the below functions fluently.
- IF….ELSE conditional statements
- User made javascript function with arguments
- HTML table row Index property
- document.getElementsByTagName()
- cloneNode()
- replaceChild()
If you prefer not to use DOM manupulation, then you can try the simpler method given at the end of this post, but please read it limitation.
Lets start:
The table layout and markup:
The table layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <table border=”1”> <tr> <td>1. </td> <td> Description of item 1 </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’up’)”>Move up</a> </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’down’)”>Move down</a> </td> </tr> <tr> <td>2. </td> <td> Description of item 2 </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’up’)”>Move up</a> </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’down’)”>Move down</a> </td> </tr> <tr> <td>3. </td> <td> Description of item 3 </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’up’)”>Move up</a> </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’down’)”>Move down</a> </td> </tr> <tr> <td>4. </td> <td> Description of item 4 </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’up’)”>Move up</a> </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’down’)”>Move down</a> </td> </tr> <tr> <td>5. </td> <td> Description of item 5 </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’up’)”>Move up</a> </td> <td> <a href=”javascript: void(0)” onclick=”move(this,’down’)”>Move down</a> </td> </tr> </table> |
The user-made javascript function:
We use the user-made function named move(), with two arguments. The first argument is supposed to help us recognize the row which we want to move either up or down. The second arguments is supposed to let us know the direction in which the row should be moved: Either up or down.
The javascript code:
The logic of the code is explained as comments. So please read them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | function move(elem,direction) { //since we have to deal with two rows: the row that should be moved and the other row which is adjacent to it, we have to first establish references to each of them. We establish these references using the rowIndex property. //lets get the row index on the row that is to be moved(the one on which the user clicked) // a td tr clickedRowIndex=elem.parentNode.parentNode.rowIndex; //This row could either be the first row or the last row or some inbetween row. //The first row cannot be moved upwards. So if the user tries to move the first row upwards, we will have to tell him that it can’t be done. if(clickedRowIndex ==”0” && direction==”up”) { alert(“This is the uppermost row and connot be moved upwards”); return false; } //Similarly, if the user wants to move the last row downwards, we again tell him that it isn’t possible //For this, this we will have to first find how many rows are there in the table, then compare if the rowIndex(rindex) is same as that of last row maxrindex= (elem.parentNode.parentNode.parentNode.getElementsByTagName(“tr”).length)-1; if(clickedRowIndex ==maxrindex && direction==”down”) { alert(“This is the bottommost row and cannot be moved downwards.”); return false; } //for easy reference to the parent elements, lets setup reference to the parent HTML table.(table tag) parentTable=elem.parentNode.parentNode.parentNode; //establish reference to the row which is supposed to be moved(the one on which the user clicked) clickedrow=parentTable.getElementsByTagName(“tr”)[clickedRowIndex]; //establish reference to adjacent row. //If the direction is ‘up’, the adjacent row’s rowIndex will be 1 less that that of ‘clickedrow’ // If the direction is ‘down’, the adjacent row’s rowIndex will be 1 more that that of ‘clickedrow’ if(direction==”up”) { adjacentRowIndex= clickedRowIndex –1; } if(direction==”down”) { adjacentRowIndex= clickedRowIndex +1; } adjacentrow= parentTable.getElementsByTagName(“tr”)[adjacentRowIndex]; //Once that we have established references to both the rows that should change their position, we should clone each of them clickedrow_clone=clickedrow.cloneNode(true); adjacentrow_clone=clickedrow.cloneNode(true); //both the cloned nodes remain ‘invisible’ to the user. //now replace the nodes. //The below replaceChild() function will replace the adjacentrow with ‘clone of the clicked row’ and then remove the clone on the clicked row. adjacentrow=parentTable.replaceChild(clickedrow_cloned,adjacentrow); //similar replacement as above clickedrow=parentTable.replaceChild(adjacentrow_cloned,clickedrow); // the clones of two rows that we made above were automatically removed by the replaceChild() function. } |
Alternate much easier way, but with limitations
If you are not comfortable with DOM, or simplt wouldn’t like to use it, you can also move rows by swapping their innerHTML. However, its limitation is that it cannot preserve the state of input elements. Eg: If you check a checkbox and then move it upwards, the checkbox would be checked while moving it.