树形图菜单级多级菜单显示

来源:互联网 发布:上传文件夹到linux 编辑:程序博客网 时间:2024/04/23 17:54

jsp页面


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>    
   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/back/comm.css" />
    <script type="text/javascript" src="<%=request.getContextPath()%>/thirdparts/time-text/WdatePicker.js"></script>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/thirdparts/jquery-easyui-1.5/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/thirdparts/jquery-easyui-1.5/themes/icon.css">
    <script type="text/javascript" src="<%=request.getContextPath()%>/thirdparts/jquery-easyui-1.5/jquery.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/thirdparts/jquery-easyui-1.5/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/thirdparts/dialog4/artDialog.js?skin=blue"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/thirdparts/dialog4/plugins/iframeTools.js"></script>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/thirdparts/jquery-easyui-1.5/demo/demo.css">
    <script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/back/table-ghhs.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/jquery.form.js"></script>
     <script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/back/My97DatePicker/WdatePicker.js" language="javascript"></script>
    <script type="text/javascript">
    
    
    
        function Add(){
            $("#formadd").ajaxSubmit({
                url:"<%= request.getContextPath()%>/jurisdiction/add.do",
                type:"post",
                resetForm:false,
                success:function(data){
                    if(data=="ok"){
                        //获取父窗口的window对象
                        var win = art.dialog.open.origin;  
                        //调用父窗口的js方法refresh();
                        win.refresh();
                        //关闭对话框
                        art.dialog.close();
                    }
                },  
                error:function(xhr,textStatus,errorThrown){  
                    alert("新增权限失败,请重试");
                }
            });
        }
        
        
        
    </script>


    <!-- <script type="text/javascript">
        $(function(){

            
            $(".pmenu").click(function(){
                var chk = this;
                $(this).parent().next().find(":checkbox").each(function(i, item){
                    item.checked = chk.checked;
                });
            });
        });
    </script> -->
    
    
    <script type="text/javascript">
        function getChecked() {
            var nodes = $('#tt').tree('getChecked');
            var s = '';
            for (var i = 0; i < nodes.length; i++) {
                if (s != '') s += ',';
                s += nodes[i].text;
            }
            alert(s);
        }
       
        
    </script>
    
</head>
<body>

    
    

<form id="formadd">
    <div class="easyui-tabs" style="width: 100%;">
        <div title="权限基本信息">
            <div class="dialog-table">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tk_table">
                    <tr>
                        <td width="100px"><span class="font01 pr5">*</span>角色名称:</td>
                        <td class="white-tr">
                            <input type="text" class="form-text" id="txtdeptname" name="rolename" />
                        </td>
                    </tr>
                   

                    <tr>                           
                            <td width="100px"><span class="font01 pr5">*</span>权限信息:</td>
                            <td class="white-tr">
                            
                   <%--         
                    <c:forEach items="${menus }" var="menu">
                                <c:if test="${menu.parentfunid==null }">
                                    <div>
                                        <h4><input type="checkbox" class="pmenu" />${ menu.funname }</h4>
                                    <div>
                                    <c:forEach items="${menus }" var="submenu">
                                        <c:if test="${submenu.parentfunid==menu.funid }">
                                            <span>
                                                <input name="funids" value="${submenu.funid }" type="checkbox" />${ submenu.funname }
                                            </span>
                                </c:if>
                          </c:forEach>
                        </div>
                    </div>
                </c:if>
            </c:forEach> --%>
                  <div data-options="region:'center',split:true" title="权限树"  style="padding: 5px">
                     <ul id="tt" class="easyui-tree" data-options="url:'<%= request.getContextPath()%>/jurisdiction/tree.json',method:'get',animate:true,checkbox:true"></ul>
                    </div>
                          <%--   <div class="easyui-panel" style="padding:5px">
                        <ul id="tt" class="easyui-tree" data-options="url:'<%= request.getContextPath() %>/jurisdiction/tree.json',method:'get',animate:true,checkbox:true"></ul>
                </div>
 --%>                            
                            </td>
                    </tr>

            </table>




            </div>
        </div>
    </div>
</form>    
</body>
</html>


Controller实例


//权限树
    List<NodelTree> nodes=null;
    @ResponseBody
    @RequestMapping(value="/tree.json",method=RequestMethod.GET)    
    public List<NodelTree> tree(HttpServletResponse resp) throws Exception{
        List<Functionmenu> functList =functionmenuService.getAllfunctionmenu();
        
         nodes = new ArrayList<NodelTree>();
        
        for(Functionmenu fun :functList){
            
            if(fun.getParentfunid()==null || fun.getParentfunid()==0){
                
                NodelTree nodetree = new NodelTree();
                nodetree.setId(fun.getFunid());
                nodetree.setText(fun.getFunname());
                nodes.add(nodetree);
                
            }
            
        }
        
        
        setChildrenMenu(functList,nodes);
        /*ObjectMapper mapper = new ObjectMapper();
        String json=mapper.writeValueAsString(node);
        System.out.println(json);
        resp.getWriter().write(json);*/
        
        
    
        return nodes;
        
    }
    
    
    private void setChildrenMenu(List<Functionmenu> allMenu,List<NodelTree> parentNodes){
        List<Functionmenu> functList =functionmenuService.getAllfunctionmenu();    
        for(NodelTree node :parentNodes){
            for(Functionmenu fun :functList){
                
                if(fun.getParentfunid()==node.getId()){
                
                NodelTree child = new NodelTree();
                child.setId(fun.getFunid());
                child.setText(fun.getFunname());
                node.getChildren().add(child);                    
                }                
            }    
            setChildrenMenu(allMenu,node.getChildren());    
        }
        
            
        
    }

注意:实体类中

package com.wskj.ssm.util;

import java.util.ArrayList;
import java.util.List;

public class NodelTree {

    
    private Integer id;
    private String text;
    private List<NodelTree> children=new ArrayList<NodelTree>();
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public List<NodelTree> getChildren() {
        return children;
    }
    public void setChildren(List<NodelTree> children) {
        this.children = children;
    }
    
    
}



1 0
原创粉丝点击