Sortering
Jeg kunne godt lige bruge en skarp hjerne til at gennemskue sorteringsfejlen i dette script.product er ok men id & pris sortere ikke rigtigt.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Sortering af data - client side</title>
<script language="JavaScript" type="text/javascript">
<!--
/*----------------------------------------------------------------------------
f_sort: Performs a client side sort of the contents of a HTML Table.
args: ao_table, The object reference to the table to be sorted.
ai_sortcol, The zero based column number to be sorted.
ab_header, Bool to indicate if the table have a header
row to be ignored.
vars: lastcol, used to store the last column sorted.
lastseq, used to store the sequence the last column was sorted by.
----------------------------------------------------------------------------*/
var lastcol, lastseq;
function f_sort( ao_table, ai_sortcol, ab_header )
{
var ir, ic, is, ii, id;
ir = ao_table.rows.length;
if( ir < 1 ) return;
ic = ao_table.rows[1].cells.length;
// if we have a header row, ignore the first row
if( ab_header == true ) is=1; else is=0;
// take a copy of the data to shuffle in memory
var row_data = new Array( ir );
ii=0;
for( i=is; i < ir; i++ )
{
var col_data = new Array( ic );
for( j=0; j < ic; j++ )
{
col_data[j] = ao_table.rows[i].cells[j].innerText;
}
row_data[ ii++ ] = col_data;
}
// sort the data
var bswap = false;
var row1, row2;
if( ai_sortcol != lastcol )
lastseq = 'A';
else
{
if( lastseq == 'A' ) lastseq = 'D'; else lastseq = 'A';
}
// if we have a header row we have one less row to sort
if( ab_header == true ) id=ir-1; else id=ir;
for( i=0; i < id; i++ )
{
bswap = false;
for( j=0; j < id - 1; j++ )
{
// test the current value + the next and
// swap if required.
row1 = row_data[j];
row2 = row_data[j+1];
if( lastseq == "A" )
{
if( row1[ ai_sortcol ] > row2[ ai_sortcol ] )
{
row_data[j+1] = row1;
row_data[j] = row2;
bswap=true;
}
}
else
{
if( row1[ ai_sortcol ] < row2[ ai_sortcol ] )
{
row_data[j+1] = row1;
row_data[j] = row2;
bswap=true;
}
}
}
if( bswap == false ) break;
}
// load the data back into the table
ii = is;
for( i=0; i < id; i++ )
{
row1 = row_data[ i ];
for( j=0; j < ic; j++ )
{
ao_table.rows[ii].cells[j].innerText = row1[j];
}
ii++;
}
lastcol = ai_sortcol;
}
//-->
</script>
</head>
<body>
<center>
<table NAME="A" ID="A" cellpadding="0" cellspacing="0" border="1">
<tr>
<th><a href="java script:f_sort(document.all.A, 0, true);">ID</a></th>
<th><a href="java script:f_sort(document.all.A, 1, true);">Produkt</th>
<th><a href="java script:f_sort(document.all.A, 2, true);">Pris</th>
</tr>
<tr>
<td>01</td>
<td>Bananer</td>
<td>5</td>
</tr>
<tr>
<td>02</td>
<td>Ananas</td>
<td>25</td>
</tr>
<tr>
<td>9</td>
<td>Citroner</td>
<td>10</td>
</tr>
<tr>
<td>03</td>
<td>Melon</td>
<td>07</td>
</tr>
<tr>
<td>22</td>
<td>Æbel</td>
<td>99</td>
</tr>
</table>
</body>
</html>