由zTree的示范我们看到zTree支持多种数据类型,我认为json是最好的,项目中json肯定来自数据库,不可能就这样纯粹静态的,如何将数据库表中的属性信息生成json呢,这是关键问题,比如数据表读出的数据可能如下:
//用一个集合模拟数据表的内容,也就是一下集合的内容完全可以查询自数据库 List<SysFunction> treeList = new ArrayList<SysFunction>(); treeList.add(new SysFunction("L01", null, "营销管理")); treeList.add(new SysFunction("L02",null,"客户管理")); treeList.add(new SysFunction("L03",null,"服务管理")); //营销管理的子功能 treeList.add(new SysFunction("L0101","L01","创建销售机会")); treeList.add(new SysFunction("L0102","L01","指派销售机会")); treeList.add(new SysFunction("L0103","L01","制定开发计划")); treeList.add(new SysFunction("L0104","L01","执行开发计划")); //指派销售机会的子功能 treeList.add(new SysFunction("L010201","L0102","经理指派")); treeList.add(new SysFunction("L010202","L0102","主管指派")); //客户管理的子功能 treeList.add(new SysFunction("L0201","L02","客户信息管理")); treeList.add(new SysFunction("L0202","L02","客户流失管理")); //服务管理子功能 treeList.add(new SysFunction("L0301","L03","咨询服务管理")); treeList.add(new SysFunction("L0302","L03","投诉服务管理")); treeList.add(new SysFunction("L0303","L03","查询服务管理"));
这里的SysFunction类封装的是系统功能表,这个类的定义如下:/** * 系统功能对象 * */ class SysFunction { private String code; private String parentCode; private String title; /**功能编号*/ public String getCode() { return code; } public void setCode(String code) { this.code = code; } /**父级功能编号*/ public String getParentCode() { return parentCode; } public void setParentCode(String parentCode) { this.parentCode = parentCode; } /**功能名称,现在树上的节点文本*/ public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public SysFunction(String code, String parentCode, String title) { super(); this.code = code; this.parentCode = parentCode; this.title = title; } public SysFunction() { super(); } }
每个功能可能有一个父功