<?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>hocuspokus &#187; javascript</title>
	<atom:link href="http://hocuspokus.net/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://hocuspokus.net</link>
	<description>the life and times of a code monkey</description>
	<lastBuildDate>Tue, 01 Jun 2010 16:40:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Javascript to Make Relative Times Work With&#160;Caching</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2009%2F02%2Fusing-javascript-to-make-relative-times-work-with-caching&amp;seed_title=Using+Javascript+to+Make+Relative+Times+Work+With%26%23160%3BCaching</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2009%2F02%2Fusing-javascript-to-make-relative-times-work-with-caching&amp;seed_title=Using+Javascript+to+Make+Relative+Times+Work+With%26%23160%3BCaching#comments</comments>
		<pubDate>Thu, 05 Feb 2009 18:09:39 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/?p=303</guid>
		<description><![CDATA[A great idea from the folks over at 37 signals. Itâ€™s easy to think that the relative-time style of â€œthis comment was written 15 minutes agoâ€ is incompatible with caching. How are you supposed to cache something if the text &#8230; <a href="http://hocuspokus.net/feeder/?FeederAction=clicked&#38;feed=Articles+%28RSS2%29&#38;seed=http%3A%2F%2Fhocuspokus.net%2F2009%2F02%2Fusing-javascript-to-make-relative-times-work-with-caching&#38;seed_title=Using+Javascript+to+Make+Relative+Times+Work+With%26%23160%3BCaching">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A great idea from the folks over at 37 signals.</p>

<blockquote>
  <p>Itâ€™s easy to think that the relative-time style of â€œthis comment was written 15 minutes agoâ€ is incompatible with caching. How are you supposed to cache something if the text changes every minute? Static pages with JavaScripts, thatâ€™s how!</p>
</blockquote>

<p><a href="http://www.37signals.com/svn/posts/1557-javascript-makes-relative-times-compatible-with-caching" class="via">full post</a>.</p>

<p class="alert"><strong>Update:</strong> Looks like <a href="http://timeago.yarp.com/">this type of idea</a> has been around for a while (I&#8217;d just never thought of it before)! <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-smile-big.png' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2009%2F02%2Fusing-javascript-to-make-relative-times-work-with-caching&amp;seed_title=Using+Javascript+to+Make+Relative+Times+Work+With%26%23160%3BCaching/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blackbird - Javascript Logging&#160;Utility</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F10%2Fblackbird-javascript-logging-utility&amp;seed_title=Blackbird+-+Javascript+Logging%26%23160%3BUtility</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F10%2Fblackbird-javascript-logging-utility&amp;seed_title=Blackbird+-+Javascript+Logging%26%23160%3BUtility#comments</comments>
		<pubDate>Mon, 20 Oct 2008 08:03:18 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/?p=228</guid>
		<description><![CDATA[Found this while trawling the feeds this morning - looks like quite a useful utility. It offers a nice alternative to Firebug (when testing in browsers other than Firefox). Blackbird via]]></description>
			<content:encoded><![CDATA[<div style="text-align:center;"><a href="http://www.gscottolson.com/blackbirdjs/"><img src="http://hocuspokus.net/wp-content/uploads/2008/10/blackbird.png" alt="blackbird.png" border="0" width="292" height="279" /></a></div>

<p>Found this while trawling the feeds this morning - looks like quite a useful utility.  It offers a nice alternative to <a href="http://www.getfirebug.com/">Firebug</a> (when testing in browsers other than Firefox).</p>

<p><a href="http://www.gscottolson.com/blackbirdjs/" class="via">Blackbird</a> <a href="http://www.webappers.com/2008/10/15/blackbird-open-source-javascript-logging-utility/" class="via">via</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F10%2Fblackbird-javascript-logging-utility&amp;seed_title=Blackbird+-+Javascript+Logging%26%23160%3BUtility/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Release: toggleController Plugin&#160;1.0</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F10%2Frelease-togglecontroller-plugin-10&amp;seed_title=Release%3A+toggleController+Plugin%26%23160%3B1.0</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F10%2Frelease-togglecontroller-plugin-10&amp;seed_title=Release%3A+toggleController+Plugin%26%23160%3B1.0#comments</comments>
		<pubDate>Sat, 04 Oct 2008 18:02:21 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/?p=215</guid>
		<description><![CDATA[I&#8217;ve just spent the last week on a Javascript and Ajax course (thanks to work for footing the bill)! As such I come armed with some newfound javascript knowledge! Here&#8217;s the first fruits of my labour - a very simple &#8230; <a href="http://hocuspokus.net/feeder/?FeederAction=clicked&#38;feed=Articles+%28RSS2%29&#38;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F10%2Frelease-togglecontroller-plugin-10&#38;seed_title=Release%3A+toggleController+Plugin%26%23160%3B1.0">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just spent the last week on a Javascript and Ajax course (thanks to work for footing the bill)! As such I come armed with some newfound javascript knowledge! <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-smile.png' alt=':)' class='wp-smiley' /> </p>

<p>Here&#8217;s the first fruits of my labour - a very simple <a href="http://www.jquery.com">jQuery</a> plugin that enables you to unobtrusively make an element on a webpage collapsable, and have the showing/hiding controlled by another element.</p>

<p>Like I said, this is very basic stuff and there is probably about a million other (most likely more feature-full and better) plugins that do the same thing already on the web, but it&#8217;s all I really need for it to be useful for my stuff and I had fun learning how to do this - never know someone else may find it useful!</p>

<p>If you want to have a play, here&#8217;s where to go:</p>

<p><a href="http://hocuspokus.net/projects/jquery-plugins/">My Projects Page</a><br />
<a href="http://hocuspokus.net/stuff/jquerytogglecontrol/">Demo/Documentation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F10%2Frelease-togglecontroller-plugin-10&amp;seed_title=Release%3A+toggleController+Plugin%26%23160%3B1.0/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Default Text Field Values That Disappear on&#160;Focus</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F05%2Fdefault-text-field-values-that-disappear-on-focus&amp;seed_title=Default+Text+Field+Values+That+Disappear+on%26%23160%3BFocus</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F05%2Fdefault-text-field-values-that-disappear-on-focus&amp;seed_title=Default+Text+Field+Values+That+Disappear+on%26%23160%3BFocus#comments</comments>
		<pubDate>Fri, 16 May 2008 18:08:58 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[noteworthy]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/?p=146</guid>
		<description><![CDATA[You will have noticed this effect out on your travels on the internet - a text input field has some default text in it (often in a slightly dimmed colour), and when you click on the text box, default text &#8230; <a href="http://hocuspokus.net/feeder/?FeederAction=clicked&#38;feed=Articles+%28RSS2%29&#38;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F05%2Fdefault-text-field-values-that-disappear-on-focus&#38;seed_title=Default+Text+Field+Values+That+Disappear+on%26%23160%3BFocus">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>You will have noticed this effect out on your travels on the internet - a text input field has some default text in it (often in a slightly dimmed colour), and when you click on the text box, default text disappears and then reappears when you click away from the box.  It happens on the search box to the right here too.</p>

<p>I needed this same effect at work today so a quick Google came up with this great <a href="http://webdeveloper.beforeseven.com/jquery/default-text-field-value-disappears-focus">blog post</a> - a lovely example of the exact effect I was looking for in both raw javascript and a version using the <a href="http://jquery.com/">jQuery</a> library.</p>

<blockquote>
  <p>The script searches the page that it&#8217;s inserted on for all form input fields that have a class of &#8216;default-value&#8217; applied. Each form input field must also have a unique ID.</p>
  
  <p>When the page loads the script changes the color of the text in the text fields it has found to the value of &#8216;inactive&#95;color&#8217;. If the user clicks on the input field, the default text is blanked, and the colour changed to &#8216;active&#95;color&#8217;. If the user clicks away from the input field, i.e. the input field loses focus, the value of the text field will revert back to the original text, and the colour will change back to &#8216;inactive&#95;color&#8217;, unless the user has entered some other text.</p>
</blockquote>

<p>Here&#8217;s a copy of the jQuery version for my own personal notes, never know when that could be useful:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
 * Written by Rob Schmitt, The Web Developer's Blog
 * http://webdeveloper.beforeseven.com/
 */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> active_color <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#000'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Colour of user provided text</span>
<span style="color: #003366; font-weight: bold;">var</span> inactive_color <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#999'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Colour of default text</span>
&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input.default-value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">,</span> inactive_color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> default_values <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input.default-value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>default_values<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      default_values<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">==</span> default_values<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> active_color<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> inactive_color<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> default_values<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>However, we don&#8217;t use jQuery at work, our entire site is based around <a href="http://www.prototypejs.org/">Prototype</a> and <a href="http://script.aculo.us/">script.aculo.us</a>, so I did a bit of butchering and here&#8217;s the code adapted to use Prototype:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
 * Written by Darren Oakley
 * http://hocuspokus.net
 */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> active_color <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#000'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Colour of user provided text</span>
<span style="color: #003366; font-weight: bold;">var</span> inactive_color <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#999'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Colour of default text</span>
&nbsp;
Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span> window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> default_values <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input.default-value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> color<span style="color: #339933;">:</span> inactive_color <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'focus'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>default_values<span style="color: #009900;">&#91;</span>s.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                default_values<span style="color: #009900;">&#91;</span>s.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> s.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">value</span> <span style="color: #339933;">==</span> default_values<span style="color: #009900;">&#91;</span>s.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                s.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
                $<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> color<span style="color: #339933;">:</span> active_color <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            $<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'blur'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">value</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> color<span style="color: #339933;">:</span> inactive_color <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    s.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> default_values<span style="color: #009900;">&#91;</span>s.<span style="color: #660066;">id</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Now all you need to do is give your input text fields a unique id, and the class of &#8216;default-value&#8217;, then the script will take care of the rest.</p>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2008%2F05%2Fdefault-text-field-values-that-disappear-on-focus&amp;seed_title=Default+Text+Field+Values+That+Disappear+on%26%23160%3BFocus/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Flot: Pure Javascript Plotting&#160;Library</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F12%2Fflot-pure-javascript-plotting-library&amp;seed_title=Flot%3A+Pure+Javascript+Plotting%26%23160%3BLibrary</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F12%2Fflot-pure-javascript-plotting-library&amp;seed_title=Flot%3A+Pure+Javascript+Plotting%26%23160%3BLibrary#comments</comments>
		<pubDate>Thu, 13 Dec 2007 21:46:28 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/2007/12/13/flot-pure-javascript-plotting-library/</guid>
		<description><![CDATA[Yet more chart drawing goodness. This takes a different approach to the Google model by generating really good looking graphs all on the client-side in javascript - very nice! Flot is a pure Javascript plotting library for jQuery. It produces &#8230; <a href="http://hocuspokus.net/feeder/?FeederAction=clicked&#38;feed=Articles+%28RSS2%29&#38;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F12%2Fflot-pure-javascript-plotting-library&#38;seed_title=Flot%3A+Pure+Javascript+Plotting%26%23160%3BLibrary">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Yet more chart drawing goodness.  This takes a different approach to the Google model by generating really good looking graphs all on the client-side in javascript - very nice!</p>

<blockquote>
  <p><a href="http://code.google.com/p/flot/">Flot</a> is a pure Javascript plotting library for jQuery. It produces graphical plots of arbitrary datasets on-the-fly client-side. The focus is on simple usage (all settings are optional), attractive looks and interactive features like zooming. Although Flot is easy to use, it is also advanced enough to be suitable for Web 2.0 data mining/business intelligence purposes which is its original application. The plugin is targeting all newer browsers.</p>
</blockquote>

<p><a href="http://www.webappers.com/2007/12/06/flot-pure-javascript-plotting-library-for-jquery/" class="via">via</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F12%2Fflot-pure-javascript-plotting-library&amp;seed_title=Flot%3A+Pure+Javascript+Plotting%26%23160%3BLibrary/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding/Deleting Rows in TableKit Tables&#160;Revisited</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F11%2Faddingdeleting-rows-in-tablekit-tables-revisited&amp;seed_title=Adding%2FDeleting+Rows+in+TableKit+Tables%26%23160%3BRevisited</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F11%2Faddingdeleting-rows-in-tablekit-tables-revisited&amp;seed_title=Adding%2FDeleting+Rows+in+TableKit+Tables%26%23160%3BRevisited#comments</comments>
		<pubDate>Thu, 01 Nov 2007 08:42:51 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[noteworthy]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[catalyst]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[tablekit]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/2007/11/01/addingdeleting-rows-in-tablekit-tables-revisited/</guid>
		<description><![CDATA[TableKit is a great javascript library for making your HTML tables fully editable. However, one problem is that you can&#8217;t add or delete rows from the tables&#8230; I came up with a solution to this not so long ago, but &#8230; <a href="http://hocuspokus.net/feeder/?FeederAction=clicked&#38;feed=Articles+%28RSS2%29&#38;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F11%2Faddingdeleting-rows-in-tablekit-tables-revisited&#38;seed_title=Adding%2FDeleting+Rows+in+TableKit+Tables%26%23160%3BRevisited">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.millstream.com.au/view/code/tablekit/">TableKit</a> is a great javascript library for making your HTML tables fully editable.  However, one problem is that you can&#8217;t add or delete rows from the tables&#8230;</p>

<p>I came up with a solution to this <a href="http://hocuspokus.net/2007/10/11/adding-deleting-rows-to-tablekit-tables/">not so long ago</a>, but it still had a problem - TableKit caches the tables on loading, so after we update the table body (adding or deleting a row) the sorting and editing of the table is completely screwed! <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-sad.png' alt=':(' class='wp-smiley' />  However, with a little more work, and some help from one of the guys in the office - we&#8217;ve finally got this cracked! <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-smile.png' alt=':)' class='wp-smiley' /> </p>

<p><span id="more-50"></span></p>

<p>The basic idea to the fix is, instead of updating the table body, replace the entire table with a new one and get TableKit to do its stuff on the new table.  Although this is not the ideal solution<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> - this <em>does</em> work quite well.  Here&#8217;s the details.<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup></p>

<p>First, we&#8217;ll update the code from our template file (/root/src/users/list.tt):</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;">[% META title = 'User List' -%]
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;users_div&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;users&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sortable resizable editable&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sortfirstasc noedit&quot;</span>&gt;</span>Id<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstname&quot;</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lastname&quot;</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;noedit nocol&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tfoot</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Id<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nocol&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tfoot</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tbody</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_body&quot;</span>&gt;</span>
    [% FOREACH user IN users -%]
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'[% user.id %]'</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.id %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.firstname %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.lastname %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nocol&quot;</span>&gt;</span>
          <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;delete&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;deleteUser([% user.id %]); return false&quot;</span>&gt;</span>delete<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    [% END -%]
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;addUser(); return false&quot;</span>&gt;</span>add a user<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>


<p>Then add this javascript code to the bottom of the same file:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> charset<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;utf-8&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #003366; font-weight: bold;">var</span> users_table <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TableKit<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'users'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    editAjaxURI<span style="color: #339933;">:</span> <span style="color: #3366CC;">'[% c.uri_for('</span><span style="color: #339933;">/</span>users<span style="color: #339933;">/</span>_update_user<span style="color: #3366CC;">') %]'</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> addUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> timestamp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> new_table <span style="color: #339933;">=</span> <span style="color: #3366CC;">'users'</span> <span style="color: #339933;">+</span> timestamp<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'[% c.uri_for('</span><span style="color: #339933;">/</span>users<span style="color: #339933;">/</span>_add_user<span style="color: #3366CC;">') %]?timestamp='</span> <span style="color: #339933;">+</span> timestamp<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Updater</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'users_div'</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> 
      asynchronous<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      onComplete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">new</span> TableKit<span style="color: #009900;">&#40;</span> new_table<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
          editAjaxURI<span style="color: #339933;">:</span> <span style="color: #3366CC;">'[% c.uri_for('</span><span style="color: #339933;">/</span>users<span style="color: #339933;">/</span>_update_user<span style="color: #3366CC;">') %]'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">function</span> deleteUser<span style="color: #009900;">&#40;</span> user_id <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> timestamp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> new_table <span style="color: #339933;">=</span> <span style="color: #3366CC;">'users'</span> <span style="color: #339933;">+</span> timestamp<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'[% c.uri_for('</span><span style="color: #339933;">/</span>users<span style="color: #339933;">/</span>_delete_user<span style="color: #339933;">/</span><span style="color: #3366CC;">') %]?user_id='</span> <span style="color: #339933;">+</span> user_id <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&amp;timestamp='</span> <span style="color: #339933;">+</span> timestamp<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> answer <span style="color: #339933;">=</span> <span style="color: #000066;">confirm</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Are you sure you want to delete this user?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>answer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Updater</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'users_div'</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> 
        asynchronous<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        onComplete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">new</span> TableKit<span style="color: #009900;">&#40;</span> new_table<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
            editAjaxURI<span style="color: #339933;">:</span> <span style="color: #3366CC;">'[% c.uri_for('</span><span style="color: #339933;">/</span>users<span style="color: #339933;">/</span>_update_user<span style="color: #3366CC;">') %]'</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>


<p>The main differences in the above from last time round are as follows:
-   The &#8216;users&#8217; table is now inside a div with the id &#8216;users_div&#8217;
-   The link to add a user now calls a javascript function rather than an   Ajax.Updater call directly.
-   The functions to add and delete users are essentially the same:
- -         They create a timestamp to individually identify the new table we       are going to create (this is <strong>the</strong> most important thing here to       remember)!
- -         They then use an Ajax.Updater call to talk to the Perl controller       and generate the new table with a row added or removed.
- -         Once the Ajax.Updater is complete, TableKit is then called again        to make our new table editable and sortable.</p>

<p>That&#8217;s the template sorted, now let&#8217;s look at the controller (/lib/MyApp/Controller/Users.pm):</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> MyApp<span style="color: #339933;">::</span><span style="color: #006600;">Controller</span><span style="color: #339933;">::</span><span style="color: #006600;">Users</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> base <span style="color: #ff0000;">'Catalyst::Controller'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head1 NAME
&nbsp;
MyApp::Controller::Users - Catalyst Controller
&nbsp;
=head1 DESCRIPTION
&nbsp;
Catalyst Controller.
&nbsp;
=head1 METHODS
&nbsp;
=cut</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head2 index 
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000066;">index</span> <span style="color: #339933;">:</span> Private <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'/users/list'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head2 list
&nbsp;
Fetch all user objects and pass to users/list.tt in stash to be displayed
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> list <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>users<span style="color: #009900;">&#125;</span>    <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">all</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>template<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'users/list.tt'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head2 _update_user
&nbsp;
Ajax method to update the users table
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> _update_user <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">find</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> id <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>id<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #006600;">update</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>field<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>value<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">body</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>value<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head2 _delete_user
&nbsp;
Ajax method to delete users from the users table
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> _delete_user <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Look-up our user entry</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$user</span> <span style="color: #339933;">=</span>
      <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">find</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> id <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>user_id<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #006600;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span>
      __return_all_users<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>timestamp<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'null'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">body</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head2 _add_user
&nbsp;
Ajax method to add users to the users table
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> _add_user <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># create a new user...</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$new_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span>
        <span style="color: #009900;">&#123;</span>
            id        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>user_id<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            firstname <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'[First Name]'</span><span style="color: #339933;">,</span>
            lastname  <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'[Last Name]'</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span> __return_all_users<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>timestamp<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">$new_user</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">body</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head2 __return_all_users
&nbsp;
Private method for the Users ajax interaction.
Returns a html table.
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> __return_all_users <span style="color: #339933;">:</span> Private <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$timestamp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$new_user_id</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@users</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">all</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$html</span> <span style="color: #339933;">=</span>
        <span style="color: #ff0000;">'&lt;table id=&quot;users'</span>
      <span style="color: #339933;">.</span> <span style="color: #0000ff;">$timestamp</span>
      <span style="color: #339933;">.</span> <span style="color: #ff0000;">'&quot; class=&quot;sortable resizable editable&quot;&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th id=&quot;id&quot; class=&quot;sortfirstasc noedit&quot;&gt;Id&lt;/th&gt;
          &lt;th id=&quot;firstname&quot;&gt;First Name&lt;/th&gt;
          &lt;th id=&quot;lastname&quot;&gt;Last Name&lt;/th&gt;
          &lt;th class=&quot;noedit nocol&quot;&gt;&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tfoot&gt;
        &lt;tr&gt;
          &lt;th&gt;Id&lt;/th&gt;
          &lt;th&gt;First Name&lt;/th&gt;
          &lt;th&gt;Last Name&lt;/th&gt;
          &lt;th class=&quot;nocol&quot;&gt;&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/tfoot&gt;
      &lt;tbody id=&quot;user_body&quot;&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">scalar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@users</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000066;">scalar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@users</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$new_user_id</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$new_user_id</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'new'</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span>   <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'rowodd'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span>                 <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'roweven'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #0000ff;">$html</span> <span style="color: #339933;">.=</span>
                <span style="color: #ff0000;">&quot;&lt;tr class=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> 
              <span style="color: #339933;">.</span> <span style="color: #0000ff;">$class</span>
              <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>
              <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
                &lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;&lt;/td&gt;
                &lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">firstname</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;&lt;/td&gt;
                &lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">lastname</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;&lt;/td&gt;
                &lt;td class=<span style="color: #000099; font-weight: bold;">\&quot;</span>nocol<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
                    &lt;a class=<span style="color: #000099; font-weight: bold;">\&quot;</span>delete<span style="color: #000099; font-weight: bold;">\&quot;</span> href=<span style="color: #000099; font-weight: bold;">\&quot;</span>#<span style="color: #000099; font-weight: bold;">\&quot;</span> onclick=<span style="color: #000099; font-weight: bold;">\&quot;</span> deleteUser(&quot;</span>
              <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;); return false<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;delete&lt;/a&gt;
                &lt;/td&gt;
              &lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$html</span> <span style="color: #339933;">.=</span>
          <span style="color: #ff0000;">'&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;nocol&quot;&gt;All Users Deleted&lt;/td&gt;&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">'&lt;/tbody&gt;&lt;/table&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head1 AUTHOR
&nbsp;
Darren Oakley
&nbsp;
=head1 LICENSE
&nbsp;
This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.
&nbsp;
=cut</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>The only real changes in the controller are a general code clean-up, and a slight change to the &#8216;__return_all_users&#8217; method - this now returns a whole table (identified by the unique timestamped id that we generated in the javascript functions).  Nothing really needed changing in the back-end.</p>

<p>There you go!  Now our TableKit based tables can be used to not only edit entries in the database, we can now add and remove them! <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-smile.png' alt=':)' class='wp-smiley' /> </p>

<p>The files used in this example can be found below.</p>

<h4>Attached Files:</h4>

<p><a href="http://hocuspokus.net/wp-content/uploads/2007/11/myapp.zip" title="myapp.zip" title="MyApp - v3" class="archive">MyApp.zip - v3</a></p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>That would be knowing more about javascript and the dom so that we can make changes to TableKits cache as we update the table body (thus keeping the original table).&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:2">
<p>We&#8217;re using the code from the <a href="http://hocuspokus.net/2007/10/11/adding-deleting-rows-to-tablekit-tables/">previous post</a> as a starting point.&#160;<a href="#fnref:2" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F11%2Faddingdeleting-rows-in-tablekit-tables-revisited&amp;seed_title=Adding%2FDeleting+Rows+in+TableKit+Tables%26%23160%3BRevisited/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Adding/Deleting Rows in TableKit&#160;Tables</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F10%2Fadding-deleting-rows-to-tablekit-tables&amp;seed_title=Adding%2FDeleting+Rows+in+TableKit%26%23160%3BTables</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F10%2Fadding-deleting-rows-to-tablekit-tables&amp;seed_title=Adding%2FDeleting+Rows+in+TableKit%26%23160%3BTables#comments</comments>
		<pubDate>Thu, 11 Oct 2007 19:50:10 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[noteworthy]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[catalyst]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[tablekit]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/2007/10/11/adding-deleting-rows-to-tablekit-tables/</guid>
		<description><![CDATA[This post has now been updated. Please head over to the new post for something better&#8230; Following on from my previous post on how to integrate the excellent TableKit into your Catalyst webapp (to make your data tables dynamically editable), &#8230; <a href="http://hocuspokus.net/feeder/?FeederAction=clicked&#38;feed=Articles+%28RSS2%29&#38;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F10%2Fadding-deleting-rows-to-tablekit-tables&#38;seed_title=Adding%2FDeleting+Rows+in+TableKit%26%23160%3BTables">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p class="alert">This post has now been <a href="http://hocuspokus.net/2007/11/01/addingdeleting-rows-in-tablekit-tables-revisited/">updated</a>. Please head over to the new post for something better&#8230;</p>

<p>Following on from my <a href="http://hocuspokus.net/2007/09/23/making-editable-tables-with-catalyst-and-tablekit/">previous post</a> on how to integrate the excellent <a href="http://www.millstream.com.au/view/code/tablekit/">TableKit</a> into your <a href="http://www.catalystframework.org/">Catalyst</a> webapp (to make your data tables dynamically editable),  here&#8217;s how i&#8217;ve gone about adding ajax inserts and deletes so that you can add and remove data rows in your tables.<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup></p>

<p><span id="more-31"></span></p>

<p>We&#8217;re going to be using prototype to do the background ajax calls, as this is already in our app as a requirement of TableKit.  Now let&#8217;s get on with doctoring our template.  First we add in a delete button for each row of our table.  Open up <code>/root/src/users/list.tt</code> and change the code of our table to look like the following (the changes come on lines 9, 17, 20 and 26-28):</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;users&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sortable resizable editable&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sortfirstasc noedit&quot;</span>&gt;</span>Id<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstname&quot;</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lastname&quot;</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;noedit nocol&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tfoot</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Id<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nocol&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tfoot</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tbody</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_body&quot;</span>&gt;</span>
  [% FOREACH user IN users -%]
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'[% user.id %]'</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.id %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.firstname %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.lastname %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nocol&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;delete&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;userDelete([% user.id %]); return false&quot;</span>&gt;</span>delete<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
  [% END -%]
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></td></tr></table></div>


<p>The above code adds a &#8216;delete&#8217; link into each row of the table.  This link is to run a short javascript function (called <code>userDelete</code>) that we shall add now at the bottom of the template (within the original javascript block):</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">userDelete <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> user_id <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> answer <span style="color: #339933;">=</span> <span style="color: #000066;">confirm</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Are you sure you want to delete this user?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>answer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #660066;">Updater</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'user_body'</span><span style="color: #339933;">,</span> 
      <span style="color: #3366CC;">'[% c.uri_for('</span><span style="color: #339933;">/</span>users<span style="color: #339933;">/</span>_delete_user<span style="color: #339933;">/</span><span style="color: #3366CC;">') %]?user_id='</span> <span style="color: #339933;">+</span> user_id<span style="color: #339933;">,</span> 
      <span style="color: #009900;">&#123;</span> asynchronous<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>This function basically asks the user to confirm that they want to delete the user entry, the sets up an Ajax.Updater that passes the <code>user_id</code> to be deleted to a new function in our controller (that we shall write next) which deletes it, and then passes the new table body back to be updated on screen.  Let&#8217;s now create a new function <code>_delete_user</code> (and a helper function <code>__return_all_users</code>) in our user controller <code>/lib/MyApp/Controller/Users.pm</code>:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">=head _delete_user
&nbsp;
Ajax method to delete users from the users table
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> _delete_user <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Look-up our user entry</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      id <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>user_id<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span> __return_all_users<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'null'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$output</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;nocol&quot;&gt;All Users Deleted&lt;/td&gt;&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">body</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$output</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">=head2 __return_all_users
&nbsp;
Private method for the Users ajax interaction.
Returns the inner body of a html table.
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> __return_all_users <span style="color: #339933;">:</span> Private <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$new_user_id</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@users</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">all</span><span style="color: #009900;">&#40;</span>
        <span style="color: #009900;">&#123;</span> order_by <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'id ASC'</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$html</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000066;">scalar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@users</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$class</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$new_user_id</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$new_user_id</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'new'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'rowodd'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$class</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'roweven'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;&lt;tr class=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$class</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
            &lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;&lt;/td&gt;
            &lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">firstname</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;&lt;/td&gt;
            &lt;td&gt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">lastname</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;&lt;/td&gt;
            &lt;td class=<span style="color: #000099; font-weight: bold;">\&quot;</span>nocol<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;
                &lt;a class=<span style="color: #000099; font-weight: bold;">\&quot;</span>delete<span style="color: #000099; font-weight: bold;">\&quot;</span> href=<span style="color: #000099; font-weight: bold;">\&quot;</span>#<span style="color: #000099; font-weight: bold;">\&quot;</span> onclick=<span style="color: #000099; font-weight: bold;">\&quot;</span> userDelete(&quot;</span>
                <span style="color: #339933;">.</span> <span style="color: #0000ff;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;); return false<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;delete&lt;/a&gt;
            &lt;/td&gt;
            &lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$html</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>The <code>_delete_user</code> function, takes the <code>user_id</code> that we sent to it, deletes the entry from the database, then calls the <code>__return_all_users</code> function to rebuild the table body.  This table body is then used to update the table back on the web page.</p>

<p>That&#8217;s the delete functionality sorted, now on with the row adding&#8230;  Back in the template, add we add another Ajax.Updater link below the table:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>33
34
35
36
37
38
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot; </span>
<span style="color: #009900;">  new Ajax.Updater( 'user_body',</span>
<span style="color: #009900;">    '[% c.uri_for('/users/_add_user') %]', </span>
<span style="color: #009900;">    { asynchronous: 1, }</span>
<span style="color: #009900;">  ); return false &quot;</span>&gt;</span>add a user<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>


<p>And add another new method into our controller to add a new user into the database and table:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">=head _add_user
&nbsp;
Ajax method to add users to the users table
&nbsp;
=cut</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> _add_user <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># create a new user...</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$new_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span>
        <span style="color: #009900;">&#123;</span>
            id        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>user_id<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            firstname <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'[First Name]'</span><span style="color: #339933;">,</span>
            lastname  <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'[Last Name]'</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$output</span> <span style="color: #339933;">=</span> __return_all_users<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$new_user</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">id</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">body</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$output</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>This function, like the delete function, does its business on the database, then rebuilds the table body and sends it back to the web page.</p>

<p>The final icing on the cake, is a little addition to our CSS to make this all look a little nicer.  Open up <code>/root/css/myapp.css</code> and add the following to the bottom of the file:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.nocol</span><span style="color: #00AA00;">,</span> th<span style="color: #6666ff;">.nocol</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
tr<span style="color: #6666ff;">.new</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ff9697</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">outline</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.add</span><span style="color: #00AA00;">,</span> a<span style="color: #6666ff;">.delete</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/images/add.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">2px</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.delete</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/images/delete.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
tr a<span style="color: #6666ff;">.delete</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0.8em</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>


<p>And there you go.  Now you can edit, add and delete users to your database and table without having to reload the page.</p>

<div style="text-align:center;"><img src="http://hocuspokus.net/wp-content/uploads/2007/10/add-remove-tablekit.png" alt="add-remove-tablekit.png" border="0" width="449" height="535" /></div>

<p>However, there is one small bug that needs to be squashed to make this perfect&#8230;  TableKit caches the tables on page load - if you have a go at re-sorting one of the tables after deleting or adding a row, this causes the table to essentially double in size as the original cached data is reloaded onto the screen! <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-sad.png' alt=':(' class='wp-smiley' />  I now need to figure out a way to clean out the cache for the current table and re-initalise TableKit.  Any suggestions would be more than welcome! <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-smile.png' alt=':)' class='wp-smiley' /> </p>

<p>The files used in the examples can be downloaded below.</p>

<h4>Attached Files:</h4>

<p><a href='http://hocuspokus.net/wp-content/uploads/2007/10/myapp.zip' title='MyApp - v2' class="archive">MyApp - v2</a></p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>You&#8217;ll need the code from the <a href="http://hocuspokus.net/2007/09/23/making-editable-tables-with-catalyst-and-tablekit/">previous post</a> to follow along.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F10%2Fadding-deleting-rows-to-tablekit-tables&amp;seed_title=Adding%2FDeleting+Rows+in+TableKit%26%23160%3BTables/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making Editable Tables With Catalyst and&#160;TableKit</title>
		<link>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F09%2Fmaking-editable-tables-with-catalyst-and-tablekit&amp;seed_title=Making+Editable+Tables+With+Catalyst+and%26%23160%3BTableKit</link>
		<comments>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F09%2Fmaking-editable-tables-with-catalyst-and-tablekit&amp;seed_title=Making+Editable+Tables+With+Catalyst+and%26%23160%3BTableKit#comments</comments>
		<pubDate>Sun, 23 Sep 2007 08:55:45 +0000</pubDate>
		<dc:creator>Daz</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[noteworthy]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[catalyst]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[tablekit]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://hocuspokus.net/2007/09/23/making-editable-tables-with-catalyst-and-tablekit/</guid>
		<description><![CDATA[Catalyst is an MVC web-development framework for Perl, very similar in concepts, ideas (and even some of the implementation) to Ruby on Rails. I&#8217;m using Catalyst a lot in my job now, and one of the first challenges that i&#8217;ve &#8230; <a href="http://hocuspokus.net/feeder/?FeederAction=clicked&#38;feed=Articles+%28RSS2%29&#38;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F09%2Fmaking-editable-tables-with-catalyst-and-tablekit&#38;seed_title=Making+Editable+Tables+With+Catalyst+and%26%23160%3BTableKit">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.catalystframework.org/">Catalyst</a> is an MVC web-development framework for Perl, very similar in concepts, ideas (and even some of the implementation) to <a href="http://rubyonrails.org/">Ruby on Rails</a>.  I&#8217;m using Catalyst a lot in my job now, and one of the first challenges that i&#8217;ve had to go through is making dynamically editable tables for some of the views we are using (in other words, Ajax driven edit-in-place tables).</p>

<p>Following a short Google, there seemed to be only two ready-made options I could see: one, using the Catalyst controller module <a href="http://search.cpan.org/~karman/Catalyst-Controller-Rose-0.04/lib/Catalyst/Controller/Rose/EIP.pm">Catalyst::Controller::ROSE::EIP</a>; or two, by integrating and using the stand-alone javascript package <a href="http://www.millstream.com.au/view/code/tablekit/">TableKit</a>.</p>

<p><span id="more-9"></span></p>

<p>Unfortunately the first option, ROSE::EIP is dependent on the use of the <a href="http://search.cpan.org/~jsiracusa/Rose-DB-Object-0.765/lib/Rose/DB/Object.pm">Rose::DB::Object</a> as your database ORM mapper. As our webapp is already using <a href="http://search.cpan.org/~ash/DBIx-Class-0.08007/lib/DBIx/Class.pm">DBIx::Class</a> as its ORM layer, that rules ROSE::EIP out and TableKit was the way to go.  Here&#8217;s a quick rundown of how I got it going, using a very simple table to store a users first and last names as an example (all files used in this tutorial are available for download at the bottom of this post) and a single controller called &#8216;users&#8217;.</p>

<p>First off, head on over to the <a href="http://www.millstream.com.au/view/code/tablekit/">TableKit</a> site and download the latest TableKit package, then create the following three directories in your <code>/MyApp/root/</code> directory: <em>javascript</em>, <em>css</em>, and <em>images</em>.</p>

<p>From the TableKit download, copy the files: <em>tablekit.js</em>, <em>fastinit.js</em> and <em>prototype.js</em> (found in scriptaculous/lib) into your new &#8216;<em>javascript</em>&#8217; directory; then copy the two images - <em>down.gif</em> and <em>up.gif</em> into the <em>images</em> directory.</p>

<p>Now create a new file and add in the following CSS code:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">table <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border-collapse</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">collapse</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
td<span style="color: #00AA00;">,</span> th <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0.5em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
thead<span style="color: #00AA00;">,</span> tfoot <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#DDD</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
thead <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#f08900</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
tfoot <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#f08900</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
tr<span style="color: #6666ff;">.rowodd</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
tr<span style="color: #6666ff;">.roweven</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#F2F2F2</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.sortcol</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.sortasc</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#DDFFAC</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/images/up.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.sortdesc</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#B9DDFF</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/images/down.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.nosort</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">default</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
th<span style="color: #6666ff;">.resize-handle-active</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">e-resize</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
div<span style="color: #6666ff;">.resize-handle</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">e-resize</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">dashed</span> <span style="color: #cc00cc;">#1E90FF</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>


<p>Save this file as &#8216;<em>myapp.css</em>&#8217; in your new <em>css</em> directory.  This will give your tables a on okay looking starting point and nice striping on odd and even rows when we&#8217;re finished.</p>

<p>Next, open up the <code>/MyApp/root/lib/site/html</code> file and add the following lines within the head tags:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/css/myapp.css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/javascript/prototype.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/javascript/fastinit.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/javascript/tablekit.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>


<p>Now, we update our <code>/MyApp/root/src/users/list.tt</code> file so that the users table looks like the following snippet of code:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;">[% META title = 'User List' -%]
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;users&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sortable resizable editable&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sortfirstasc noedit&quot;</span>&gt;</span>Id<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstname&quot;</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lastname&quot;</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tfoot</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Id<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>First Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Last Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tfoot</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
        [% FOREACH user IN users -%]
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;[% user.id %]&quot;</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.id %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.firstname %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>[% user.lastname %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
        [% END -%]
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></td></tr></table></div>


<p>In the above lines of code there are two important details that need to be done in order for the table sorting and editing to work:</p>

<p><em>One: The id properties</em></p>

<p>The id properties on each column of the table are set to the same names as the fields in the database table. and the table rows, (in the body of the table) also have an id associated with them - this becomes the database row &#8216;ID&#8217; attribute when the template is interpreted.  These id&#8217;s then become some of the parameters that are passed via TableKit to our Ajax.Updater controller method.</p>

<p><em>Two: The class properties</em></p>

<p>In the table header, the id column has two classes applied to it - &#8216;sortfirstasc&#8217; and &#8216;noedit&#8217; - this tells TableKit to first sort the table based on this column (and ascending), and that the values of this column are not to be editable.</p>

<p>Also in the declaration of the table, the table has the classes: &#8216;sortable&#8217; &#8216;resizable&#8217; and &#8216;editable&#8217; - these tell TableKit what you&#8217;d like to be able to do with your table.</p>

<p>Finally, add the following snippet of Javascript code to the bottom of the same file (<code>/MyApp/root/src/users/list.tt</code>):</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> charset<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;utf-8&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #003366; font-weight: bold;">var</span> comments_table <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TableKit<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'users'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        editAjaxURI<span style="color: #339933;">:</span> <span style="color: #3366CC;">'[% update_uri %]'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>


<p>This initializes the TableKit actions and tells TableKit the uri that the Prototype Ajax.Updater is to call (a Catalyst controller method that we&#8217;re about to create).</p>

<p>The final step is now back in the &#8216;Users&#8217; controller.  Here&#8217;s the code that we need for both the initial list of users and the Ajax.Updater method that TableKit calls:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> list <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>users<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">all</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>template<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'users/list.tt'</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">stash</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>update_uri<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">uri_for</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'/users/_update_user'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> _update_user <span style="color: #339933;">:</span> Local <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MyAppDB::Users'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #006600;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> id <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>id<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #006600;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>field<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>value<span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">body</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">params</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>value<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>There you go, you should now have a table on your &#8216;/users/list&#8217; page that is sortable, resizable, and now even in-place editable.  I hope these instructions are clear enough (there&#8217;s quite a lot in there!), any questions, comments or improvements people could give me would be most appreciated.</p>

<p>Next up, i&#8217;ll go through how I made the tables more functional by adding in add and delete buttons (to add and delete rows to the table, and therefore create new database entries). <img src='http://hocuspokus.net/wp-content/plugins/smilies-themer/tango/face-smile.png' alt=':)' class='wp-smiley' /> </p>

<h4>Attached Files:</h4>

<p><a href='http://hocuspokus.net/wp-content/uploads/2007/09/myapp.zip' title='MyApp' class="archive">MyApp</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hocuspokus.net/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fhocuspokus.net%2F2007%2F09%2Fmaking-editable-tables-with-catalyst-and-tablekit&amp;seed_title=Making+Editable+Tables+With+Catalyst+and%26%23160%3BTableKit/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->