从数据库中动态的获取部门 显示动态树,逐级加载
总部门
····部门1
··部门1+
····部门2
··部门2+
····部门3
··部门3+
http://www.adp-gmbh.ch/ora/sql/connect_by_nocycle.html
这里有个文章可以看一下。
ext,jquery插件都可以
public class TreeUtil {
public static ListgetTree(List list){
List tree= new ArrayList();
List parent= new ArrayList();
List chil =new ArrayList();
if(list==null){
return tree;
}
for(Tree ca:list){
if(ca.getLeaf()==0){//取出所有根节点
CTree pt=new CTree();
pt.setId(ca.getId());
pt.setCid(ca.getCid());
pt.setCls(ca.getCls());
pt.setText(ca.getText());
parent.add(pt);
}else{//取出所有子节点
CTree ch=new CTree();
ch.setId(ca.getId());
ch.setCid(ca.getCid());
ch.setCls(ca.getCls());
ch.setText(ca.getText());
ch.setUrl(ca.getUrl());
ch.setLeaf(ca.getLeaf()==1?true:false);
chil.add(ch);
}
}
for(CTree pr:parent){
List child= new ArrayList();
for(CTree cd:chil){
if(pr.getCid()==cd.getCid()){
child.add(cd);
pr.setChildren(child);
}
}
tree.add(pr);
}
return tree;
}
}
package com.byd.entity;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Tree implements Serializable{
private String text;
private int id;
private int leaf;
private String cls;
private String url;
private int cid;//类别ID
public Tree(){
}
/**
@param text the text to set
*/
public void setText(String text) {
this.text = text;
}
/**
@return the cls
/
public String getCls() {
return cls;
}
/*
@param cls the cls to set
/
public void setCls(String cls) {
this.cls = cls;
}
/*
@return the cid
/
public int getCid() {
return cid;
}
/*
@param cid the cid to set
/
public void setCid(int cid) {
this.cid = cid;
}
/*
@return the leaf
/
public int getLeaf() {
return leaf;
}
/*
@param leaf the leaf to set
*/
public void setLeaf(int leaf) {
this.leaf = leaf;
}
/**
@return the id
/
public int getId() {
return id;
}
/*
@param id the id to set
/
public void setId(int id) {
this.id = id;
}
/*
@return the url
/
public String getUrl() {
return url;
}
/*
@param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
}
Ext.ns('Ext.tree');
//TreeLoader扩展,支持josn-plugin返回的json对象中包含的数组值
Ext.tree.JsonPluginTreeLoader = function (config) {
this.rootName = 'root';
Ext.tree.JsonPluginTreeLoader.superclass.constructor.call(this, config);
}
Ext.extend(Ext.tree.JsonPluginTreeLoader, Ext.tree.TreeLoader, {
processResponse: function (response, node, callback,scope) {
var json = response.responseText;
try {
var o = response.responseData || Ext.decode(json);
//在原代码基础上增加了下面处理---------------------
if (Ext.type(o) == 'object') {//如果返回的是对象则获取他的root部分,rootName是可以在使用的时候配置的
o = o[this.rootName || 'root'];
}
//--------------------------------------------------
node.beginUpdate();
for (var i = 0, len = o.length; i < len; i++) {
var n = this.createNode(o[i]);
if (n) {
node.appendChild(n);
}
}
node.endUpdate();
this.runCallback(callback, scope || node, [node]);
} catch(e) {
this.handleFailure(response);
}
}
});
//Ext.ux.JsonPluginTreeLoader = Ext.ux.tree.JsonTreeLoader;
楼主如果只是想要一个Tree的插件,而不像用Ext什么的话,推荐ZTree,非常简单,非常轻量级,楼主不妨去官网一看[code="java"]http://baby666.cn/hunter/zTree.html[/code],不是广告哦
SimpleTree
[code="java"]
是Oracle数据库吗 ?
如果是的话 可以用下面的语法 Connect by prior start with
你可以去Google上搜下 :Oracle 树型查询
select bs_name,bs_value from balance_sheet
connect by prior bs_id = bs_pid
start with bs_pid = 0
[/code]
[code="java"][/code]
给你推荐一个树,很好用dhtmlxtree