ExtJS4.2 根据数据库记录构建树形菜单
背景:最近用ExtJS4.2做一个系统,需要在前端展示资源菜单,为树形结构,该树形结构是从数据库动态加载的。
ExtJS的树形结构大致有两种情况:
1.静态树形结构,此处不多说,看API就能简单明白;
2.动态加载,ExtJS的特性是根据父节点ID来查询子节点,从而动态更新树形菜单,这里有一个缺陷,或许是我孤陋寡闻不知道,那就是无法根据数据库节点信息自动构建成为一棵树,记得zTree插件就有这个功能。
那么,我希望能够根据数据库树节点信息自动的构建成一棵树,就需要自己去写个小算法在后台拼接成ExtJS需要的数据结构。
代码部分:
1.节点pojo,必要属性有:节点ID(id)、父节点ID(parentId)、文本信息(text)、孩子(children),其他属性,比如节点url,顺序order等根据自己需要设置。
public class Resource {
private Integer id;
private String text;
private Integer parentId;
private Boolean expanded;
private List<Resource> children = new ArrayList<Resource>();
}
2.根据查询出来的节点List集合拼装成为前端展示需要的结构,这里写了个静态方法。
public static final <T> List<T> buildTree(List<T> nodes) {
if(null == nodes || nodes.size() == 0) return null;
Map<Integer, T> resources = new HashMap<Integer, T>();
List<T> result = new ArrayList<T>();
try {
for(int i=0; i<nodes.size(); i++) {
T node = nodes.get(i);
Method getId = node.getClass().getMethod("getId");
Integer id = (Integer) getId.invoke(node);
resources.put(id, node);
}
for (Map.Entry<Integer, T> e : resources.entrySet()) {
T node = e.getValue();
Method getparentId = node.getClass().getMethod("getParentId");
Integer parentId = (Integer) getparentId.invoke(node);
if(parentId == 0) {
result.add(node);
} else {
T parent = resources.get(parentId);
if( null != parent) {
@SuppressWarnings("unchecked")
List<T> children = (List<T>) parent.getClass().getMethod("getChildren").invoke(parent);
children.add(node);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
3.数据库记录。

4.ExtJS前端代码。
Ext.onReady(function() {
var store = Ext.create('Ext.data.TreeStore', {
proxy: {
type: 'ajax',
url: 'your url'
},
root: {
text: '系统菜单',
id: 0,
expanded: true
}
});
var treePanel = Ext.create('Ext.tree.Panel', {
title: '树形菜单',
width: 300,
height: 350,
margin: '50 0 0 500',
store: store,
rootVisible: false,
useArrows: true,
renderTo: Ext.getBody()
});
});
5.效果图。

6.完毕。
ExtJS4.2 根据数据库记录构建树形菜单的更多相关文章
- java构建树形菜单递归工具类
1.设计菜单实体 import java.util.List; public class Menu { //菜单id private Long id; //父节点id private Long par ...
- 使用zTree插件构建树形菜单
zTree下载:https://github.com/zTree/zTree_v3 目录: 就我看来,zTree较为实用的有以下几点: zTree 是一个依靠 jQuery 实现的多功能 “树插件”. ...
- Java从数据库读取页面树形菜单
从数据库加载菜单及子菜单主要使用递归的方法,具体实现可看代码 首先封装一个菜单pojo public class Menu { // 菜单id private String id; // 菜单名称 p ...
- 构建简单的json树形菜单
json结构: var Menu = [{ tit:"一级菜单", submenu:[{ tit:"二级菜单", url:"", func: ...
- jQuery树形菜单(1)jquery.treeview
jQuery的树形插件资料URL:http://bassistance.de/jquery-plugins/jquery-plugin-treeview/从该网站Download得到jquery.tr ...
- ExtJS4 根据分配不同的树形菜单在不同的角色登录后
继续我的最后.建立cookie后,带他们出去 var userName = Ext.util.Cookies.get('userName'); var userAuthority = Ext.util ...
- 用Vue.js递归组件构建一个可折叠的树形菜单
在Vue.js中一个递归组件调用的是其本身,如: Vue.component('recursive-component', { template: `<!--Invoking myself! ...
- EasyUI创建异步树形菜单和动态添加标签页tab
创建异步树形菜单 创建树形菜单的ul标签 <ul class="easyui-tree" id="treeMenu"> </ul> 写j ...
- ERP存储过程的调用和树形菜单的加载(四)
引用:DAL:System.Data.SqlClient;System.Data; namespace CommTool { public class SqlComm { /// <summar ...
随机推荐
- 发起post请求
string postUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo"; //string req ...
- android关于The connection to adb is down, and a severe error has occured.这个问题的解决办法
有时在打开模拟器的时候会出现The connection to adb is down, and a severe error has occured.这个问题,这个问题的解决办法有两个: 方法一:找 ...
- python_类
1. 对象的概念 对象包括特性和方法.特性只是作为对象的一部分的变量,方法则是存储在对象内的函数.对象中的方法和其他函数的区别在于方法总是将对象作为自己的第一个参数,这个参数一般称为self. 2. ...
- hbase基本操作
public class Demo { private Configuration conf; private Connection conn; @Before public void prepare ...
- virtual box硬盘扩容 不是加一块硬盘
先执行 VirtualBox list hdds 查看当前 VirtualBox 管理的虚拟磁盘. 获取磁盘的 uuid. 结果如下 D:\Program Files\Oracle\VirtualBo ...
- PostgreSQL Replication之第十三章 使用PL/Proxy扩展(3)
13.3 聪明地扩展与处理集群 建立集群不是您面临的唯一任务.如果所有的事情都做完了并且系统已经运行了,您可能需要到处调整配置. 13.3.1 添加和移动分区 一旦一个集群启动并运行,您可能会发现您的 ...
- Leetcode: Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- SQL 分组查询 group by
select * from emp --deptno 为部门号 --输出每个部门的编号 和 该部门的平均工资 --group by deptno; 通过deptno分组 select deptno, ...
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
- CCF真题之字符串匹配
201409-3 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符:当选项关闭时,表示同一 ...