java 生成easyui comotree 树对象

	public void getAllModuleTree(){
		List<TreeDataModel<Module>> result = new ArrayList<TreeDataModel<Module>>(); 

		result = createChildren("root");
		getWriter().write(LazyValueFilterSerializer.serialize(result));
	}
	public List<TreeDataModel<Module>> createChildren(String parentId){
		List<TreeDataModel<Module>> list = new ArrayList<TreeDataModel<Module>>();
		
		List<Module> mlist  = roleService.getModuleByParent(parentId); 	
		if(mlist.size()>0){
			for(int i=0;i<mlist.size();i++){
				Module m = mlist.get(i);
				
				TreeDataModel<Module> node = new TreeDataModel<Module>();
				node.setAttributes(null);
				node.setId(m.getMid());
				node.setText(m.getName());
				node.setState("open");
				
				node.setChildren(createChildren(m.getMid()));
				
				list.add(node);
			}
		}
		
		return list;
	}

 

 

package com.company.core.model;


import java.util.List;


public class TreeDataModel<T>  {

	/** 树节点id,可选属性 */
	private String id;

	/** 节点名称 */
	private String text;

	/** 原始对象 */
	private T attributes;

	/** 是否展开 open closed */
	private String state;

	/** 树节点图标,非必需 */
	private String iconCls;

	/** 孩子节点 */
	private List<TreeDataModel<T>> children;
    /**
     * 是否选中
     */
    private boolean checked;



    public String getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

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

    public T getAttributes() {
        return attributes;
    }

    public void setAttributes(T attributes) {
        this.attributes = attributes;
    }

    public String getState() {
        return state;
    }

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

    public String getIconCls() {
        return iconCls;
    }

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

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

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

    public boolean isChecked() {
        return checked;
    }

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

//    @Override
//    public String toString() {
//        //return JsonUtils.convertToJsonfromObject(this).toString();
//    }

}

 

 

function loadModuleTree(){
	var url = contextPath + "/role/!getAllModuleTree.action";
    $('#modulebt').combotree({
        //data:prodata,
    	url:url,
        multiple:true,
        editable:false,
        checkbox:false,
        cascadeCheck:false,
        onCheck:function(){

        },
		onLoadSuccess : function() {
			setModuleRole();
		}        
   });		
}

function setModuleRole(){
	var url = contextPath + "/role/!getModulesByRole.action";
	var actionUrl = url + "?roleId="+roleId;
	$.ajax({
        type: "get",
        async: false,
        url: actionUrl,
        contentType: "application/json; charset=utf-8",
        dataType: "text",
        cache: false,
        success: function (data) {
        	var p = [];
        	if(data != ""){
            	if(data.indexOf(",")>0){
                	var a = data.split(",");
                	for(var i=0;i<a.length;i++){
                		var m = a[i];
                		p.push(m);
                	}        		
            	}else{
            		p.push(data);
            	}
            	//必须采用数组的方式才能赋值
            	$('#modulebt').combotree('setValues',p);
        	}
        },
        error :function (XMLHttpRequest, textStatus, err) {
        }
     });	
}


function saveModuleRole(){
	var t = $('#modulebt').combotree('tree');  
	var n = t.tree('getChecked'); 
	var mids = "";
	for(var i=0;i<n.length;i++){
		mids = mids + n[i].id+"@";
	}
	mids = mids.substring(0,mids.length-1);
	
	var actionUrl = contextPath + "/role/!saveOrUpdateModuleRole.action?ids="+mids+"&roleId="+roleId;
	$.ajax({
        type: "get",
        async: false,
        url: actionUrl,
        contentType: "application/json; charset=utf-8",
        dataType: "text",
        cache: false,
        success: function (data) {
	    	if(data == "true"){
	    		$.messager.alert('提示','保存成功!','info');
	    		var listUrl = contextPath +  "/role/!list.action";
            	setTimeout(function(){
            		window.location.href = listUrl;	
            	},1000);	    		
            }else if(data == "false"){
            	$.messager.alert('提示','保存失败!','info');
            }     	
        },
        error :function (XMLHttpRequest, textStatus, err) {
        }
     });	
	
}

 

 

你可能感兴趣的:(easyui)