Alternate Way - Sorting Table Scripts

  • October 2019
  • PDF

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.

More details

  • Words: 3,210
  • Pages: 22
brainjar.com: table sort demo <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <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; } <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.















nfl 2001 offensive stats
rank team <span title="games played">gms yds yds/g ruyds ruyds/g payds payds/g pts pts/g
arizona 16 4898 306.1 1449 90.6 3449 215.6 295 18.4
atlanta 16 5070 316.9 1773 110.8 3297 206.1 291 18.2
baltimore 16 4773 318.2 1598 106.5 3175 211.7 284 18.9
buffalo 16 5137 321.1 1686 105.4 3451 215.7 265 16.6
carolina 16 4254 265.9 1372 85.8 2882 180.1 253 15.8
chicago 16 4694 293.4 1742 108.9 2952 184.5 338 21.1
cincinnati 16 4800 300.0 1712 107.0 3088 193.0 226 14.1
cleveland 16 4152 259.5 1351 84.4 2801 175.1 285 17.8
dallas 16 4402 275.1 2184 136.5 2218 138.6 246 15.4
denver 16 4817 301.1 1877 117.3 2940 183.8 340 21.2
detroit 16 4994 312.1 1398 87.4 3596 224.8 270 16.9
green bay 16 5463 341.4 1693 105.8 3770 235.6 390 24.4
indianapolis 16 5955 372.2 1966 122.9 3989 249.3 413 25.8
jacksonville 16 4840 302.5 1600 100.0 3240 202.5 294 18.4
kansas city 16 5673 354.6 2008 125.5 3665 229.1 320 20.0
miami 16 4821 301.3 1664 104.0 3157 197.3 344 21.5
minnesota 16 5006 333.7 1523 101.5 3483 232.2 287 19.1
new england 16 4882 305.1 1793 112.1 3089 193.1 371 23.2
new orleans 16 5226 326.6 1712 107.0 3514 219.6 333 20.8
new york giants 16 5335 333.4 1777 111.1 3558 222.4 294 18.4
new york jets 16 4795 299.7 2054 128.4 2741 171.3 308 19.2
oakland 16 5361 335.1 1654 103.4 3707 231.7 399 24.9
philadelphia 16 4923 307.7 1778 111.1 3145 196.6 343 21.4
pittsburgh 16 5887 367.9 2774 173.4 3113 194.6 352 22.0
saint louis 16 6690 418.1 2027 126.7 4663 291.4 503 31.4
san diego 16 5200 325.0 1695 105.9 3505 219.1 332 20.8
san francisco 16 5689 355.6 2244 140.2 3445 215.3 409 25.6
seattle 16 4772 298.2 1936 121.0 2836 177.2 301 18.8
tampa bay 16 4694 293.4 1371 85.7 3323 207.7 324 20.2
tennessee 16 5354 334.6 1796 112.2 3558 222.4 336 21.0
washington 16 4435 277.2 1948 121.8 2487 155.4 256 16.0















nfl 2001 defensive stats
rank team <span title="games played">gms yds yds/g ruyds ruyds/g payds payds/g pts pts/g
arizona 16 5685 355.3 2087 130.4 3598 224.9 343 21.4
atlanta 16 5845 365.3 1943 121.4 3902 243.9 377 23.6
baltimore 16 4267 284.5 1325 88.3 2942 196.1 262 17.5
buffalo 16 5292 330.8 2133 133.3 3159 197.4 420 26.2
carolina 16 5943 371.4 2301 143.8 3642 227.6 410 25.6
chicago 16 4978 311.1 1313 82.1 3665 229.1 203 12.7
cincinnati 16 4832 302.0 1675 104.7 3157 197.3 309 19.3
cleveland 16 5297 331.1 2208 138.0 3089 193.1 319 19.9
dallas 16 4599 287.4 1710 106.9 2889 180.6 338 21.1
denver 16 4774 298.4 1492 93.2 3282 205.1 339 21.2
detroit 16 5521 345.1 1993 124.6 3528 220.5 424 26.5
green bay 16 4937 308.6 1769 110.6 3168 198.0 266 16.6
indianapolis 16 5715 357.2 2115 132.2 3600 225.0 486 30.4
jacksonville 16 5070 316.9 1611 100.7 3459 216.2 286 17.9
kansas city 16 5304 331.5 2140 133.8 3164 197.8 344 21.5
miami 16 4608 288.0 1779 111.2 2829 176.8 290 18.1
minnesota 16 5315 354.3 2087 139.1 3228 215.2 371 24.7
new england 16 5352 334.5 1855 115.9 3497 218.6 272 17.0
new orleans 16 5070 316.9 1715 107.2 3355 209.7 409 25.6
new york giants 16 4975 310.9 1545 96.6 3430 214.4 321 20.1
new york jets 16 5153 322.1 2154 134.6 2999 187.4 295 18.4
oakland 16 5071 316.9 1988 124.2 3083 192.7 327 20.4
philadelphia 16 4701 293.8 1837 114.8 2864 179.0 208 13.0
pittsburgh 16 4139 258.7 1197 74.8 2942 183.9 212 13.2
saint louis 16 4471 279.4 1385 86.6 3086 192.9 273 17.1
san diego 16 4904 306.5 1504 94.0 3400 212.5 321 20.1
san francisco 16 4954 309.6 1571 98.2 3383 211.4 282 17.6
seattle 16 5206 325.4 1721 107.6 3485 217.8 324 20.2
tampa bay 16 4653 290.8 1702 106.4 2951 184.4 280 17.5
tennessee 16 5515 344.7 1431 89.4 4084 255.2 388 24.2
washington 16 4846 302.9 1869 116.8 2977 186.1 303 18.9

Related Documents

Scripts
November 2019 27
Sorting
May 2020 22
Scripts
June 2020 14