tablesorter/docs/example-parsers-advanced.html

146 lines
3.6 KiB
HTML
Raw Normal View History

2012-03-07 18:06:10 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery plugin: Tablesorter 2.0 - Writing custom parsers, advanced use</title>
<!-- jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<!-- Demo stuff -->
<link rel="stylesheet" href="css/jq.css">
<script src="js/chili/jquery.chili-2.2.js"></script>
<script src="js/chili/recipes.js"></script>
<script src="js/docs.js"></script>
<!-- Tablesorter: required -->
2012-09-27 19:57:19 +00:00
<link rel="stylesheet" href="../css/theme.blue.css">
2012-03-07 18:06:10 +00:00
<script src="../js/jquery.tablesorter.js"></script>
<script id="js">// add parser through the tablesorter addParser method
$(function(){
$.tablesorter.addParser({
// set a unique id
id: 'data',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s, table, cell, cellIndex) {
2012-04-12 22:05:28 +00:00
var $cell = $(cell);
2012-03-07 18:06:10 +00:00
// I could have used $(cell).data(), then we get back an object which contains both
// data-lastname & data-date; but I wanted to make this demo a bit more straight-forward
// and easier to understand.
// first column (zero-based index) has lastname data attribute
if (cellIndex === 0) {
// returns lastname data-attribute, or cell text (s) if it doesn't exist
return $cell.attr('data-lastname') || s;
// third column has date data attribute
} else if (cellIndex === 2) {
// return "mm-dd" that way we don't need to use "new Date()" to process it
return $cell.attr('data-date') || s;
}
// return cell text, just in case
return s;
},
// set type, either numeric or text
type: 'text'
});
$('table').tablesorter({
2012-09-27 19:57:19 +00:00
theme: 'blue',
headers: {
2012-03-07 18:06:10 +00:00
0 : { sorter: 'data' },
2 : { sorter: 'data' }
},
widgets: ['zebra']
});
});
</script>
</head>
<body>
<div id="banner">
<h1>table<em>sorter</em></h1>
<h2>Writing custom parsers, advanced use</h2>
<h3>Flexible client-side table sorting</h3>
<a href="index.html">Back to documentation</a>
</div>
<div id="main">
2012-09-27 19:57:19 +00:00
<p class="tip">
<em>NOTE!</em>
<ul>
<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>
</ul>
</p>
2012-03-07 18:06:10 +00:00
<h1>Demo</h1>
<div id="demo"><table>
<thead>
<tr>
<th>Name (Last)</th>
<th>Originally from...</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
<tr>
<td data-lastname="Allen">Joe Allen</td>
<td>South Carolina</td>
<td data-date="01-15">Jan 15</td>
</tr>
<tr>
<td data-lastname="Torres">Lisa Torres</td>
<td>Maryland</td>
<td data-date="03-02">March 2nd</td> <!-- leading zeros needed to sort properly! -->
</tr>
<tr>
<td data-lastname="Franklin">Peter Louis Franklin</td>
<td>Coventry</td>
<td data-date="12-26">Boxing Day (Dec 26th)</td>
</tr>
<tr>
<td data-lastname="Jones">Maria Consuela de Los Angeles Ortiz Del Toro-Jones</td>
<td>Texas</td>
<td data-date="05-10">10 Mayo</td>
</tr>
<tr>
<td data-lastname="Bigglesworth">Mike "the Smasher" Bigglesworth</td>
<td>Rhode Island</td>
<td data-date="06-22">22nd of June</td>
</tr>
<tr>
<td data-lastname="Smith">Fredrick Smith</td>
<td>Ohio</td>
<td data-date="03-10">10th Mar</td>
</tr>
</tbody>
</table></div>
<h1>Javascript</h1>
<div id="javascript">
<pre class="js"></pre>
</div>
<h1>HTML</h1>
<div id="html">
<pre class="html"></pre>
</div>
<div class="next-up">
<hr />
Next up: <a href="example-option-text-extraction.html">Dealing with markup inside cells (textExtraction function) &rsaquo;&rsaquo;</a>
</div>
</div>
</body>
</html>