This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Alternate Way - Sorting Table Scripts as PDF for free.
} tr.alternaterow { background-color: #e0e0e0; } td.sortedcolumn { background-color: #f0f0f0; } th.sortedcolumn { background-color: #b0b0b0; } tr.alternaterow td.sortedcolumn { background-color: #d0d0d0; } <script type="text/javascript">//
// set the table display style to "none" - necessary for netscape 6 // browsers. var olddsply = tblel.style.display; tblel.style.display = "none"; // sort the rows based on the content of the specified column using a // selection sort. var var var var var
tmpel; i, j; minval, minidx; testval; cmp;
for (i = 0; i < tblel.rows.length - 1; i++) { // assume the current row has the minimum value. minidx = i; minval = gettextvalue(tblel.rows[i].cells[col]); // 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]); } } // make it look pretty. makepretty(tblel, col); // set team rankings. setranks(tblel, col, rev); // restore the table's display style. tblel.style.display = olddsply; 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); } function comparevalues(v1, v2) { var f1, f2; // if the values are numeric, convert them to floats. f1 = parsefloat(v1); f2 = parsefloat(v2); if (!isnan(f1) && !isnan(f2)) { v1 = f1; v2 = f2; }
}
// compare the two values. if (v1 == v2) return 0; if (v1 > v2) return 1 return -1;
// regular expressions for normalizing white space. var whtspends = new regexp("^\\s*|\\s*$", "g"); var whtspmult = new regexp("\\s\\s+", "g");
function normalizestring(s) { s = s.replace(whtspmult, " "); s = s.replace(whtspends, ""); }
// collapse any multiple whites space. // remove leading or trailing white space.
return s;
//----------------------------------------------------------------------------// functions to update the table appearance after a sort. //----------------------------------------------------------------------------// style class names. var rowclsnm = "alternaterow"; var colclsnm = "sortedcolumn"; // regular expressions for setting class names. var rowtest = new regexp(rowclsnm, "gi"); var coltest = new regexp(colclsnm, "gi"); function makepretty(tblel, col) { var i, j; var rowel, cellel; // set style classes on each row to alternate their appearance. for (i = 0; i < tblel.rows.length; i++) { rowel = tblel.rows[i]; rowel.classname = rowel.classname.replace(rowtest, ""); if (i % 2 != 0) rowel.classname += " " + rowclsnm; rowel.classname = normalizestring(rowel.classname); // set style classes on each column (other than the name column) to // highlight the one that was sorted. for (j = 2; j < tblel.rows[i].cells.length; j++) { cellel = rowel.cells[j]; cellel.classname = cellel.classname.replace(coltest, ""); if (j == col) cellel.classname += " " + colclsnm; cellel.classname = normalizestring(cellel.classname); } } // 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); } } function setranks(tblel, col, rev) {
// determine whether to start at the top row of the table and go down or // at the bottom row and work up. this is based on the current sort // direction of the column and its reversed flag. var i = 0; var incr = 1; if (tblel.reversesort[col]) rev = !rev; if (rev) { incr = -1; i = tblel.rows.length - 1; } // now go through each row in that direction and assign it a rank by // counting 1, 2, 3... var var var var
count = 1; rank = count; curval; 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) { // get the value of the sort column in this row. curval = gettextvalue(tblel.rows[i].cells[col]); // // // // if
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.) (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. var rowel, cellel; var lastrank = 0; // go through the rows from top to bottom. for (i = 0; i < tblel.rows.length; i++) { rowel = tblel.rows[i]; cellel = rowel.cells[0]; // delete anything currently in the rank column. while (cellel.lastchild != null)
cellel.removechild(cellel.lastchild); // if this row's rank is different from the previous one, insert a new text // node with that rank. if (col > 1 && rowel.rank != lastrank) { cellel.appendchild(document.createtextnode(rowel.rank)); lastrank = rowel.rank; } }
}
//]]>
table sorting demo
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.
the table's appearance and some of its content changes with each sort as well. in addition to style changes, the rank column is updated to match the sorted column.
use your browser's view source option to see the full source code.