JQuery树菜单

要做个树菜单,找了几个jquery控件,发现treeview,dtree功能都太简单,不支持我需要的异步加载,jstree又太麻烦,看到那个主题包我就不想用了,最后发现了国人开发的ztree,照着API写了个简单的demo,感觉还不错。配合struts2的json插件,基本满足我的要求。

 

前台:

 

var setting; setting = { checkable: false, async: true, asyncUrl: "<%=request.getContextPath() %>/jeaf/eai/listChildren.shtml", //获取节点数据的URL地址 asyncParam: ["name", "id"], callback:{ click: zTreeOnClick } }; function zTreeOnClick(event, treeId, treeNode) { top.frames["contentFrame"].location = "<%=request.getContextPath() %>/cms/site/view.shtml?view.id=" + treeNode.id; } var zTreeNodes = [ {"id":<s:property value="tree.rootNode.id"/>, "name":"<s:property value="tree.rootNode.text" escape="false"/>", "nodes": [ <s:iterator var="node" value="tree.rootNode.children" status="status"> {"id":<s:property value="id"/>, "name":"<s:property value="text" escape="false"/>", "isParent": "<s:property value="hasChildren"/>"}<s:if test="!#status.last">,</s:if> </s:iterator> ]} ] var zTree = $("#tree").zTree(setting, zTreeNodes);

 

后台struts2:

 

public String createDirectoryTree() throws Exception { SiteDirectoryService siteDirectoryService = (SiteDirectoryService)getService("siteDirectoryService"); tree = siteDirectoryService.createDirectoryTree(1); return SUCCESS; } public String listChildren() throws Exception { SiteDirectoryService siteDirectoryService = (SiteDirectoryService)getService("siteDirectoryService"); nodes = siteDirectoryService.listChildren(id); return SUCCESS; }

 

struts.xml:

 

<action name="createDirectoryTree" class="navigatorAction" method="createDirectoryTree"> <result name="success">/jeaf/eai/navigator.jsp</result> </action> <action name="listChildren" class="navigatorAction" method="listChildren"> <!-- result name="success">/jeaf/eai/listChildren.jsp</result--> <result name="success" type="json"> <param name="root">nodes</param> <!-- param name="wrapPrefix">[</param--> <!-- param name="wrapSuffix">]</param--> </result> </action>

 

pojo中用到json插件:

 

public class TreeNode extends BaseModel { private String id; private String text; private boolean hasChildren; private boolean expanded; //是否需要展开 private List<TreeNode> children; //子节点列表 /** * @return the text */ @JSON(name="name") public String getText() { return text; } /** * @return the hasChildren */ @JSON(name="isParent") public boolean isHasChildren() { return hasChildren; } ...... }

你可能感兴趣的:(jquery,exception,json,struts,String,iterator)