easyUi ---tree 后台动态生成json字符串形成菜单树

public class TreeNode implements java.io.Serializable {
    private Integer id;
    private String text;// 树节点名称
    private String iconCls;// 前面的小图标样式
    private Boolean checked = false;// 是否勾选状态
    private Map<String, Object> attributes;// 其他参数
    private List<TreeNode> children;// 子节点
    private String state = "open";// 是否展开(open,closed)

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

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

    public Boolean getChecked() {
        return checked;
    }

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

    public Map<String, Object> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

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

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

    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;
    }

}
   
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
	<class name="com.acca.entity.Menu" table="t_menu"> 
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<property name="ciconcls"/>
		<property name="url"/>
		
		<set name="menus" cascade="all" inverse="true">
		 <key>
		  <column name="pid"></column>
		 </key>
		 <one-to-many class="com.acca.entity.Menu"/>
		</set>
		<many-to-one name="menu" class="com.acca.entity.Menu" column="pid"></many-to-one>
	</class>
</hibernate-mapping>
 
   
private List jsonList = new ArrayList();//action中的属性

 /**
     * 形成菜单树
     * 
     * @return
     * @throws IOException
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public String makeMenuTree() throws IOException {
        jsonList=menuService.tree(menu, true);
        return SUCCESS;
    }
 
   <action name="makeMenuTree" class="menuAction"  method="makeMenuTree">
   		<result type="json">
   		<param name="root">jsonList</param>
		</result>
   </action>
 
public List<TreeNode> tree(Menu menu, Boolean b) {
        List<Object> param = new ArrayList<Object>();
        String hql = "from Menu m where m.menu is null";
        if (menu != null && menu.getId() != null) {
            hql = "from Menu m where m.menu.id = ?";
            param.add(menu.getId());
        }
        List<Menu> l = menuDao.find(hql, param);
        List<TreeNode> tree = new ArrayList<TreeNode>();
        for (Menu t : l) {
            tree.add(tree1(t, b));
        }
        return tree;
    }
    
    /**
     * 菜单
     * @param t
     * @param recursive
     * @return
     */
    @SuppressWarnings("unused")
    private TreeNode tree1(Menu t, boolean recursive) {
        TreeNode node = new TreeNode();
        node.setId(t.getId());
        node.setText(t.getName());
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("url", t.getUrl());
        node.setAttributes(attributes);
        if (t.getCiconcls() != null) {
            node.setIconCls(t.getCiconcls());
        } else {
            node.setIconCls("");
        }
        if (t.getMenus() != null && t.getMenus().size() > 0) {
            node.setState("closed");
            if (recursive) {// 递归查询子节点
                List<Menu> l = new ArrayList<Menu>(t.getMenus());
                //Collections.sort(l, new MenuComparator());// 排序
                List<TreeNode> children = new ArrayList<TreeNode>();
                for (Menu r : l) {
                    TreeNode tn = tree1(r, true);
                    children.add(tn);
                }
                node.setChildren(children);
            }
        }
        return node;
    }
 
<script type="text/javascript" src="js/timer.js">

$().ready(function() {
	$('#tree').tree({   
	    url:'makeMenuTree.action' ,
	    lines : true,
	    onClick : function (node) {
	             if (node.attributes) {
	              Open(node.text, node.attributes.url);
	             }
	          }
	}); 
</script>

<ul id="tree" class="easyui-tree"></ul>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值