<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Bart G. Dev...: Protoype Effects with CSS and Tables</title>
    <link>http://bartgdev.com/articles/2006/08/08/protoype-effects-with-css-and-tables</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>&lt;b&gt;Dev&lt;/b&gt;eloping well &lt;b&gt;dev&lt;/b&gt;ised applications</description>
    <item>
      <title>Protoype Effects with CSS and Tables</title>
      <description>&lt;p&gt;One thing I’ve been struggling with the last day was how to incorporate Prototypes Effects into an HTML table.  I need a well formatted list of records, something easily done with a &lt;code&gt;&amp;#60;table&amp;#62;&lt;/code&gt;, but something that will take advantage of AJAXian effects.  &lt;a href="http://bartgdev.com/public_demos/public/admin/list"&gt;Here's&lt;/a&gt; the final product.  Read on to see how I got it.
&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;The problem is that IE doesn’t remove a table’s elements out of the DOM, and as of recently, Firefox doesn’t either, unless you walk through the DOM.  There’s a great description of how to delete records through the DOM using JavaScript &lt;a href="http://mredkj.com/tutorials/tabledeleterow.html"&gt;here&lt;/a&gt;.  So to avoid tables, and for example use Effect.Fade to remove a record on deletion, I’d have to use pure CSS and struggle with the cross-browser inconsistencies.  Searching the web, I found some &lt;a href="http://www.glish.com/css"&gt;great techniques&lt;/a&gt; for dealing with these issues, but not being a CSS god, I needed a simpler solution.  Another great tool for record handling is &lt;a href="http://wiki.script.aculo.us/scriptaculous/show/Sortables"&gt;SortTables&lt;/a&gt; from script.aculo.us.  It allows you to move the records around.  But still, not what I’m looking for.
&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;So to only use Prototype (actually Effect.Fade is &lt;a href="http://script.aculo.us/"&gt;script.aculo.us&lt;/a&gt;, which is built on top of Prototype), I found that a combination of CSS and tables does the trick.  What you need is a CSS Record &lt;code&gt;id&lt;/code&gt;, and a CSS Column &lt;code&gt;class&lt;/code&gt; element.  Then create a table for each row in your database, assigning the entire table to your CSS Record id.  You can equally wrap the entire table into a div.   Next, set each &lt;code&gt;&amp;#60;td&amp;#62;&lt;/code&gt;’s class to your Column Class.  The key is to set the columns &lt;code&gt;width:&lt;/code&gt; property in order to have a uniform layout.  If each column’s width is the same, you only need one of these defined in CSS.  For my example below, each column is different, so I defined 4 different column classes.  
&lt;/p&gt;&lt;br /&gt;

&lt;p&gt;&lt;a href="http://bartgdev.com/public_demos/public/admin/list"&gt;Here&lt;/a&gt;, I list User objects, with the option to Delete each record.  I use &lt;code&gt;link_to_remote&lt;/code&gt; with Effect.Fade to delete the record from the database, then fade the record out from the DOM.
&lt;/p&gt;&lt;br /&gt;

If you’re using scaffold, here’s what you need:&lt;br /&gt;
&lt;h4&gt;CSS content&lt;/h4&gt;
&lt;h4&gt;File:  scaffold.css&lt;/h4&gt;
&lt;pre&gt;
#cssRecord{
  width: 700px;
  border-top:   1px solid #000000;
  border-left:  1px solid #000000;
  border-right: 1px solid #000000;
  background:   #ffffff;
}

#cssRecord th { 
  background: #dddddd; 
  font-size: 14px; 
  vertical-align: top; 
  text-align: left; 
}

#cssRecord td { 
  background: #eeeeee; 
  font-size: 12px; 
  vertical-align: top; 
  text-align: left; 
}

/** you’ll need a new set of these for each table type  **/
#cssRecord .cssUserCol1 { width: 15%; }
#cssRecord .cssUserCol2 { width: 15%; }
#cssRecord .cssUserCol3 { width: 60%; }
#cssRecord .cssUserCol4 { width: 10%; text-align: center; }
&lt;/pre&gt;
&lt;br /&gt;
  
