tablesorter/docs/example-parsers.html

161 lines
6.1 KiB
HTML
Raw Normal View History

2011-07-17 15:01:18 +00:00
<!DOCTYPE html>
<html>
2011-06-22 23:19:27 +00:00
<head>
2011-07-17 15:01:18 +00:00
<meta charset="utf-8">
2011-06-22 23:19:27 +00:00
<title>jQuery plugin: Tablesorter 2.0 - Writing custom parsers</title>
<!-- jQuery -->
2012-03-03 01:38:02 +00:00
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
2011-06-22 23:19:27 +00:00
<!-- Demo stuff -->
2011-07-17 15:01:18 +00:00
<link rel="stylesheet" href="css/jq.css">
<link href="css/prettify.css" rel="stylesheet">
<script src="js/prettify.js"></script>
2011-07-17 15:01:18 +00:00
<script src="js/docs.js"></script>
2011-06-22 23:19:27 +00:00
<!-- Tablesorter: required -->
2012-09-27 19:57:19 +00:00
<link rel="stylesheet" href="../css/theme.blue.css">
2011-07-17 15:01:18 +00:00
<script src="../js/jquery.tablesorter.js"></script>
<script src="../addons/pager/jquery.tablesorter.pager.js"></script>
<script id="js">// add parser through the tablesorter addParser method
2011-06-22 23:19:27 +00:00
$.tablesorter.addParser({
// set a unique id
id: 'grades',
2013-03-26 21:16:13 +00:00
is: function(s, table, cell) {
2011-06-22 23:19:27 +00:00
// return false so this parser is not auto detected
2013-01-29 22:42:23 +00:00
return false;
},
2012-03-07 18:06:35 +00:00
format: function(s, table, cell, cellIndex) {
2011-06-22 23:19:27 +00:00
// format your data for normalization
2011-07-17 15:01:18 +00:00
return s.toLowerCase()
.replace(/good/,2)
.replace(/medium/,1)
.replace(/bad/,0);
2011-06-22 23:19:27 +00:00
},
// set type, either numeric or text
type: 'numeric'
});
$(function() {
$("table").tablesorter({
2012-09-27 19:57:19 +00:00
theme: 'blue'
2011-08-04 06:24:26 +00:00
// "sorter-grades" added as a class name in the HTML (new in v2.0.11)
// or un-comment out the option below
2012-09-27 19:57:19 +00:00
// ,headers: { 6: { sorter: 'grades' } }
2011-06-22 23:19:27 +00:00
});
2011-07-17 15:01:18 +00:00
2011-06-22 23:19:27 +00:00
});</script>
</head>
<body>
2011-07-17 15:01:18 +00:00
2011-06-22 23:19:27 +00:00
<div id="banner">
<h1>table<em>sorter</em></h1>
<h2>Writing custom parsers</h2>
<h3>Flexible client-side table sorting</h3>
<a href="index.html">Back to documentation</a>
</div>
2011-07-17 15:01:18 +00:00
2011-06-22 23:19:27 +00:00
<div id="main">
2011-09-08 16:28:10 +00:00
2013-03-26 21:16:13 +00:00
<div class="tip">
Notes about the <code>addWidget</code> template:
<ul>
<li>The <code>id</code> block is required and must be unique.</li>
<li>The <code>is</code> block will allow the parser to be used for autodetecting the parser
<ul>
<li>Most custom parsers are made for a specific set of data/column, so the <code>is</code> block usually just returns false telling the plugin that the parser doesn't match any columns.</li>
<li>If so desired, the function provides three parameters: <code>s</code> contains the text from the cell, <code>table</code> is the table DOM element and <code>cell</code> is the current cell DOM element.</li>
<li>This function must <code>return true</code> before the plugin will use the custom parser's format block to process the column content.</li>
<li>In version 2.7.4, the <code>is</code> block became optional because most parsers just return false.</li>
</ul>
</li>
<li>The <code>format</code> block is used to normalize your data and return it to the plugin for sorting
<ul>
<li>Within this function, modify the given text from the cell (<code>s</code>) or obtain parameters and/or other data from the cell (<code>cell</code>) then return this data to the plugin.</li>
<li>As an example, the date parser takes the date string (e.g. <code>"12/25/2013"</code>) and converts it into a numeric value (<code>1387951200000</code>; <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTime">ref</a>) to make sorting and comparing dates easier.</li>
<li>Use the <code>cellIndex</code> if the cells within columns contain different data - see this demo for an example.</li>
</ul>
</li>
<li>The <code>type</code> block sets the type of sort to use:
<ul>
<li>Set it to either <code>"text"</code> or <code>"numeric"</code>.</li>
<li>This tells the plugin the type of sorter to use.</li>
<li>Text type sorting uses a natural sort and is a tiny bit slower than a pure numeric sort.</li>
</ul>
</li>
</ul>
</div>
<h3>Add Parser Template</h3>
<pre class="prettyprint lang-javascript">$.tablesorter.addParser({
// use a unique id
id: 'myparser',
is: function(s, table, cell) {
// s is the text from the cell
// table is the current table (as a DOM element; not jQuery object)
// cell is the current table cell (DOM element)
// return false if you don't want this parser to be auto detected
return false;
},
format: function(s, table, cell, cellIndex) {
// s is the text from the cell
// table is the current table (as a DOM element; not jQuery object)
// cell is the current table cell (DOM element)
// cellIndex is the current cell's column index
// format your data for normalization
// (i.e. do something to get and/or modify your data, then return it)
return s;
},
// set the type to either numeric or text (text uses a natural sort function
// so it will work for everything, but numeric is faster for numbers
type: 'numeric'
});</pre>
2011-09-08 16:28:10 +00:00
2011-06-22 23:19:27 +00:00
<h1>Demo</h1>
2011-07-17 15:01:18 +00:00
<div id="demo"><table class="tablesorter">
<thead>
<tr>
2011-08-04 06:24:26 +00:00
<th class="sorter-text">Name</th>
2011-07-17 15:01:18 +00:00
<th>Major</th>
<th>Gender</th>
<th>English</th>
<th>Japanese</th>
<th>Calculus</th>
2011-08-04 06:24:26 +00:00
<th class="sorter-grades">Overall grades</th> <!-- set the column parser using a class name -->
2011-07-17 15:01:18 +00:00
</tr>
</thead>
<tbody>
2013-03-26 21:16:13 +00:00
<tr><td>Student01</td><td>Languages</td><td>male</td><td>80</td><td>70</td><td>75</td><td>medium</td></tr>
<tr><td>Student02</td><td>Mathematics</td><td>male</td><td>90</td><td>88</td><td>100</td><td>good</td></tr>
<tr><td>Student03</td><td>Languages</td><td>female</td><td>85</td><td>95</td><td>80</td><td>good</td></tr>
<tr><td>Student04</td><td>Languages</td><td>male</td><td>20</td><td>50</td><td>65</td><td>bad</td></tr>
<tr><td>Student05</td><td>Mathematics</td><td>female</td><td>70</td><td>78</td><td>70</td><td>medium</td></tr>
<tr><td>Student06</td><td>Mathematics</td><td>male</td><td>44</td><td>65</td><td>60</td><td>bad</td></tr>
2011-07-17 15:01:18 +00:00
</tbody>
</table></div>
2011-06-22 23:19:27 +00:00
<h1>Javascript</h1>
2013-03-26 21:16:13 +00:00
<h3>Grades Parser Code</h3>
2011-06-22 23:19:27 +00:00
<div id="javascript">
<pre class="prettyprint lang-javascript"></pre>
2011-06-22 23:19:27 +00:00
</div>
<h1>HTML</h1>
2013-03-26 21:16:13 +00:00
<p class="tip">
<em>NOTE!</em> Assigning the parser using a class name was added in version 2.0.11 (it is not part of the original plugin; use the <code>headers</code> option in older versions).
</p>
2011-06-22 23:19:27 +00:00
<div id="html">
<pre class="prettyprint lang-html"></pre>
2011-06-22 23:19:27 +00:00
</div>
2011-07-17 15:01:18 +00:00
<div class="next-up">
<hr />
2012-03-07 18:06:35 +00:00
Next up: <a href="example-parsers-advanced.html">Writing custom parsers, advanced &rsaquo;&rsaquo;</a>
2011-07-17 15:01:18 +00:00
</div>
2011-06-22 23:19:27 +00:00
</div>
</body>
</html>