| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>jQuery tablesorter 2.0 - Writing custom parsers, advanced use</title> |
| 6 |
|
| 7 |
|
| 8 |
<script src="js/jquery-latest.min.js"></script> |
| 9 |
|
| 10 |
|
| 11 |
<link rel="stylesheet" href="css/jq.css"> |
| 12 |
<link href="css/prettify.css" rel="stylesheet"> |
| 13 |
<script src="js/prettify.js"></script> |
| 14 |
<script src="js/docs.js"></script> |
| 15 |
|
| 16 |
|
| 17 |
<link rel="stylesheet" href="../css/theme.blue.css"> |
| 18 |
<script src="../js/jquery.tablesorter.js"></script> |
| 19 |
|
| 20 |
<script id="js">// add parser through the tablesorter addParser method |
| 21 |
$(function() { |
| 22 |
|
| 23 |
$.tablesorter.addParser({ |
| 24 |
// set a unique id |
| 25 |
id: 'data', |
| 26 |
is: function(s, table, cell, $cell) { |
| 27 |
// return false so this parser is not auto detected |
| 28 |
return false; |
| 29 |
}, |
| 30 |
format: function(s, table, cell, cellIndex) { |
| 31 |
var $cell = $(cell); |
| 32 |
// I could have used $(cell).data(), then we get back an object which contains both |
| 33 |
// data-lastname & data-date; but I wanted to make this demo a bit more straight-forward |
| 34 |
// and easier to understand. |
| 35 |
|
| 36 |
// first column (zero-based index) has lastname data attribute |
| 37 |
if (cellIndex === 0) { |
| 38 |
// returns lastname data-attribute, or cell text (s) if it doesn't exist |
| 39 |
return $cell.attr('data-lastname') || s; |
| 40 |
|
| 41 |
// third column has date data attribute |
| 42 |
} else if (cellIndex === 2) { |
| 43 |
// return "mm-dd" that way we don't need to use "new Date()" to process it |
| 44 |
return $cell.attr('data-date') || s; |
| 45 |
} |
| 46 |
|
| 47 |
// return cell text, just in case |
| 48 |
return s; |
| 49 |
}, |
| 50 |
// flag for filter widget (true = ALWAYS search parsed values; false = search cell text) |
| 51 |
parsed: false, |
| 52 |
// set type, either numeric or text |
| 53 |
type: 'text' |
| 54 |
}); |
| 55 |
|
| 56 |
$('table').tablesorter({ |
| 57 |
theme: 'blue', |
| 58 |
headers: { |
| 59 |
0 : { sorter: 'data' }, |
| 60 |
2 : { sorter: 'data' } |
| 61 |
}, |
| 62 |
widgets: ['zebra'] |
| 63 |
}); |
| 64 |
|
| 65 |
}); |
| 66 |
</script> |
| 67 |
</head> |
| 68 |
<body> |
| 69 |
|
| 70 |
<div id="banner"> |
| 71 |
<h1>table<em>sorter</em></h1> |
| 72 |
<h2>Writing custom parsers, advanced use</h2> |
| 73 |
<h3>Flexible client-side table sorting</h3> |
| 74 |
<a href="index.html">Back to documentation</a> |
| 75 |
</div> |
| 76 |
|
| 77 |
<div id="main"> |
| 78 |
|
| 79 |
<p class="tip"> |
| 80 |
<em>NOTE!</em> |
| 81 |
</p> |
| 82 |
<ul> |
| 83 |
<li>In <span class="version">v2.15.0</span>, the <code>parsed</code> parameter |
| 84 |
<ul> |
| 85 |
<li>This parameter is a flag used by the filter widget.</li> |
| 86 |
<li>When <code>true</code>, the filter widget will only search through parsed data for matches.</li> |
| 87 |
<li>If <code>false</code> (default value), the filter widget will search through the cell text for matches.</li> |
| 88 |
</ul> |
| 89 |
</li> |
| 90 |
<li>This method of writing custom parsers will NOT work with the original tablesorter 2.0.5b plugin because the format function does not consistently provide the <code>cell</code> and <code>cellIndex</code> parameters.</li> |
| 91 |
</ul> |
| 92 |
|
| 93 |
<h1>Demo</h1> |
| 94 |
<div id="demo"><table> |
| 95 |
<thead> |
| 96 |
<tr> |
| 97 |
<th>Name (Last)</th> |
| 98 |
<th>Originally from...</th> |
| 99 |
<th>Birthday</th> |
| 100 |
</tr> |
| 101 |
</thead> |
| 102 |
<tbody> |
| 103 |
<tr> |
| 104 |
<td data-lastname="Allen">Joe Allen</td> |
| 105 |
<td>South Carolina</td> |
| 106 |
<td data-date="01-15">Jan 15</td> |
| 107 |
</tr> |
| 108 |
<tr> |
| 109 |
<td data-lastname="Torres">Lisa Torres</td> |
| 110 |
<td>Maryland</td> |
| 111 |
<td data-date="03-02">March 2nd</td> |
| 112 |
</tr> |
| 113 |
<tr> |
| 114 |
<td data-lastname="Franklin">Peter Louis Franklin</td> |
| 115 |
<td>Coventry</td> |
| 116 |
<td data-date="12-26">Boxing Day (Dec 26th)</td> |
| 117 |
</tr> |
| 118 |
<tr> |
| 119 |
<td data-lastname="Jones">Maria Consuela de Los Angeles Ortiz Del Toro-Jones</td> |
| 120 |
<td>Texas</td> |
| 121 |
<td data-date="05-10">10 Mayo</td> |
| 122 |
</tr> |
| 123 |
<tr> |
| 124 |
<td data-lastname="Bigglesworth">Mike "the Smasher" Bigglesworth</td> |
| 125 |
<td>Rhode Island</td> |
| 126 |
<td data-date="06-22">22nd of June</td> |
| 127 |
</tr> |
| 128 |
<tr> |
| 129 |
<td data-lastname="Smith">Fredrick Smith</td> |
| 130 |
<td>Ohio</td> |
| 131 |
<td data-date="03-10">10th Mar</td> |
| 132 |
</tr> |
| 133 |
</tbody> |
| 134 |
</table></div> |
| 135 |
|
| 136 |
<h1>Javascript</h1> |
| 137 |
<div id="javascript"> |
| 138 |
<pre class="prettyprint lang-javascript"></pre> |
| 139 |
</div> |
| 140 |
<h1>HTML</h1> |
| 141 |
<div id="html"> |
| 142 |
<pre class="prettyprint lang-html"></pre> |
| 143 |
</div> |
| 144 |
|
| 145 |
<div class="next-up"> |
| 146 |
<hr /> |
| 147 |
Next up: <a href="example-option-text-extraction.html">Dealing with markup inside cells (textExtraction function) ››</a> |
| 148 |
</div> |
| 149 |
|
| 150 |
</div> |
| 151 |
|
| 152 |
</body> |
| 153 |
</html> |