&lt;h4&gt;RHMTL Content&lt;/h4&gt; – see below for explanation of split_str method.&lt;br /&gt;
&lt;h4&gt;File: list.rhmtl&lt;/h4&gt;
&lt;pre&gt;
&amp;#60;h1&amp;#62;Listing users&amp;#60;&amp;#47;h1&amp;#62;
  &amp;#60;table id="cssRecord" cellpadding="2" cellspacing="2"&amp;#62;&amp;#60;tr&amp;#62;
    &amp;#60;th class="cssUserCol1"&amp;#62;Name&amp;#60;&amp;#47;th&amp;#62;
    &amp;#60;th class="cssUserCol2"&amp;#62;Date&amp;#60;&amp;#47;th&amp;#62;
    &amp;#60;th class="cssUserCol3"&amp;#62;Description&amp;#60;&amp;#47;th&amp;#62;
    &amp;#60;th class="cssUserCol4"&amp;#62;&amp;#60;&amp;#47;th&amp;#62;
  &amp;#60;&amp;#47;tr&amp;#62;&amp;#60;&amp;#47;table&amp;#62;

  &amp;#60;% for user in @users -%&amp;#62;
  &amp;#60;div id="userObject&amp;#60;%= user.id %&amp;#62;"&amp;#62;
    &amp;#60;table id="cssRecord" cellpadding="2" cellspacing="2"&amp;#62;&amp;#60;tr&amp;#62;
      &amp;#60;td class="cssUserCol1"&amp;#62;&amp;#60;%= link_to h(split_str(user.name), 10), 
           :action =&amp;#62; 'show', :id =&amp;#62; user, :controller =&amp;#62; 'admin' -%&amp;#62;&amp;#60;&amp;#47;td&amp;#62;
      &amp;#60;td class="cssUserCol2"&amp;#62;&amp;#60;%=h split_str(user.created_on.strftime("%d %B %Y"), 10) 
           -%&amp;#62;&amp;#60;&amp;#47;td&amp;#62;
      &amp;#60;td class="cssUserCol3"&amp;#62;&amp;#60;%=h split_str(user.description, 50) -%&amp;#62;&amp;#60;&amp;#47;td&amp;#62;

      &amp;#60;td class="cssUserCol4"&amp;#62;&amp;#60;%= link_to_remote 'Delete', 
                       :complete =&amp;#62; "new Effect.Fade('userObject#{user.id}')",
                       :url =&amp;#62; {:action =&amp;#62; 'fake_destroy',
                          		:controller =&amp;#62; 'admin',
                          		:id =&amp;#62; user}, 
                        :confirm =&amp;#62; 'Are you sure?', :post =&amp;#62; true  
                    -%&amp;#62;
      &amp;#60;&amp;#47;td&amp;#62;
    &amp;#60;&amp;#47;tr&amp;#62;&amp;#60;&amp;#47;table&amp;#62;
  &amp;#60;&amp;#47;div&amp;#62;
  &amp;#60;% end -%&amp;#62;
&lt;/pre&gt;


&lt;br /&gt; 
&lt;p&gt;If your field content doesn’t wrap within the length you chose, it will realign the columns for that row only, which is messy and defeats the purpose.  To remedy this, I created a method to split longer strings.  It’s not the best way I’m sure, but works quite well.  Just put it into your *_helper.rb file, and pass the object to it.  I used it for the cssUserCol2 and cssUserCol3.
&lt;/p&gt;&lt;br /&gt;
&lt;h4&gt;File: *_helper.rb&lt;/h4&gt;

&lt;pre&gt;
def split_str(str=nil, len=10, char=" ")
 work_str = str.to_s.split(&amp;#47;&amp;#47;) if str
  return_str = ""
  i = 0
  if work_str
    work_str.each do |s|
      if (s == char || i == len)
        return_str += char
        return_str += s if s != char
        i = 0
      else
        return_str += s
        i += 1
      end
    end
  end
  return_str
end
&lt;/pre&gt;
</description>
      <pubDate>Tue, 08 Aug 2006 17:48:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:68c717d7-8a0a-4859-86c3-88e437f3e35c</guid>
      <author>Bart</author>
      <link>http://bartgdev.com/articles/2006/08/08/protoype-effects-with-css-and-tables</link>
      <category>Rails</category>
      <category>Web Dev</category>
      <category>Rails</category>
      <category>css</category>
    </item>
    <item>
      <title>"Protoype Effects with CSS and Tables" by Good design!
&lt;a href="http://imttbevu.com/tqbv/ovtg.html" rel="nofollow"&gt;My homepage&lt;/a&gt; | &lt;a href="http://vfumlquc.com/tdlx/xudy.html" rel="nofollow"&gt;Please visit&lt;/a&gt;</title>
      <description>&lt;p&gt;Good design!
&lt;a href="http://imttbevu.com/tqbv/ovtg.html"&gt;My homepage&lt;/a&gt; | &lt;a href="http://vfumlquc.com/tdlx/xudy.html"&gt;Please visit&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 09 Feb 2007 12:41:41 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:bed1edc7-be35-4f19-b11e-71c9c81c882e</guid>
      <link>http://bartgdev.com/articles/2006/08/08/protoype-effects-with-css-and-tables#comment-25032</link>
    </item>
    <item>
      <title>"Protoype Effects with CSS and Tables" by Great work!
[url=http://imttbevu.com/tqbv/ovtg.html]My homepage[/url] | [url=http://lowfljrv.com/gdmj/pbna.html]Cool site[/url]</title>
      <description>&lt;p&gt;Great work!
[url=http://imttbevu.com/tqbv/ovtg.html]My homepage[/url] | [url=http://lowfljrv.com/gdmj/pbna.html]Cool site[/url]&lt;/p&gt;</description>
      <pubDate>Fri, 09 Feb 2007 12:40:15 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f142d0e5-8be7-4500-8062-142bdc35f07f</guid>
      <link>http://bartgdev.com/articles/2006/08/08/protoype-effects-with-css-and-tables#comment-25031</link>
    </item>
  </channel>
</rss>
