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 -->
2014-05-21 22:09:23 +00:00
< script src = "js/jquery-latest.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" >
2013-01-26 15:21:13 +00:00
< 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" > / / a d d p a r s e r t h r o u g h t h e t a b l e s o r t e r a d d P a r s e r m e t h o d
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" >
2014-02-01 16:19:26 +00:00
Notes about the < code > addParser< / code > template:
2013-03-26 21:16:13 +00:00
< 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 >
2014-02-19 10:28:45 +00:00
< li > The < code > parsed< / code > block (added < span class = "version" > v2.15.0< / span > )
< ul >
< li > This parameter is a flag used by the filter widget.< / li >
< li > When < code > true< / code > , the filter widget will only search through parsed data for matches.< / li >
< li > If < code > false< / code > (default value), the filter widget will search through the cell text for matches.< / li >
< li > Currently, only the parsers for inputs, checkboxes and selects have this parameter set to < code > true< / code > .< / li >
< / ul >
< / li >
2013-03-26 21:16:13 +00:00
< 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;
},
2014-02-19 10:28:45 +00:00
// flag for filter widget (true = ALWAYS search parsed values; false = search cell text)
parsed: false,
2013-03-26 21:16:13 +00:00
// 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" >
2013-01-26 15:21:13 +00:00
< 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" >
2013-01-26 15:21:13 +00:00
< 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 › › < / a >
2011-07-17 15:01:18 +00:00
< / div >
2011-06-22 23:19:27 +00:00
< / div >
< / body >
< / html >