Click table to sort

Source: Internet
Author: User
Tip: you can modify some code before running

<LINK href="http://www.brainjar.com/common/default.css" type=text/css rel=stylesheet><STYLE type=text/css>TABLE {BORDER-RIGHT: #000000 2px solid; BORDER-TOP: #000000 2px solid; BORDER-LEFT: #000000 2px solid; BORDER-BOTTOM: #000000 2px solid; border-spacing: 0px; cell-spacing: 0px} TD {PADDING-RIGHT: 0.5em; PADDING-LEFT: 0.5em; FONT-SIZE: 10pt; PADDING-BOTTOM: 2px; PADDING-TOP: 2px; FONT-FAMILY: Arial, Helvetica, sans-serif; WHITE-SPACE: nowrap} TH {PADDING-RIGHT: 0.5em; PADDING-LEFT: 0.5em; FONT-SIZE: 10pt; PADDING-BOTTOM: 2px; PADDING-TOP: 2px; FONT-FAMILY: Arial, Helvetica, sans-serif; WHITE-SPACE: nowrap} TD. numeric {TEXT-ALIGN: right} TH {BACKGROUND-COLOR: # c0c0c0} TH. mainHeader {COLOR: # ffffff; BACKGROUND-COLOR: #808080; TEXT-ALIGN: left} th a {COLOR: #000080; TEXT-DECORATION: none} th: visited {COLOR: #000080} th a: active {COLOR: #800000; TEXT-DECORATION: underline} 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>Function sortTable (id, col, rev) {// Get the table or table section to sort. var tblEl = document. getElementById (id); // 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 ction. if (col = tblEl. lastColumn) tblEl. reverseSort [col] =! TblEl. reverseSort [col]; // Remember this column as the last one sorted. tblEl. lastColumn = col; // 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 tmpEl; var I, j; var minVal, minIdx; var testVal; var 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 s Ort 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 // eleme Nt. 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 wht1_ds = new RegExp ("^ \ s * | \ s * $", "g"); var whtSpMult = new RegExp ("\ s + ", "g"); function normalizeString (s) {s = s. replace (whtSpMult, ""); // Collapse any multiple whites space. s = s. replace (whtSpEnds, ""); // 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 Reg Exp (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 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) {// Get the value of the sort column in this row. curVa L = getTextValue (tblEl. rows [I]. cells [col]); // 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. 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 ;}}}</sCRIPT><META content="MSHTML 6.00.2600.0" name=GENERATOR></hEAD><P><!-- Defensive statistics table. --><TABLE cellSpacing=0 cellPadding=0 border=0> <THEAD> <TR> <TH class=mainHeader colSpan=11>NFL 2001 Defensive Stats</tH></tR> <TR> <TH >Rank</tH> <TH >Team</tH> <TH><SPAN title="Games Played">Gms</sPAN></tH> <TH>Yds</tH> <TH>Yds/G</tH> <TH>RuYds</tH> <TH>RuYds/G</tH> <TH>PaYds</tH> <TH>PaYds/G</tH> <TH>Pts</tH> <TH>Pts/G</tH></tR></tHEAD> <TBODY id=defTblBdy> <TR class=""> <TD class=numeric></tD> <TD class="">Arizona</tD> <TD class=numeric>16</tD> <TD class=numeric>5685</tD> <TD class=numeric>355.3</tD> <TD class=numeric>2087</tD> <TD class=numeric>130.4</tD> <TD class=numeric>3598</tD> <TD class=numeric>224.9</tD> <TD class=numeric>343</tD> <TD class=numeric>21.4</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Atlanta</tD> <TD class=numeric>16</tD> <TD class=numeric>5845</tD> <TD class=numeric>365.3</tD> <TD class=numeric>1943</tD> <TD class=numeric>121.4</tD> <TD class=numeric>3902</tD> <TD class=numeric>243.9</tD> <TD class=numeric>377</tD> <TD class=numeric>23.6</tD></tR> <TR class=""> <TD class=numeric></tD> <TD class="">Baltimore</tD> <TD class=numeric>16</tD> <TD class=numeric>4267</tD> <TD class=numeric>284.5</tD> <TD class=numeric>1325</tD> <TD class=numeric>88.3</tD> <TD class=numeric>2942</tD> <TD class=numeric>196.1</tD> <TD class=numeric>262</tD> <TD class=numeric>17.5</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Buffalo</tD> <TD class=numeric>16</tD> <TD class=numeric>5292</tD> <TD class=numeric>330.8</tD> <TD class=numeric>2133</tD> <TD class=numeric>133.3</tD> <TD class=numeric>3159</tD> <TD class=numeric>197.4</tD> <TD class=numeric>420</tD> <TD class=numeric>26.2</tD></tR> <TR> <TD class=numeric></tD> <TD class="">Carolina</tD> <TD class=numeric>16</tD> <TD class=numeric>5943</tD> <TD class=numeric>371.4</tD> <TD class=numeric>2301</tD> <TD class=numeric>143.8</tD> <TD class=numeric>3642</tD> <TD class=numeric>227.6</tD> <TD class=numeric>410</tD> <TD class=numeric>25.6</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Chicago</tD> <TD class=numeric>16</tD> <TD class=numeric>4978</tD> <TD class=numeric>311.1</tD> <TD class=numeric>1313</tD> <TD class=numeric>82.1</tD> <TD class=numeric>3665</tD> <TD class=numeric>229.1</tD> <TD class=numeric>203</tD> <TD class=numeric>12.7</tD></tR> <TR> <TD class=numeric></tD> <TD class="">Cincinnati</tD> <TD class=numeric>16</tD> <TD class=numeric>4832</tD> <TD class=numeric>302.0</tD> <TD class=numeric>1675</tD> <TD class=numeric>104.7</tD> <TD class=numeric>3157</tD> <TD class=numeric>197.3</tD> <TD class=numeric>309</tD> <TD class=numeric>19.3</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Cleveland</tD> <TD class=numeric>16</tD> <TD class=numeric>5297</tD> <TD class=numeric>331.1</tD> <TD class=numeric>2208</tD> <TD class=numeric>138.0</tD> <TD class=numeric>3089</tD> <TD class=numeric>193.1</tD> <TD class=numeric>319</tD> <TD class=numeric>19.9</tD></tR> <TR> <TD class=numeric></tD> <TD class="">Dallas</tD> <TD class=numeric>16</tD> <TD class=numeric>4599</tD> <TD class=numeric>287.4</tD> <TD class=numeric>1710</tD> <TD class=numeric>106.9</tD> <TD class=numeric>2889</tD> <TD class=numeric>180.6</tD> <TD class=numeric>338</tD> <TD class=numeric>21.1</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Denver</tD> <TD class=numeric>16</tD> <TD class=numeric>4774</tD> <TD class=numeric>298.4</tD> <TD class=numeric>1492</tD> <TD class=numeric>93.2</tD> <TD class=numeric>3282</tD> <TD class=numeric>205.1</tD> <TD class=numeric>339</tD> <TD class=numeric>21.2</tD></tR> <TR> <TD class=numeric></tD> <TD class="">Detroit</tD> <TD class=numeric>16</tD> <TD class=numeric>5521</tD> <TD class=numeric>345.1</tD> <TD class=numeric>1993</tD> <TD class=numeric>124.6</tD> <TD class=numeric>3528</tD> <TD class=numeric>220.5</tD> <TD class=numeric>424</tD> <TD class=numeric>26.5</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Green Bay</tD> <TD class=numeric>16</tD> <TD class=numeric>4937</tD> <TD class=numeric>308.6</tD> <TD class=numeric>1769</tD> <TD class=numeric>110.6</tD> <TD class=numeric>3168</tD> <TD class=numeric>198.0</tD> <TD class=numeric>266</tD> <TD class=numeric>16.6</tD></tR> <TR> <TD class=numeric></tD> <TD class="">Indianapois</tD> <TD class=numeric>16</tD> <TD class=numeric>5715</tD> <TD class=numeric>357.2</tD> <TD class=numeric>2115</tD> <TD class=numeric>132.2</tD> <TD class=numeric>3600</tD> <TD class=numeric>225.0</tD> <TD class=numeric>486</tD> <TD class=numeric>30.4</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Jacksonville</tD> <TD class=numeric>16</tD> <TD class=numeric>5070</tD> <TD class=numeric>316.9</tD> <TD class=numeric>1611</tD> <TD class=numeric>100.7</tD> <TD class=numeric>3459</tD> <TD class=numeric>216.2</tD> <TD class=numeric>286</tD> <TD class=numeric>17.9</tD></tR> <TR> <TD class=numeric></tD> <TD class="">Kansas City</tD> <TD class=numeric>16</tD> <TD class=numeric>5304</tD> <TD class=numeric>331.5</tD> <TD class=numeric>2140</tD> <TD class=numeric>133.8</tD> <TD class=numeric>3164</tD> <TD class=numeric>197.8</tD> <TD class=numeric>344</tD> <TD class=numeric>21.5</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">Miami</tD> <TD class=numeric>16</tD> <TD class=numeric>4608</tD> <TD class=numeric>288.0</tD> <TD class=numeric>1779</tD> <TD class=numeric>111.2</tD> <TD class=numeric>2829</tD> <TD class=numeric>176.8</tD> <TD class=numeric>290</tD> <TD class=numeric>18.1</tD></tR> <TR> <TD class=numeric></tD> <TD class="">Minnesota</tD> <TD class=numeric>16</tD> <TD class=numeric>5315</tD> <TD class=numeric>354.3</tD> <TD class=numeric>2087</tD> <TD class=numeric>139.1</tD> <TD class=numeric>3228</tD> <TD class=numeric>215.2</tD> <TD class=numeric>371</tD> <TD class=numeric>24.7</tD></tR> <TR class=alternateRow> <TD class=numeric></tD> <TD class="">New England</tD> <TD class=numeric>16</tD> <TD class=numeric>5352</tD> <TD class=numeric>334.5</tD> <TD class=numeric>1855</tD> <TD class=numeric>115.9</tD> <TD class=numeric>3497</tD> <TD class=numeric>218.6</tD> <TD class=numeric>272</tD> <TD class=numeric>17.0</tD></tR> <TR> <TD class=numeric></tD> <TD class="">New Orleans</tD> <TD class=numeric>16</tD> <TD class=numeric>5070</tD> <TD class=numeric>316.9</tD> <TD class=numeric>1715</tD> <TD class=numeric>107.2</tD> <TD class=numeric>3355</tD> <TD class=numeric>209.7</tD> <TD class=numeric>409</tD> <TD class=numeric>25.6</tD></tR></tBODY></tABLE></p>
Tip: you can modify some code before running

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.