easyui tree

1、TreeJson

package com.hyxt.data.box.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

/**
 * easui中的tree_data.json数据,只能有一个root节点
 * [{
 * "id":1,
 * "text":"Folder1",
 * "iconCls":"icon-save",
 * "children":[{
 * "text":"File1",
 * "checked":true
 * }]
 * }]
 * 提供静态方法formatTree(List<TreeJson> list) 返回结果
 * TreeJson.formatTree(treeJsonlist) ;
 *
 * @author huw
 */
public class TreeJson implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private String id;
    private String pid;
    private String text;
    private String iconCls;
    private String state;
    private String checked;
    private JSONObject attributes = new JSONObject();
    private List<TreeJson> children = new ArrayList<TreeJson>();

    /**
     * ***** setter and getter *********
     */

    public static List<TreeJson> formatTree(List<TreeJson> list) {

        TreeJson root = new TreeJson();
        TreeJson node = new TreeJson();
        List<TreeJson> treelist = new ArrayList<TreeJson>();// 拼凑好的json格式的数据
        List<TreeJson> parentnodes = new ArrayList<TreeJson>();// parentnodes存放所有的父节点
        int k=0;
        //for (int k = 0; k < parentList.size(); k++) {
            if (list != null && list.size() > 0) {
                root = list.get(0);
                //循环遍历oracle树查询的所有节点
                for (int i = 1; i < list.size(); i++) {
                    node = list.get(i);
                    if (node.getPid().equals(root.getId())) {
                        //为tree root 增加子节点
                        parentnodes.add(node);
                       // node.setChecked("true");
                       root.getChildren().add(node);
                    } else {//获取root子节点的孩子节点
                        //node.setChecked("true");
                        getChildrenNodes(parentnodes, node);
                        parentnodes.add(node);
                    }

                }
                treelist.add(root);
            }

       // }
        return treelist;

    }

    private static void getChildrenNodes(List<TreeJson> parentnodes, TreeJson node) {
        //循环遍历所有父节点和node进行匹配,确定父子关系
        for (int i = parentnodes.size() - 1; i >= 0; i--) {

            TreeJson pnode = parentnodes.get(i);
            //如果是父子关系,为父节点增加子节点,退出for循环
            if (pnode.getId().equals(node.getPid())) {
                //pnode.setState("closed");//关闭二级树
                pnode.getChildren().add(node);

                return;
            } else {
                //如果不是父子关系,删除父节点栈里当前的节点,
                //继续此次循环,直到确定父子关系或不存在退出for循环
                parentnodes.remove(i);
            }
        }
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getChecked() {
        return checked;
    }

    public void setChecked(String checked) {
        this.checked = checked;
    }

    public List<TreeJson> getChildren() {
        return children;
    }

    public void setChildren(List<TreeJson> children) {
        this.children = children;
    }
}

 

2、用户页面

<script type="text/javascript">
    $(function(){
        $('#treeul').tree({
            checkbox:true,
            url:'<%=request.getContextPath()%>/manage/role/roleresourcedata?roleId='+${roleId},
            onLoadSuccess:function(){
                //绑定权限
                $.ajax({
                    cache:false,
                    dataType:'text',
                    success:function(data){
                        var array = data.split(',');
                        for(var i=0;i<array.length;i++)
                        {
                            var node = $('#treeul').tree('find',array[i]);
                            $('#treeul').tree('check',node.target);
                        }
                    }
                })
            }
        });
    });


    function getChecked(){
        var nodes = $('#treeul').tree('getChecked');
        var s = '';
        for(var i=0; i<nodes.length; i++){
            if (s != '') s += ',';
            s += nodes[i].id;
        }
        $("#rightIds").val(s);
        return true;
    }
</script>

---------------------------

还可以这样写:

$(function(){
        $('#treeul').tree({
            checkbox:true,
            url:'<%=request.getContextPath()%>/manage/role/roleresourcedata?roleId='+${roleId},
            onLoadSuccess:function(){
                //绑定权限
                $.ajax({
                    url:'getId.aspx?id='+v,
                    cache:false,
                    dataType:'text',
                    success:function(data){
                        var array = data.split(',');
                        for(var i=0;i<array.length;i++)
                        {
                            var node = $('#treeul').tree('find',array[i]);
                            $('#treeul').tree('check',node.target);
                        }
                    }
                })
            }
        });
    });

 

 


<form id="resourceForm" name="resourceForm" action="<%=request.getContextPath()%>/manage/role/resourcesave">
<input type="hidden" id="roleId" name="roleId" value="${roleId}">
<input type="hidden" id="rightIds" name="rightIds" value="">


    <div class="easyui-panel" style="padding:5px">


   <ul id="treeul" style="height: 300px;" class="easyui-tree" <%--data-options="url:'<%=request.getContextPath()%>/manage/role/roleresourcedata?roleId='+${roleId},method:'get',animate:true,checkbox:true"--%>></ul>
</div>

3、后台JSON

@RequestMapping(value = "/roleresourcedata")
    public
    @ResponseBody
   String roleresourcedata(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String roleId=request.getParameter("roleId");
        StringBuilder jsonString =new StringBuilder();
        List<TreeJson> list = rightService.findAll(roleId);
        List<TreeJson> treeJsonList = TreeJson.formatTree(list);
        net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(treeJsonList);
        logger.info(" = ============== " +jsonArray.toString());
        return jsonArray.toString();
    }

你可能感兴趣的:(easyui)