Rails使用EXT Tree实现级联树

首先下载ext的js包,可以到http://extjs.com/下载,现在有两个版本1.0和2.0,如果下载后的包名为ext1.0或者2.0则去掉版本号,统一改成ext,方便以后写路径名称;这个ext里的资源是一个总包,如果你对这个资源包里的内容很熟悉的话,可以对资源内容进行有选择性的使用;

将改好名的ext包拷贝到项目目录/publi下,形成路径public/ext,由于ext只是一个js包,并不是rails的插件;

 

其他操作如下:

 

数据库:

  1. DROP TABLE IF EXISTS `nested_set_items`;
  2. CREATE TABLE `nested_set_items` (
  3.   `id` int(11) NOT NULL auto_increment,
  4.   `parent_id` int(11) default NULL,
  5.   `lft` int(11) NOT NULL default '0',
  6.   `rgt` int(11) NOT NULL default '0',
  7.   `name` varchar(100) NOT NULL default '',
  8.   PRIMARY KEY  (`id`)
  9. );
  10. LOCK TABLES `nested_set_items` WRITE;
  11. INSERT INTO `nested_set_items` VALUES (1,NULL,1,14,'Root');
  12. INSERT INTO `nested_set_items` VALUES (2,1,2,7,'Child 1');
  13. INSERT INTO `nested_set_items` VALUES (3,2,3,4,'Child 1.1');
  14. INSERT INTO `nested_set_items` VALUES (4,2,5,6,'Child 1.2');
  15. INSERT INTO `nested_set_items` VALUES (5,1,8,13,'Child 2');
  16. INSERT INTO `nested_set_items` VALUES (6,5,9,10,'Child 2.1');
  17. INSERT INTO `nested_set_items` VALUES (7,5,11,12,'Child 2.2');
  18. UNLOCK TABLES;

 

控制器:

  1. class CategoriesController < ApplicationController
  2.   
  3.   def index(id = params[:node])   
  4.     puts id
  5.     respond_to do |format|   
  6.       format.html # render static index.html.erb   
  7.       format.json { render :json => Category.find_children(id)}   
  8.     end  
  9.   end
  10. end

model:

如果根据网上的demo来实现的话会出现node id不正确,id会出现类似ynode-xxx(xxx为数字),导致程序错误;本想去修改js的,但是js内容很庞大就放弃了,从而在mode里组织node的数据,而后觉得这种形式更好一些,可以根据业务逻辑进行控制;

  1. class Category < ActiveRecord::Base
  2.   establish_connection :localhost 
  3.   acts_as_nested_set
  4.   
  5.   #首先先得到树的根节点,再根据传过来的id找到根的子节点   
  6.   def self.find_children(start_id = nil)  
  7.     data = Array.new
  8.     if start_id.blank?
  9.       root_nodes
  10.     else
  11.       @nodes = find :all,:conditions=>"parent_id=#{start_id}"
  12.       @nodes.each { |node|
  13.           
  14.         if leaf? node.id
  15.            data +=   [{"text"  =>  node.name, "id"  => node.id, "cls" => "folder""leaf"  => false}] 
  16.         else
  17.            data +=   [{"text"  =>  node.name, "id"  => node.id, "cls" => "folder""leaf"  => true}]
  18.         end   
  19.         
  20.       }                         
  21.     end 
  22.     return  data
  23.   end  
  24.   
  25.   #如果parent_id为空,则为树的根节点   
  26.   def self.root_nodes   
  27.     find(:all:conditions => 'parent_id IS NULL')   
  28.   end 
  29.   def self.leaf?(nodeid)  
  30.     children = Category.find :all,:conditions=>["parent_id =?",nodeid]
  31.     if children.size > 0
  32.        return true;
  33.     else
  34.        return false;
  35.     end
  36.   end  
  37.    
  38. end

 

视图:

  1. <html>  
  2.   <head>  
  3.     <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>  
  4.     <title>Rails Ext Tree 测试</title>  
  5.     <%= stylesheet_link_tag "../ext/resources/css/ext-all.css" %>
  6.     <%= javascript_include_tag :defaults %>
  7.     <%= javascript_include_tag "../ext/adapter/prototype/ext-prototype-adapter.js" %>
  8.     <%= javascript_include_tag "../ext/ext-all.js" %>
  9.         <%= javascript_include_tag "../ext/adapter/ext/ext-base.js" %>
  10.          <%= javascript_include_tag "../ext/ext-all-debug.js" %>
  11.   </head>  
  12.   <body>  
  13.     <div id="category-tree" style="padding:20px"></div>
  14.     <% javascript_tag do -%>  
  15.       Ext.onReady(function(){        
  16.       var root = new Ext.tree.AsyncTreeNode({   
  17.       text: 'EXT测试树结构ROOT'
  18.       draggable:false,
  19.       id:'1'   
  20.       });   
  21.     
  22.       var tree = new Ext.tree.TreePanel({  
  23.       animate:false,
  24.       loader: new Ext.tree.TreeLoader({   
  25.       dataUrl:'/categories',   
  26.       requestMethod:'GET',   
  27.       baseParams:{format:'json',lib:'yui'}   
  28.       }),   
  29.       renderTo:'category-tree',   
  30.       root: root,   
  31.       rootVisible:true ,
  32.       autoScroll:true,   
  33.       enableDD:false,
  34.       containerScroll: true
  35.       });  
  36.     
  37.       tree.on('click', function(node){
  38.          alert(node.text);
  39.       });
  40.     
  41.     
  42.       tree.setRootNode(root);
  43.       tree.render(); 
  44.       root.expand();   
  45.       });   
  46.     
  47.     
  48.     
  49.     <% end -%>  
  50.   </body>  
  51. </html>  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值