<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Legato &#187; tablesorter</title>
	<atom:link href="http://www.paullegato.com/blog/tag/tablesorter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paullegato.com</link>
	<description></description>
	<lastBuildDate>Tue, 06 Dec 2011 00:52:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jQuery Tablesorter Absolute Value Sort</title>
		<link>http://www.paullegato.com/blog/jquery-tablesorter-absolute-value/</link>
		<comments>http://www.paullegato.com/blog/jquery-tablesorter-absolute-value/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 04:19:25 +0000</pubDate>
		<dc:creator>Paul Legato</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tablesorter]]></category>

		<guid isPermaLink="false">http://www.paullegato.com/?p=65</guid>
		<description><![CDATA[jQuery&#8217;s Tablesorter is easily customizable to sort by absolute value or anything else you&#8217;d like. In my case, I have a column of &#8220;standard deviations above or below the mean&#8221; that I want to display sorted by absolute value. This was pretty easy with a little Javascript code. Absolute value Tablesort parser code: How the [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery&#8217;s <a target="_blank" href="http://tablesorter.com/docs/" >Tablesorter</a> is easily customizable to sort by absolute value or anything else you&#8217;d like. In my case, I have a column of &#8220;standard deviations above or below the mean&#8221; that I want to display sorted by absolute value. This was pretty easy with a little Javascript code.<span id="more-65"></span></p>
<h1>Absolute value Tablesort parser code:</h1>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function() {
  $.tablesorter.addParser({
    id: 'abs',
    is: function(s) { return false; },
    format: function(s) { return Math.abs(s); },
    type: 'numeric'
  });&quot;

    $(&quot;#tablename&quot;).tablesorter(
      {
       headers:
       {
         5 : { sorter: &quot;abs&quot;  }
       }
     });
/* ... */
});
</pre>
<h2>How the custom Tablesort parser works</h2>
<p>jQuery&#8217;s &#8220;document loaded&#8221; event handler,  called <code>ready</code>, is grabbed in line 1. We pass it an anonymous function, which will be called when all of the document assets (images, embedded .js files, etc.) are done being downloaded.</p>
<p>The <code>$.tablesorter</code> call returns the global Tablesorter object. We can add custom parsers to it fairly easy, based on <a target="_blank" href="http://tablesorter.com/docs/example-parsers.html" >the example given in the Tablesorter documentation</a> and the default <a target="_blank" href="http://tablesorter.com/jquery.tablesorter.js"  class="broken_link">parsers included in the Tablesorter source code itself</a>.</p>
<p>Our parser is defined based on the hash given to <code>addParser()</code>. It will be called &#8220;<code>abs</code>&#8221; (line 3). The anonymous function labelled &#8220;<code>is</code>&#8221; (line 4) is called by Tablesorter as part of its auto-detection routine to determine whether this parser should be used for a given column of data. In our case, we don&#8217;t ever want the <code>abs</code> parser to be used automatically, only when specifically called for, so this function simply always returns false.</p>
<p>Line 5, &#8220;<code>format</code>&#8220;, is the heart of the parser. This translates the data into the format it will be sorted by: here, we can simply pass whatever is in the cell to the builtin <code>Math.abs</code> function to get its absolute value, and return that. (The name is somewhat misleading. &#8220;Format&#8221; implies that its return value will be used solely for visualization, when it will actually be performing data manipulation based on the result.)</p>
<p>Lastly, line 6 simply indicates that this is a <code>numeric</code> field.</p>
<h2>Applying the custom parser to a column</h1>
<p>Tablesorter&#8217;s &#8220;<a target="_blank" href="http://tablesorter.com/docs/example-meta-parsers.html" >setting parser by metadata</a>&#8221; stuff did not (easily) work with my customized parser, maybe because of load-order issues: it probably needs the custom parser to already be defined when the DOM is parsed or when the Tablesorter code is loaded, or something like that. No matter where I put the definition of my parser within the HTML page, it didn&#8217;t work. I tried it at the beginning, at the end, and in a <code>$(document).ready(</code> callback function before the call to <code>tablesorter()</code> itself. (Perhaps it would work if loaded in the header; I didn&#8217;t try that.)</p>
<p>Rather than wrestle with Tablesorter&#8217;s <code>class</code>-based parser specification further, I decided to simply add the custom parser manually to the <code>ready</code> function, in lines 9-15. This indicates that column 5 is to be sorted with the &#8220;<code>abs</code>&#8221; parser.</p>
<p>Specifying the column number numerically has obvious drawbacks in that if I ever add or subtract a column numbered 5 or less, or change their order, I have to go in here and change that number. Since my project is in its prototype phase, this is an acceptable tradeoff at this stage for the time saved by not figuring out why the <code>class</code> based selector doesn&#8217;t work. If this project gets promoted, I will have to revisit the issue.</p>
<img src="http://www.paullegato.com/?ak_action=api_record_view&id=65&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.paullegato.com/blog/jquery-tablesorter-absolute-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

