Ztree组件

本项目简介。

老大让我们在页面左侧做一颗查询树。看了很多文档再结合自己项目最终做了出来。点击节点可查询数据。

效果如下
左侧查询树

组件依赖

在JS和html内需要加载所用组件的一些依赖项。可以去Tree官网查询,或者layui查看。
Ztree
layui

JS

在这里插入图片描述
tempPath传参数
点击节点查询功能
接口,初始化

html

加入组件

Controller

  @RequestMapping(value = "/tree")
    @ResponseBody
    public synchronized List<ZTreeNode> tree() {
        List<ZTreeNode> tree = new ArrayList<ZTreeNode>();
        ShiroUser user = ShiroKit.getUser();
        List<SrPerspective> srPerspectiveList = this.srPerspectiveService.GetTree(user.getId());
        String temp ="";
        String sum="";
        String fullparentPath="";
        ZTreeNode node = new ZTreeNode();
        node.setChecked(true);
        //子节点
        node.setId(0L);
        //节点名称
        node.setName("顶级");
        node.setOpen(true);
        //父节点
        node.setPId(0L);
        //路径
        node.setFullPath("");
        tree.add(node);
        for(SrPerspective srPerspective : srPerspectiveList){
            try{
                String[] aftersplit = srPerspective.getReqPath().split("\\\\");
                for(int i = 0; i <aftersplit.length; i++) {
                    temp = aftersplit[i] + "\\";
                    sum += temp;
                    //fullparentPath这个路径会比FULLPATH少一层
                    fullparentPath = "";
                    for(int j=0;j<i;j++){
                        fullparentPath += (aftersplit[j] + "\\");
                    }
                    if (nodefound(sum, tree) == true) {
                    } else {
                        ZTreeNode zTreeNode = new ZTreeNode();
                        zTreeNode.setFullPath(sum);
                        zTreeNode.setName(aftersplit[i]);
                        //size加一是因为有顶级节点
                        zTreeNode.setId((long)(tree.size()+1));
                        zTreeNode.setPId((long)getNodeId(fullparentPath,tree));
                        tree.add(zTreeNode);
                    }
                }
                sum="";
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return tree;
    }
    
    //判断节点是否重复
    //思路就是把完整for(i)循环的路径是否存在作为判断
    private boolean nodefound(String fullPath,List<ZTreeNode> zTreeNodeList){
        for(ZTreeNode node : zTreeNodeList){
            if(node.getFullPath().equals(fullPath)){
                return true;
            }
        }
        return false;
    }

	//获取当前节点ID,少一层目录的fullparentPath(for(j)循环的)与完整fullPath相同获取其ID作为父节点
    private int getNodeId(String fullparentPath,List<ZTreeNode> zTreeNodeList){
        for (ZTreeNode node : zTreeNodeList){
            if (node.getFullPath().equals(fullparentPath)){
                return node.getId().intValue();
            }
        }
        return 0;
    }

Service

提取数据库中的字段,并加权限控制。

    public List<SrPerspective> GetTree(Long userId) {
        List<SrPerspective> srPerspectiveList = this.baseMapper.selectReqPath(userId);
        return srPerspectiveList;
    }

Mapper

mapper.java

同样的,权限控制加一下。

 List<SrPerspective> selectReqPath(@Param("userId") Long userId);

mapper.xml

查找切割数据。

 <select id="selectReqPath" resultMap="BaseResultMap">
      select V_SJ_SR_PERSPECTIVE.REQ_PATH
  from V_SJ_SR_PERSPECTIVE , SJ_PP_MEMBER
        WHERE V_SJ_SR_PERSPECTIVE.PROJECT_NO = SJ_PP_MEMBER.PROJECT_NO
        and SJ_PP_MEMBER.USER_ID = #{userId}
 </select>

查询页面数据,tempath做对比

    <select id="selectPage" resultType="map">
        select distinct
        <include refid="Base_Column_List"/>
        from V_SJ_SR_PERSPECTIVE, SJ_PP_MEMBER
        WHERE V_SJ_SR_PERSPECTIVE.PROJECT_NO = SJ_PP_MEMBER.PROJECT_NO
        and SJ_PP_MEMBER.USER_ID = #{userId}
        <if test="name != null and name != ''">
            and V_SJ_SR_PERSPECTIVE.FUNC_MODULE like CONCAT(CONCAT('%',#{name}),'%')
        </if>
   <if test="tempPath != null and tempPath != ''">
            and CONCAT(V_SJ_SR_PERSPECTIVE.REQ_PATH ,'\\')like CONCAT(#{tempPath},'%')
        </if>
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值