栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

Ztree新增角色和编辑角色回显问题的解决

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Ztree新增角色和编辑角色回显问题的解决

最近在项目中使用到了ztree,在回显时候费了点时间,特记录下来供下次参考。

1、新增角色使用ztree加载权限,由于权限不多,所以使用直接全部加载。

效果图:

具体涉及ztree代码:

jsp中导入:/js/ztree/zTreeStyle.css和js/ztree/jquery.ztree.all-3.5.js

页面加入

    js代码(此js中加入了layer弹框效果):

    
    

    权限中Ztree格式: 

    private String id; 
    private String name;
    private String code;
    private String description;
     
    // private String page; 
    //private String generatemenu; 
    //private String zindex;
     
    private String pid; 
    private boolean isParent;
     
    //ztree组件需要格式
    public String getpId() {
       return this.pid==null?"0":this.pid;
     }
     
     ......
      
    

    2、编辑角色回显Ztree

    js代码:

    
    

    java后台:

    controller:

    
      @RequestMapping(value = "queryFunByRoleId", method = RequestMethod.POST)
      public ResponseEntity>> queryFunByRoleId(String roleId) {
        try {
          if(StringUtils.isBlank(roleId)){
     // 返回400
     return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
          }
          return ResponseEntity.ok(sysService.queryFunByRoleId(roleId));
        } catch (Exception e) {
          e.printStackTrace();
        }
        // 出错 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
      }
    

    service:

    由于List中的contains方法校验老是失败,也没纠结什么原因,自己写的根据id校验

    
      @Transactional(readonly=true)
      public List> queryFunByRoleId(String roleId) {
        //查询所有权限
        List functions = queryAllAuthFunction();
        //查询指定角色的权限
        List functionsByRoleId = findFunctionByRoleId(roleId);
        //包装zTree
         
        List> list =new ArrayList>();
        Mapmap=null;
        for(int i=0;i();
          //Role role=functions.get(i);
          AuthFunction fun = functions.get(i);
          map.put("id", fun.getId());
          map.put("pId", fun.getpId());
          map.put("name", fun.getName());
          map.put("isParent", fun.getIsParent());
          //判断指定用户的角色是否在所有角色中包含,有则设置checked=true.
          if(functionsByRoleId != null && functionsByRoleId.size()>0 && ListIsContainsObj(functionsByRoleId,fun)){
     map.put("checked",true);
          }else {
     map.put("checked",false);
          }
          list.add(map);
        }
        return list;
      }
       
      //校验全部权限中是否有某个权限,有返回true
      private boolean ListIsContainsObj(List functions, AuthFunction fun) {
        if(fun == null || functions == null || functions.size()<=0){
          return false;
        }
        for (AuthFunction authFunction : functions) {
          if(fun.getId().equals(authFunction.getId())){
     return true;
          }
        }
        return false;
      }
    
    

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/93232.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号