<?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>Vinsol - Leading Ruby on Rails Development and Consulting Firm in India &#187; ajax tree</title>
	<atom:link href="http://vinsol.com/blog/category/ajax-tree/feed/" rel="self" type="application/rss+xml" />
	<link>http://vinsol.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 21 May 2012 13:11:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Sample Rails Application &#8211; A demo for the ajax based drag drop tree in rubyonrails</title>
		<link>http://vinsol.com/blog/2006/11/26/sample-rails-application-a-demo-for-the-ajax-based-drag-drop-tree-in-rubyonrails/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sample-rails-application-a-demo-for-the-ajax-based-drag-drop-tree-in-rubyonrails</link>
		<comments>http://vinsol.com/blog/2006/11/26/sample-rails-application-a-demo-for-the-ajax-based-drag-drop-tree-in-rubyonrails/#comments</comments>
		<pubDate>Sun, 26 Nov 2006 09:44:31 +0000</pubDate>
		<dc:creator>Sur Max</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax tree]]></category>
		<category><![CDATA[drag drop tree]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[RubyonRails]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://ajaxonrails.wordpress.com/2006/11/26/ajaxonrailsdragdroptree/</guid>
		<description><![CDATA[I have provided the source code of the ajax based drag drop tree in rubyonrails in one of my previous posts.
I found some of the people are getting problems to incorporate the code into their running applications so i am providing a sample rails application in which all the code for tree is already been [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><p>I have provided the <a href="http://ajaxonrails.wordpress.com/2006/08/18/sorce-code-for-ajax-based-drag-drop-navigation-tree-for-rails/"><strong>source code</strong></a> of the ajax based drag drop tree in rubyonrails in one of my previous posts.<br />
I found some of the people are getting problems to incorporate the code into their running applications so i am providing a sample rails application in which all the code for tree is already been placed well.<br />
However the code written seems to be lagged behind the current trends followed in rails development coz of the fire growth of rails itself, but its simply that when i wrote this tree i was very new to rails so you may find the code looks like an old wine but still tastes good to go with.</p>
<p>CHECK THIS OUT&#8230;</p>
<p><strong>Four simple steps to make the tree working&#8230;</strong></p>
<ol>
<li><a href="http://rubyforge.org/frs/download.php/15381/ajaxtree.zip"><strong>DOWNLOAD</strong></a> the sample application. (let me know if you are getting any error in downloading the application.)</li>
<li>Create a <strong>test</strong> database in mysql <strong>or</strong> modify the file <strong>/config/database.yml</strong> according to the database and user u need.</li>
<li>Run the command
<pre>ajaxtree&gt; rake db:migrate</pre>
<p>from the application root.</li>
<li>Run the application server by running
<pre>ajaxtree&gt; ruby script/server</pre>
<p>and watch the working tree at <strong>http://localhost:3000</strong></li>
</ol>
</div>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/11/26/sample-rails-application-a-demo-for-the-ajax-based-drag-drop-tree-in-rubyonrails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Source Code For Ajax Based Drag Drop Navigation Tree in Ruby on Rails &#8211; the tree works well with firefox and IE-6</title>
		<link>http://vinsol.com/blog/2006/08/18/source-code-for-ajax-based-drag-drop-navigation-tree-in-ruby-on-rails-the-tree-works-well-with-firefox-and-ie-6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=source-code-for-ajax-based-drag-drop-navigation-tree-in-ruby-on-rails-the-tree-works-well-with-firefox-and-ie-6</link>
		<comments>http://vinsol.com/blog/2006/08/18/source-code-for-ajax-based-drag-drop-navigation-tree-in-ruby-on-rails-the-tree-works-well-with-firefox-and-ie-6/#comments</comments>
		<pubDate>Fri, 18 Aug 2006 12:02:35 +0000</pubDate>
		<dc:creator>Sur Max</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax tree]]></category>
		<category><![CDATA[drag drop tree]]></category>
		<category><![CDATA[navigation tree]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">https://ajaxonrails.wordpress.com/2006/08/18/sorce-code-for-ajax-based-drag-drop-navigation-tree-for-rails/</guid>
		<description><![CDATA[Ajaxified Drag Drop Tree in RoR
CASE STUDY
I m providing a very generalized use case where the tree fits in a good position. Here it is&#8230;
Consider a model Item, a controller Items. Item model is using a fabulous acts_as_tree and we are going to put a seed for Item to grow it in an ajax tree [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><p><strong>Ajaxified Drag Drop Tree in RoR</strong><br />
<strong><span>CASE STUDY</span></strong></p>
<p>I m providing a very generalized use case where the tree fits in a good position. Here it is&#8230;</p>
<p>Consider a model <strong>Item</strong>, a controller <strong>Items</strong>. Item model is using a fabulous <strong>acts_as_tree</strong> and we are going to put a seed for Item to grow it in an ajax tree <img src='http://ajaxonrails.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> &#8230; Ok no more non-code talk. So, lets start the code now&#8230;</p>
<p>==========================================================<br />
I have also incorporated the code into a sample application which you can directly check out and try the tree yourself if you find it a<br />
headache to add the following code in a number of described files.<br />
So, here is the <a href="http://ajaxonrails.wordpress.com/2006/11/26/ajaxonrailsdragdroptree/"><b>Sample Tree Application</b></a></p>
<p>or you can try to code yourself as&#8230;</p>
<p>Create a sample rails application say <b>treeapp</b> by running</p>
<pre>
rails treeapp
</pre>
<p>from the command prompt.<br />
Now simply change your directiry into just created <b>treeapp</b> and make sure that you are in the directory <b>treeapp</b></p>
<p>Now configure the database settings for this application by modifying the file <b>/config/database.yml</b> as &#8230;</p>
<pre>
development:
  adapter: mysql
  database: tree_dev
  username: root
  password: root
  host: localhost
</pre>
<p>Here it simply shows that you have a mysql database named <b>tree_dev</b> and a user <b>root</b> with password <b>root</b> can access this database. So make sure about these settings.</p>
<p>From the command prompt in application root(i.e. you are in the directory <b>treeapp</b>) run this command to generate the model <strong>Item</strong>&#8230;</p>
<pre>
treeapp&gt; ruby script/generate model item
</pre>
<p>Add the following code to the file <strong>app/models/itme.rb</strong></p>
<pre>
class Item &lt; ActiveRecord::Base

  acts_as_tree
  validates_presence_of :name
  attr_accessor :style

  def self.roots
    self.find(:all, :conditions=&gt;["parent_id = ?", 0])
  end

  def level
    self.ancestors.size
  end
end</pre>
<p>This simply shows that you should have a table named <strong>tems</strong> in your database&#8230;<br />
<strong>so why we havnt mentioned it earlier ? </strong><br />
Thats the thing which will make you feel an <strong>agile web development</strong>.<br />
Now look at the directory <strong>db/migrate</strong>and a you will find a file named as <strong>db/migrate/001_create_items.rb</strong><br />
Add the following code to this file <strong>001_create_items.rb</strong><br />
Here we are creating our database table and also adding some initial data to work with.</p>
<pre>
class CreateItems &lt; ActiveRecord::Migration
  def self.up
      create_table "items", :force =&gt; true do |t|
         t.column "name", :string
         t.column "created_at", :datetime
         t.column "parent_id", :integer, :default =&gt; 0, :null =&gt; false
      end
      %w(item1 item2 item3 item4 item5).each do |name|
            parent = Item.new(:name=&gt;name)
            parent.save
            Item.create(:name=&gt;name+".1", :parent_id=&gt;parent.id)
            Item.create(:name=&gt;name+".2", :parent_id=&gt;parent.id)
            Item.create(:name=&gt;name+".3", :parent_id=&gt;parent.id)
       end
 end

 def self.down
   drop_table :items
 end

end</pre>
<p>Now from the command line from the root of your application run the following command to have a table named <strong>Item</strong> in your database with some initial data.</p>
<pre>
  treeapp&gt; rake db:migrate</pre>
<p>Before we start handling our views and controller part just have a smart small image named as <strong>drag.gif</strong> in your public/images directory that we will use as a handle to drag the nodes. So, now you can see a small image at <strong>public/images/drag.gif</strong>, cool !.<br />
Now from the command line from the root of your application run the following command to create a controller &#8230;</p>
<pre>
treeapp&gt; ruby script/generate controller items show</pre>
<p>Make sure that now you have the files <strong>app/controllers/items_controller.rb</strong> and <strong>app/views/items/show.rhtml</strong>.<br />
Add the following code in the file <strong>app/controllers/items_controller.rb</strong></p>
<pre>
class ItemsController &lt; ApplicationController

  def show
    @items = Item.find(:all)
    @item = Item.find(:first)
    # select according to your choice...
    #this item will be selected node by default in the tree when it will first be loaded.
  end

  def display_clicked_item
    # this action will handle the two way syncronization...all the tree nodes(items) will be linked
    # to this action to show the detailed item on the left of the tree when the item is clicked
    # from the tree
    if request.xhr?
      @item = Item.find(params[:id]) rescue nil
      if @item
        # the code below will render all your RJS code inline and
        # u need not to have any .rjs file, isnt this interesting
        render :update do |page|
          page.hide "selected_item"
          page.replace_html "selected_item", :partial=&gt;"items/item", :object=&gt;@item
          page.visual_effect 'toggle_appear', "selected_item"
        end
      else
        return render :nothing =&gt; true
      end
    end
  end

  def sort_ajax_tree
    if request.xhr?
      if @item = Item.find(params[:id].split("_").first) rescue nil
        parent_item = Item.find(params[:parent_id])
        render :update do |page|
          @item.parent_id = parent_item.id
          @item.save
          @items=Item.find(:all)
          page.replace_html "ajaxtree", :partial=&gt;"items/ajax_tree", :object=&gt;[@item,@items]
          page.hide "selected_item"
          page.replace_html "selected_item", :partial=&gt;"items/item", :object=&gt;@item
          page.visual_effect 'toggle_appear', "selected_item"
        end
      else
        return render :nothing =&gt; true
      end
    end
  end

end</pre>
<p>Add the following code in the file <strong>app/views/items/show.rhtml</strong></p>
<pre>
&lt;h2&gt;<strong>Ajax Tree Application</strong>&lt;/h2&gt;

&lt;div id=&#8221;ajaxtree&#8221; style=&#8221;width:40%;float:left;&#8221;&gt;
 &lt;%= render :partial=&gt;&#8217;items/ajax_tree&#8217;, :object=&gt;[@item,@items] %&gt;
&lt;/div&gt;

&lt;div id=&#8221;selected_item&#8221;&gt;
 &lt;%= render :partial=&gt;&#8217;items/item&#8217;, :object=&gt;@item %&gt;
&lt;/div&gt;</pre>
<p>Add the following code in the file <strong>app/views/items/_item.rhtml</strong></p>
<pre>
&lt;% if @item %&gt;
  &lt;h2&gt;Selected Item is &lt;%=h @item.name%&gt; &lt;/h2&gt;
&lt;% else %&gt;
  Item not found
&lt;% end %&gt;</pre>
<p>Add the following code in the file <strong>app/views/items/_ajax_tree.rhtml</strong></p>
<pre>
&lt;script type="text/javascript"&gt;

function toggleDiv()
{
  Element.toggle('mytree');
  Element.toggle('expanded');
  Element.toggle('collapsed');
  return false;
}
function showDrag()
{
  var drag_images = $$('img.drag_image');
  drag_images.all(function(value,index){return value.style.display='inline';});
  Element.toggle('done');
  Element.toggle('reorder');
  return false;
}
function hideDrag()
{
  var drag_images = $$('img.drag_image');
  drag_images.all(function(value,index){return value.style.display='none';});
  Element.toggle('done');
  Element.toggle('reorder');
  return false;
}
&lt;/script&gt;

&lt;style&gt;

.mytree{padding:0 0 0 0px;}

.mytree li {padding:2 0 0 3px;}

.outer_tree_element{margin:0 0 0 10px;}

.inner_tree_element{margin:5px 0 0 10px;}

.mytree a{text-decoration:none; font-size:13px; color:black;}

.mytree a:hover{background-color:lightblue;}

.mytree label{font-weight:normal;}

.highlighted{background-color:lightblue;}

.normal{background-color:white;}

.drag_image{border:0px;}

&lt;/style&gt;

&lt;div id="mytree" class="mytree"&gt;

  &lt;% @ancestors = @item.ancestors.collect{|parent| parent.id} if @item.has_parent? %&gt;
  &lt;% @items = Item.find(:all) %&gt;
  &lt;%= get_tree_data(@items, 0){|n|
      link_to_remote(n.name,
      :url=&gt;{:controller=&gt;'items', :action=&gt;'display_clicked_item', :id=&gt;n.id},
  :loading=&gt;"Element.show('tree_indicator')",
  :complete=&gt;"Element.hide('tree_indicator')"
  )}
  %&gt;

  &lt;% @items.each do |node| %&gt;
  &lt;%= draggable_element node.id.to_s+'_tree_div',:revert=&gt;true,:snap=&gt;false, :handle=&gt;"'#{node.id.to_s}_drag_image'" %&gt;
  &lt;%= drop_receiving_element node.id.to_s+'_tree_div',
      :accept=&gt;'inner_tree_element',
  :url=&gt;{:controller=&gt;'items',:action=&gt;'sort_ajax_tree', :parent_id=&gt;node.id,:id=&gt;nil},
  :loading=&gt;"Element.show('sort_tree_indicator')",
  :complete=&gt;"Element.hide('sort_tree_indicator')"
  %&gt;

  &lt;% end %&gt;

  &lt;%= image_tag 'indicator.gif', :id=&gt;'tree_indicator', :style=&gt;'display:none' %&gt;
  &lt;%= image_tag 'indicator.gif', :id=&gt;'sort_tree_indicator', :style=&gt;'display:none' %&gt;
&lt;/div&gt;

&lt;script type="text/javascript"&gt;

  var selected_el = document.getElementById('&lt;%=@item.id%&gt;_tree_item');
  selected_el.className='highlighted';

  function toggleMyTree(id)
  {
  Element.toggle(id+'collapsed');
  Element.toggle(id+'expanded');
  Element.toggle(id+'children');
  return false;
  }
  function toggleBackground(el)
  {
  // using collection proxies to change the background
  var highlighted_el = $$("span.highlighted");
  highlighted_el.all(function(value,index){return value.className='normal'});

  el.className='highlighted';
  selected_el = el;
  return false;
  }
  function openMyTree(id)
  {
  Element.hide(id+'collapsed');
  Element.show(id+'expanded');
  Element.show(id+'children');
  return false;
  }

&lt;/script&gt;</pre>
<p>As you can see in the above file we have used some indicator and toggle images. So you will be required to have three more images in the directory <strong>public/images/</strong>.<br />
Here is the small description about these images&#8230;</p>
<ul>
<li> An indicator image that will be displayed at the bottom of the tree whenever a tree node is clicked. You can select from a number of <a href="http://www.napyfab.com/ajax-indicators/"><strong>Ajax Inidicators</strong></a>available on the web. Select one indicator image and save in your app with the name <strong>indicator.gif</strong>. So, now make sure that you can see the image at <strong>public/images/indicator.gif</strong></li>
<li> Second, we need to have a small image with <strong>+</strong> sign. That will be used to toggle the tree. save it as <strong>public/images/collapsed.gif</strong></li>
<li> Similarly, an image with <strong>-</strong> sign. Save it as <strong>public/images/expanded.gif</strong></li>
</ul>
<p>We have to include the prototype and scriptaculous javascript  libraries in the application.<br />
So just manually create a layout file <b>app/views/layouts/application.rhtml</b> and add the following code in the file <b>application.rhtml</b></p>
<pre>
&lt;html&gt;
  &lt;head&gt;
    &lt;%= javascript_include_tag :defaults %&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;%= @content_for_layout %&gt;
  &lt;/body&gt;

&lt;/html&gt;
</pre>
<p>Now the last but the most importatnt&#8230;The recursion to obtain the tree.<br />
Add the following code in the file <strong>app/helpers/application_helper.rb</strong></p>
<pre>
module ApplicationHelper

  def get_tree_data(tree, parent_id)
    ret = "&lt;div class='outer_tree_element' &gt;"
    tree.each do |node|
      if node.parent_id == parent_id
        node.style = (@ancestors and @ancestors.include?(node.id))? 'display:inline' : 'display:none'
        display_expanded = (@ancestors and @ancestors.include?(node.id))? 'inline' : 'none'
        display_collapsed = (@ancestors and @ancestors.include?(node.id))? 'none' : 'inline'
        ret += "&lt;div class='inner_tree_element' id='#{node.id}_tree_div'&gt;"
        if node.has_children?
          ret += "&lt;img id='#{node.id.to_s}expanded' src='/images/expanded.gif' onclick='javascript: return toggleMyTree(&#92;"#{node.id}&#92;"); ' style='display:#{display_expanded}; cursor:pointer;'  /&gt;  "
          ret += "&lt;img style='display:#{display_collapsed}; cursor:pointer;'  id='#{node.id.to_s}collapsed' src='/images/collapsed.gif' onclick='javascript: return toggleMyTree(&#92;"#{node.id.to_s}&#92;"); '  /&gt;  "
        end

        ret += " &lt;img src='/images/drag.gif' style='cursor:move' id='#{node.id}_drag_image' align='absmiddle' class='drag_image' /&gt; "

        ret += "&lt;span id='#{node.id}_tree_item'&gt;"
        ret += yield node
        ret += "&lt;/span&gt;"
        ret += "&lt;span id='#{node.id}children' style='#{node.style}' &gt;"
        ret += get_tree_data(node.children, node.id){|n| yield n}
        ret += "&lt;/span&gt;"
        ret += "&lt;/div&gt;"
      end
    end
    ret += "&lt;/div&gt;"
    return ret
  end
end</pre>
<p>Now you can check the tree functionality at <strong>http://localhost:3000/items/show</strong>.. assuming that you are running your server on port 3000. njoy!!</p>
</div>]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/08/18/source-code-for-ajax-based-drag-drop-navigation-tree-in-ruby-on-rails-the-tree-works-well-with-firefox-and-ie-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fully Ajax Based Drag Drop Navigation Tree For Ruby on Rails</title>
		<link>http://vinsol.com/blog/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails</link>
		<comments>http://vinsol.com/blog/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/#comments</comments>
		<pubDate>Wed, 09 Aug 2006 05:10:27 +0000</pubDate>
		<dc:creator>Sur Max</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax tree]]></category>
		<category><![CDATA[drag drop tree]]></category>
		<category><![CDATA[navigation tree]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">https://ajaxonrails.wordpress.com/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/</guid>
		<description><![CDATA[Ajaxified tree comes with the following features
1.) Containing the ajax links for the tree items using link_to_remote
2.) Highlighting the selected node of the tree and other custom effects.
3.) Two way syncronization between the tree nodes and the currently displayed item on the web-page.
4.) Supports the tree architecture provided by the acts_as_tree.
5.) Clear and self traversing [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Ajaxified tree comes with the following features</p>
<p>1.) Containing the ajax links for the tree items using link_to_remote</p>
<p>2.) Highlighting the selected node of the tree and other custom effects.</p>
<p>3.) Two way syncronization between the tree nodes and the currently displayed item on the web-page.</p>
<p>4.) Supports the tree architecture provided by the acts_as_tree.</p>
<p>5.) Clear and self traversing upto the selected node and all remaining nodes remained closed.</p>
<p>6.) Drag-n-Drop  sorting of the tree nodes, including the functionality of even changing the parent of the node.</p>
<p>This tree works very fine in my application and hope it will help u also. Check out the <a href="http://ajaxonrails.wordpress.com/2006/08/18/sorce-code-for-ajax-based-drag-drop-navigation-tree-for-rails/">Source Code</a> of the tree.
</p>
]]></content:encoded>
			<wfw:commentRss>http://vinsol.com/blog/2006/08/09/fully-ajax-based-drag-drop-navigation-tree-for-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

