d10

测试ztree菜单

  • ztree 是一个依靠 jQuery 实现的多功能 “树插件”,需要导入jquery才能导入zTree
  • 树状菜单、树状数据的Web显示、权限管理等功能会使用到
  • 特点
    优异的性能、灵活的配置、多种功能的组合

ztree菜单测试

引入js

在这里插入图片描述

编写test

生成的数据的类型为List<Map<String,Object>>

public class TestZtreeJsonData {
    @Test
    public void test01() throws JsonProcessingException {
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> node1=new HashMap<String,Object>();
        node1.put("id",1);
        node1.put("pId",0);
        node1.put("name","Sass管理");
        node1.put("open",true);
        Map<String,Object> node2=new HashMap<String,Object>();
        node2.put("id",11);
        node2.put("pId",1);
        node2.put("name","企业管理");
        node2.put("open",true);
        node2.put("checked",true);
        Map<String,Object> node3=new HashMap<String,Object>();
        node3.put("id",111);
        node3.put("pId",1);
        node3.put("name","模块管理");
        //因为三个元素放在[]中,所以本质上放到集合中的
        list.add(node1);
        list.add(node2);
        list.add(node3);
        String json = new ObjectMapper().writeValueAsString(list);
        System.out.println(json);
    }
}

ztree.jsp

<%@  page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
    <title>Title</title>
    <% pageContext.setAttribute("path",request.getContextPath()); %>
    <link rel="stylesheet" type="text/css" href="${path}/plugins/ztree/css/zTreeStyle/zTreeStyle.css">
    <script type="text/javascript" src="${path}/plugins/ztree/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="${path}/plugins/ztree/js/jquery.ztree.all-3.5.min.js"></script>
    <script type="text/javascript">
        //当前的配置信息
        var setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        $(document).ready(function(){
            var fn =function(data){
                //菜单的初始化
                $.fn.zTree.init($("#treeDemo"), setting, data);
                //参1 显示的标签
                //参2 设置的参数 比如支持复选 check enable = true
                //参3 数据
            }
            $.get('${path}/test/getZtreeData.do',fn,'json')
        });
    </script>
</head>
<body>
    <ul id="treeDemo" class="ztree"></ul>
</body>
</html>

controller后台传输数据给页面

@Controller
@RequestMapping("/test")
public class TestZtreeDataController extends BaseController {

    @RequestMapping(path="/getZtreeData",method ={ RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody
    Object getZtreeData(){
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> node1=new HashMap<String,Object>();
        node1.put("id",1);
        node1.put("pId",0);
        node1.put("name","Sass管理");
        node1.put("open",true);
        Map<String,Object> node2=new HashMap<String,Object>();
        node2.put("id",11);
        node2.put("pId",1);
        node2.put("name","企业管理");
        node2.put("open",true);
        node2.put("checked",true);
        Map<String,Object> node3=new HashMap<String,Object>();
        node3.put("id",111);
        node3.put("pId",1);
        node3.put("name","模块管理");
        list.add(node1);
        list.add(node2);
        list.add(node3);
        return list; //list虽然是对象,但会被 @ResponseBody转成json
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值