Forum Moderators: open

Message Too Old, No Replies

Moving Cells up/down

moving table cells up and down with javascript

         

danspeca

3:03 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



I'm trying to find a script that will move cells in a specific column of a data set up or down (user clicks a gif arrow)...the key is to keep the rest of the row intact...

the user is tasked with reordering a list, and then submitting that reordered list to a database...

assuming everything here will need to happen in an array (or two!)...

a caveat - there is a column of numbers (numbering the list) that need to remain intact...so to reiterate, i only need elements in one column to move up/down, not the entire row!

thanks...

john_k

3:17 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah - the Friday javascript challenge.

<html>
<head>
<script language="javascript">
<!--
function swapCells(idA,idB)
{
var cellA=document.getElementById('color'+idA);
var cellB=document.getElementById('color'+idB);
if(cellA&&cellB)
{
var temp=cellA.innerHTML;
cellA.innerHTML=cellB.innerHTML;
cellB.innerHTML=temp;
}
}
//-->
</script>
</head>
<body>
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<td>1</td>
<td id="color1">blue</td>
<td>&nbsp;</td>
<td><input type="button" value="Down" onClick="swapCells(1,2);"></td>
</tr>
<tr>
<td>1</td>
<td id="color2">orange</td>
<td><input type="button" value="Up" onClick="swapCells(2,1);"></td>
<td><input type="button" value="Down" onClick="swapCells(2,3);"></td>
</tr>
<tr>
<td>1</td>
<td id="color3">green</td>
<td><input type="button" value="Up" onClick="swapCells(3,2);"></td>
<td><input type="button" value="Down" onClick="swapCells(3,4);"></td>
</tr>
<tr>
<td>1</td>
<td id="color4">violet</td>
<td><input type="button" value="Up" onClick="swapCells(4,3);"></td>
<td><input type="button" value="Down" onClick="swapCells(4,5);"></td>
</tr>
<tr>
<td>1</td>
<td id="color5">red</td>
<td><input type="button" value="Up" onClick="swapCells(5,4);"></td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>

danspeca

3:55 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



great! not as flexible as i would like, but it will work in a pinch!

john_k

4:18 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Best I could do while the boss wasn't looking :)

By using innerHTML, you can include hidden fields that will move with the text.

danspeca

5:00 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



yep - using hidden fields is what we've done to pass this info to the database...

thanks again for the quick turnaround

dcrombie

2:43 pm on Jun 19, 2004 (gmt 0)



Nothing like a challenge. This seems to work in everything that I can get my hands on except IE(Mac) which hasn't implemented insertBefore according to the standards. Anyone want to try in IE(Win) and report back? ;)

<SCRIPT type="text/javascript"> 
function moveRowUp(tableid, n)
{
var tablebody = document.getElementById(tableid);
if(tablebody.nodeName!= "TBODY") return false;
tablerows = document.getElementsByTagName("TR");
if(n > 0 && n < tablerows.length) {
var tmp = tablerows[n-1].cloneNode(true);
tablebody.removeChild(tablerows[n-1]);
tablebody.insertBefore(tmp, tablerows[n]);
}
}
</SCRIPT>

<TABLE> 
<TBODY id="data">
<TR bgcolor="red"><TD>1</TD><TD>red</TD></TR>
<TR bgcolor="orange"><TD>2</TD><TD>orange</TD></TR>
<TR bgcolor="yellow"><TD>3</TD><TD>yellow</td></TR>
<TR bgcolor="green"><TD>4</TD><TD>green</td></TR>
<TR bgcolor="blue"><TD>5</TD><TD>blue</td></TR>
</TBODY>
</TABLE>

<FORM action="#"> 
Row # (1 - 4) <INPUT type="text" size="2" name="row">
<INPUT type="button" value="move row up" onClick="moveRowUp('data', row.value);">
</FORM>

this one moves the row along with everything in it

[edited by: dcrombie at 3:17 pm (utc) on June 19, 2004]

Bernard Marx

3:13 pm on Jun 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IE5 Win
Possible bug report: (sorry)

1. enter 3
2. click --> rows 3 & 4 swap (as expected)
3. click again --> rows 3 & 4 swap back ( expected something different)

Does the number entered represent the current (0-based) row index, or the number in the first column (+1)?

dcrombie

3:21 pm on Jun 19, 2004 (gmt 0)



That's what's meant to happen - row[4] becomes row[3] and vice-versa. The problem I found was when you try to move up the last row in IE(Mac) - it doesn't understand that insertBefore should appendChild instead if the second argument is null (at least I think that's the problem). The table then just loses a row.

Bernard Marx

4:09 pm on Jun 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..if the second argument is null

LONG SHOT: Perhaps it gets confused if it receives null (which it will if n is out-of-bounds for the collection). Maybe feeding it undefined instead.

insertBefore could be switched to appendChild if n == [row collection].length. This would involve a little more work.

Anyway..

I reckoned that the process:

[blue]clone > remove > insert clone[/blue]
isn't necessary. insertBefore (like appendChild) should move an element if it's already part of the doc.

New code:

<SCRIPT type="text/javascript">
function moveRowUp(tableid, n)
{
var tablebody = document.getElementById(tableid);
if(tablebody.nodeName!= "TBODY")
return false;
tablerows = document.getElementsByTagName("TR");
if(n > 0 && n < tablerows.length)
{
tablebody.insertBefore(tablerows[n], tablerows[n-1]);
}
}
</SCRIPT>

I think this has cured the last row problem too.

dcrombie

11:14 am on Jun 20, 2004 (gmt 0)



Thanks. I thought so too but couldn't get it working yesterday - must have had a typo. Works great now of course...

Now to try and build something _useful_ out of it ;)

Bernard Marx

1:35 pm on Jun 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Useful? Interesting concept. I'll have to try it sometime.