为了账号安全,请及时绑定邮箱和手机立即绑定

随笔-jquery easyUI tree的使用

标签:
JQuery


通过tree完成权限菜单的显示;

权限菜单(根)-用户管理(二级),岗位管理(二级),资源管理(二级);

用户管理-普通用户管理(叶子),高级用户管理(叶子);

岗位管理-岗位管理1(叶子),岗位管理2(叶子),岗位管理3(叶子);

资源管理-资源管理1(叶子),资源管理2(叶子),资源管理3(叶子);


  1. 建立一个满足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>



点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消