隨筆-jquery easyUI tree的使用

來源:互聯網
上載者:User

標籤:jquery easyui tree的使用


通過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>



隨筆-jquery easyUI tree的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.