随笔-jquery easyUI tree的使用
通过tree完成权限菜单的显示;
权限菜单(根)-用户管理(二级),岗位管理(二级),资源管理(二级);
用户管理-普通用户管理(叶子),高级用户管理(叶子);
岗位管理-岗位管理1(叶子),岗位管理2(叶子),岗位管理3(叶子);
资源管理-资源管理1(叶子),资源管理2(叶子),资源管理3(叶子);
建立一个满足tree json
树控件数据格式化
每个节点都具备以下属性:
id:节点ID,对加载远程数据很重要。
text:显示节点文本。
state:节点状态,‘open‘ 或 ‘closed‘,默认:‘open‘。如果为‘closed‘的时候,将不自动展开该节点。
checked:表示该节点是否被选中。
attributes: 被添加到节点的自定义属性。
children: 一个节点数组声明了若干节点。
要求的vo:
public class TreeDTO {
private int id ;
private String text ;
private String iconCls ;
private int checked ;
private int parent_id ;
private Map<String, Object> attributes = new HashMap<String, Object>();
private String state ;
public TreeDTO(int id, String text, String iconCls, int checked,
int parentId, Map<String, Object> attributes, String state) {
super();
this.id = id;
this.text = text;
this.iconCls = iconCls;
this.checked = checked;
parent_id = parentId;
this.attributes = attributes;
this.state = state;
}
public TreeDTO() {
super();
// TODO Auto-generated constructor stub
}
// get/set
}
2.建立一个权限bean:
public class Resource {
private int id ;
private String name ;
private String url ;
private int checked ;
private String icon ;
private int parent_id ;
public Resource() {
super();
}
public Resource(int id, String name, String url, int checked, String icon,
int parentId) {
super();
this.id = id;
this.name = name;
this.url = url;
this.checked = checked;
this.icon = icon;
parent_id = parentId;
}
//get/set
}
3.查询库,
首先通过id查询Resource,然后把Resource对象,然后根据Resource的父节点pid查找孩子节点,最后封装到treeDTO;
具体Dao层代码:
/**
* 根据父id获取子节点们
*/
public List<TreeDTO> getChildrenByParentId(String id) throws Exception {
Connection conn = DBUtils.createConn();
String sql = "";
if("".equals(id) || id == null){
sql = "select * from resource where parent_id = 999999";
} else {
sql = "select * from resource where parent_id = " + id ;
}
PreparedStatement ps = DBUtils.getPs(conn, sql);
ResultSet rs = ps.executeQuery();
List<Resource> rlist = new ArrayList<Resource>();
while(rs.next()){
Resource r = new Resource();
r.setId(rs.getInt("id"));
r.setIcon(rs.getString("icon"));
r.setChecked(rs.getInt("checked"));
r.setName(rs.getString("name"));
r.setUrl(rs.getString("url"));
r.setParent_id(rs.getInt("parent_id"));
rlist.add(r);
}
List<TreeDTO> tlist = new ArrayList<TreeDTO>();
for (Iterator iterator = rlist.iterator(); iterator.hasNext();) {
Resource resource = (Resource) iterator.next();
TreeDTO tree = new TreeDTO();
tree.setId(resource.getId());
tree.setText(resource.getName());
tree.setChecked(resource.getChecked());
tree.setIconCls(resource.getIcon());
tree.setParent_id(resource.getParent_id());
if(getChildren(resource.getId()).size() > 0){
tree.setState("closed");
} else {
tree.setState("open");
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("url", resource.getUrl());
tree.setAttributes(map);
tlist.add(tree);
}
return tlist;
}
/**
* 根据pid 获取孩子
* @param pid
* @return
* @throws Exception
*/
public List<Resource> getChildren(int pid) throws Exception{
Connection conn = DBUtils.createConn();
String sql = "select * from resource where parent_id = " + pid;
PreparedStatement ps = DBUtils.getPs(conn, sql);
ResultSet rs = ps.executeQuery();
List<Resource> children = new ArrayList<Resource>();
while(rs.next()){
Resource r = new Resource();
r.setId(rs.getInt("id"));
r.setIcon(rs.getString("icon"));
r.setChecked(rs.getInt("checked"));
r.setName(rs.getString("name"));
r.setUrl(rs.getString("url"));
r.setParent_id(rs.getInt("parent_id"));
children.add(r);
}
return children;
}
4.jsp
$(function(){
$(‘#t1‘).tree({
//发送异步ajax请求, 还会携带一个id的参数
url:‘ResourceServlet?method=loadTree‘
});
});
<ul id="t1"></ul>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。