Documente Academic
Documente Profesional
Documente Cultură
0 strict//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
<!--************************************************************************-->
<!--* table sort demo *-->
<!--* *-->
<!--* copyright 2002 by mike hall *-->
<!--* please see http://www.brainjar.com for terms of use. *-->
<!--************************************************************************-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>brainjar.com: table sort demo</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link href="/common/default.css" rel="stylesheet" type="text/css" />
<style type="text/css">
/* table styles. */
table {
border-color: #000000;
border-spacing: 0px;
border-style: solid;
border-width: 2px;
cell-spacing: 0px;
}
td, th {
font-family: arial, helvetica, sans-serif;
font-size: 10pt;
padding: 2px 0.5em;
white-space: nowrap;
}
td.numeric {
text-align: right;
}
th {
background-color: #c0c0c0;
}
th.mainheader {
background-color: #808080;
color: #ffffff;
text-align: left;
}
th a {
color: #000080;
text-decoration: none;
}
th a:visited {
color: #000080;
}
th a:active, th a:hover {
color: #800000;
text-decoration: underline;
}
tr.alternaterow {
background-color: #e0e0e0;
}
td.sortedcolumn {
background-color: #f0f0f0;
}
th.sortedcolumn {
background-color: #b0b0b0;
}
tr.alternaterow td.sortedcolumn {
background-color: #d0d0d0;
}
</style>
<script type="text/javascript">//<![cdata[
//-----------------------------------------------------------------------------
// sorttable(id, col, rev)
//
// id - id of the table, tbody, thead or tfoot element to be sorted.
// col - index of the column to sort, 0 = first column, 1 = second column,
// etc.
// rev - if true, the column is sorted in reverse (descending) order
// initially.
//
// note: the team name column (index 1) is used as a secondary sort column and
// always sorted in ascending order.
//-----------------------------------------------------------------------------
// the first time this function is called for a given table, set up an
// array of reverse sort flags.
if (tblel.reversesort == null) {
tblel.reversesort = new array();
// also, assume the team name column is initially sorted.
tblel.lastcolumn = 1;
}
// if this column has not been sorted before, set the initial sort direction.
if (tblel.reversesort[col] == null)
tblel.reversesort[col] = rev;
// if this column was the last one sorted, reverse its sort direction.
if (col == tblel.lastcolumn)
tblel.reversesort[col] = !tblel.reversesort[col];
// sort the rows based on the content of the specified column using a
// selection sort.
var tmpel;
var i, j;
var minval, minidx;
var testval;
var cmp;
// search the rows that follow the current one for a smaller value.
for (j = i + 1; j < tblel.rows.length; j++) {
testval = gettextvalue(tblel.rows[j].cells[col]);
cmp = comparevalues(minval, testval);
// negate the comparison result if the reverse sort flag is set.
if (tblel.reversesort[col])
cmp = -cmp;
// sort by the second column (team name) if those values are equal.
if (cmp == 0 && col != 1)
cmp = comparevalues(gettextvalue(tblel.rows[minidx].cells[1]),
gettextvalue(tblel.rows[j].cells[1]));
// if this row has a smaller value than the current minimum, remember its
// position and update the current minimum value.
if (cmp > 0) {
minidx = j;
minval = testval;
}
}
// by now, we have the row with the smallest value. remove it from the
// table and insert it before the current row.
if (minidx > i) {
tmpel = tblel.removechild(tblel.rows[minidx]);
tblel.insertbefore(tmpel, tblel.rows[i]);
}
}
return false;
}
//-----------------------------------------------------------------------------
// functions to get and compare values during a sort.
//-----------------------------------------------------------------------------
// this code is necessary for browsers that don't reflect the dom constants
// (like ie).
if (document.element_node == null) {
document.element_node = 1;
document.text_node = 3;
}
function gettextvalue(el) {
var i;
var s;
// find and concatenate the values of all text nodes contained within the
// element.
s = "";
for (i = 0; i < el.childnodes.length; i++)
if (el.childnodes[i].nodetype == document.text_node)
s += el.childnodes[i].nodevalue;
else if (el.childnodes[i].nodetype == document.element_node &&
el.childnodes[i].tagname == "br")
s += " ";
else
// use recursion to get text within sub-elements.
s += gettextvalue(el.childnodes[i]);
return normalizestring(s);
}
f1 = parsefloat(v1);
f2 = parsefloat(v2);
if (!isnan(f1) && !isnan(f2)) {
v1 = f1;
v2 = f2;
}
return s;
}
//-----------------------------------------------------------------------------
// functions to update the table appearance after a sort.
//-----------------------------------------------------------------------------
var i, j;
var rowel, cellel;
// find the table header and highlight the column that was sorted.
var el = tblel.parentnode.thead;
rowel = el.rows[el.rows.length - 1];
// set style classes for each column as above.
for (i = 2; i < rowel.cells.length; i++) {
cellel = rowel.cells[i];
cellel.classname = cellel.classname.replace(coltest, "");
// highlight the header of the sorted column.
if (i == col)
cellel.classname += " " + colclsnm;
cellel.classname = normalizestring(cellel.classname);
}
}
var i = 0;
var incr = 1;
if (tblel.reversesort[col])
rev = !rev;
if (rev) {
incr = -1;
i = tblel.rows.length - 1;
}
var count = 1;
var rank = count;
var curval;
var lastval = null;
// note that this loop is skipped if the table was sorted on the name
// column.
while (col > 1 && i >= 0 && i < tblel.rows.length) {
// on rows after the first, compare the sort value of this row to the
// previous one. if they differ, update the rank to match the current row
// count. (if they are the same, this row will get the same rank as the
// previous one.)
if (lastval != null && comparevalues(curval, lastval) != 0)
rank = count;
// set the rank for this row.
tblel.rows[i].rank = rank;
// save the sort value of the current row for the next time around and bump
// the row counter and index.
lastval = curval;
count++;
i += incr;
}
// now go through each row (from top to bottom) and display its rank. note
// that when two or more rows are tied, the rank is shown on the first of
// those rows only.
//]]></script>
</head>
<body>
<div id="demobox">
<p>this page contains two similar tables than can be dynamically sorted in a
number of ways just by clicking on a column header. to reverse the sort order
for a given column, click on it twice in a row.</p>
<p>the table's appearance and some of its content changes with each sort as
well. in addition to style changes, the <i>rank</i> column is updated to match
the sorted column.</p>
<p>use your browser's <code>view source</code> option to see the full source
code.</p>
</div>
<p></p>
<p></p>
</body>
</html>