Br*************@Singularity.co.uk wrote:
Hi
I have a javascript problem that has been annoying me for two days now
and thought that a javascript expert might have the magic solution.
I am populating a table dynamically from the database and I am trying
to allow the user to order the rows in the table using up and down
arrows.
The function I am using is:
function moveContactRow(direction)
{
var tempVal; //stores temporary value while swapping hidden values
rowIndex =
parseInt(window.event.srcElement.parentElement.par entElement.index);
if((rowIndex == 0 && direction > 0) || (rowIndex ==
id_listTable.rows.length - 1 && direction < 0) || (rowIndex > 0 &&
rowIndex < id_listTable.rows.length - 1))
{
if(id_listTable.rows[rowIndex].cells[0].swapNode)
{
id_listTable.rows[rowIndex +
direction].cells[0].swapNode(id_listTable.rows[rowIndex].cells[0]);
id_listTable.rows[rowIndex +
direction].cells[1].swapNode(id_listTable.rows[rowIndex].cells[1]);
id_listTable.rows[rowIndex +
direction].cells[2].swapNode(id_listTable.rows[rowIndex].cells[2]);
tempVal = document.all('hidID' + rowIndex).value;
document.all('hidID' + rowIndex).value = document.all('hidID' +
[rowIndex + direction]).value;
document.all('hidID' + [rowIndex + direction]).value = tempVal;
document.all('hid' + rowIndex).value = rowIndex + direction;
document.all('hid' + [rowIndex + direction]).value = rowIndex;
}
}
}
To call the function the arrows have the following code:
upArrow - onclick="moveContactRow(-1)
DownArrow - onclick="moveContactRow(1)
The good thing is that the table rows move correctly, but the values
written to the hidden fields (prefixed with hid or hidID) aren't always
correct.
Any ideas on whats happening?
Any help is greatly appreciated.
Thanks
Brendan
Hi Brendan,
I wouldn't have used swapNodes to handle the re-ordering, instead I
would have an array that represents the table and then used the "sort"
function of array to re index the array and then redraw the table from
the array.
Consider this javascript
:
------------------------ JAVASCRIPT ---------------------------
// An Array of data.
var aTable = {
{"1", "First Name", "First Address", "First Phone" },
{"2", "Second Name", "Second Address", "Second Phone" },
{"3", "Third Name", "Third Address", "Third Phone" }};
// A var to define which column is ordered
var nCurrentColumn = 0;
// A function the wrap the sorting and rendering;
function OrderCol( nCol ){
// @nCol defines what index the Array is to be index on.
nCurrentColumn = nCol;
aTable.sort(sortTable);
RenderTable();
}
// The Function to the array.
function sortTable (a,b){
if (a[nCurrentColumn]<b[nCurrentColumn]) return -1
if (a[nCurrentColumn]>b[nCurrentColumn]) return 1
return 0;
}
// A function to step through the array to render a table
function RenderTable(){
eTableBody = document.getElementById("tableBody");
eTableBody.innerHTML="";
for( var i=0;i<aTable.length;i++ ){
var eRow = document.createElement( "tr" );
for( var j=0; j<aTable[i].length;j++ ){
var eCell = document.createElement( "td" );
eCell.appendChild( document.createTextNode( aTable[i][j] ) );
eRow.appendChild(eCell);
}
eTableBody.appendChild( eRow );
}
}
// Using the onload event to start the table rendering
window.onload = function(){ OrderCol(0); }
---------------------------------------------------------
And this HTML :
------------------------ HTML ---------------------------
<table>
<thead>
<tr>
<td onclick="OrderCol(0);">KEY</td>
<td onclick="OrderCol(1);">Name</td>
<td onclick="OrderCol(2);">Address</td>
<td onclick="OrderCol(3);">Phone</td>
</tr>
</thead>
<tbody id="tableBody"></tbody>
</table>
---------------------------------------------------------
You will probably want to handle the control of direction so if a column
has been clicked twice for indexing you invert the direction of the
ordering. All you need to do to achive this is to have an indicator set
either to 1 or -1 and then in the sortTable function return the value
multiplied by this indicator.
For Example
if (a[nCurrentColumn]<b[nCurrentColumn]) return ( -1 * nDirection );
if (a[nCurrentColumn]>b[nCurrentColumn]) return ( 1 * nDirection );
return 0;
Hope this helps
Andy
----------------------------------------------------------------------
http://km0ti0n.blunted.co.uk/blog http://km0ti0n.blunted.co.uk/mozXPath http://km0ti0n.blunted.co.uk
----------------------------------------------------------------------