关于easyui的tree
所有树数据都保存在同一张表中,实现无线下级的tree
tree的实体类
import java.io.Serializable;
/**
* 类别信息
*
*/
public class Project_Type implements Serializable {
/**
* 类别ID
*/
private int type_id;
/** 类别编号 **/
private String type_no;
/** 类别名称 **/
private String type_name;
/** 父类ID **/
private int parent_id;
/** 是否末级 **/
private boolean type_end;
/** 类别描述 **/
private String type_describe;
public Project_Type() {
super();
}
public Project_Type(int type_id, String type_no, String type_name,
int parent_id, boolean type_end,
String type_describe) {
super();
this.type_id = type_id;
this.type_no = type_no;
this.type_name = type_name;
this.parent_id = parent_id;
this.type_end = type_end;
this.type_describe = type_describe;
}
public int getType_id() {
return type_id;
}
public void setType_id(int type_id) {
this.type_id = type_id;
}
public String getType_no() {
return type_no;
}
public void setType_no(String type_no) {
this.type_no = type_no;
}
public String getType_name() {
return type_name;
}
public void setType_name(String type_name) {
this.type_name = type_name;
}
public int getParent_id() {
return parent_id;
}
public void setParent_id(int parent_id) {
this.parent_id = parent_id;
}
public boolean getType_end() {
return type_end;
}
public void setType_end(boolean type_end) {
this.type_end = type_end;
}
public String getType_describe() {
return type_describe;
}
public void setType_describe(String type_describe) {
this.type_describe = type_describe;
}
}
新增子树数据的代码
public Project_Type getTypeNoById(int pid) {
Project_Type pttype=new Project_Type();
String hql=" from Project_Type as t where t.parent_id=? order by t.type_id desc";
List<Project_Type> pt = null;
try {
pt = baseDao.find(hql, new Integer[]{pid}, 1, 1);
} catch (Exception e) {
System.err.println(e.toString());
System.err.println("--------------------");
System.err.println(e.getMessage());
System.err.println("--------------------");
e.printStackTrace();
return null;
}
if(pt==null || pt.size()==0){
pttype=baseDao.get(Project_Type.class, pid);//不存在,就加载父类。获取父类的NO+01
pttype.setType_no(pttype.getType_no()+"01");
}else{
pttype=pt.get(0);
int nummber=Integer.parseInt(pttype.getType_no())+1;//强制转父类下最大的type_no 并加1 就为新的
pttype.setType_no(nummber+"");
}
return pttype;
}
查询tree的时候封装成框架需要的代码
public List<TreeNode> getAllTreeNode(Integer type_id,Integer id) {
List<Project_Type> list=new ArrayList<Project_Type>();
System.out.println("当前类别:" + type_id + ",父类Id:" + id);
try {
if(type_id == null && id == null){
list=baseDao.find(" from Project_Type where parent_id=0");
}else{
if(id == null){
list=baseDao.find(" from Project_Type where parent_id= 0 and type_id = "+type_id);
}else{
list=baseDao.find(" from Project_Type where parent_id= " + id);
}
}
} catch (Exception e) {
System.err.println(e.toString());
System.err.println("--------------------");
System.err.println(e.getMessage());
System.err.println("--------------------");
e.printStackTrace();
return null;
}
List<TreeNode>nodes=new ArrayList<TreeNode>();
for (Project_Type p : list) {
TreeNode t=new TreeNode();
HashMap<String,String>map=new HashMap<String, String>();
t.setChecked(false);
if(p.getType_end()){
map.put("isend", "YES");
t.setState("open");
}else{
t.setState("closed");
map.put("isend", "NO");
}
t.setText(/*"["+p.getType_no()+"]"+*/p.getType_name());
t.setId(p.getType_id()+"");
t.setAttributes(map);
t.setIconCls("icon-blank"); //设置tree的图标为空白
t.setT(p);
nodes.add(t);
}
return nodes;
}
前台代码首先导入easyui的js和css等
其次tree的代码
<ul id="mytree" ></ul>
$(function(){
//树形菜单加载
$('#mytree').tree({
url:'loadTree.action',
toolbar:'#toolbar',
dnd:false,//是否允许拖拽
onSelect : function(node) {
if(node.attributes.isend=='NO'){
url:'loadTree.action'
//alert('数据刷新中')
}else{
//alert('数据不能刷新/不能新增子类')
}
},
onDblClick: function(node){
update();
},
onContextMenu: function(e, node){
e.preventDefault();
// 查找节点
$('#mytree').tree('select', node.target);
// 显示快捷菜单
$('#mm').menu('show', {
left: e.pageX,
top: e.pageY
});
},
onBeforeDrag:function(node){ //在开始拖动节点之前触发,返回false可以拒绝拖动
}
});
});
easyui的帮助文档: http://download.csdn.net/download/love_xiolan/9628767