• bootstrap-treeview的 简单使用


    理论:http://blog.csdn.net/babyxue/article/details/73835444

    插依赖Bootstrap 和jQuery

    <link href="~/bootstrap-treeview.css" rel="stylesheet" />
    <link href="./css/bootstrap.css" rel="stylesheet">
    <script src="./js/jquery.js"></script>
    <script src="./js/bootstrap-treeview.js"></script>

    jsp中

    <div class="col-sm-3">
    <input type="hidden" id="powerList" name="powerList" value='${powerList}'/>
    <input type="hidden" id="ids" name="ids" value="${ids}" />
    <c:if test="${powerList!=null}">
    <div id="tree"></div>
    </c:if>
    </div>

    js中

    var log = {
        initTree: function () {
            var data = $('#powerList').val();        
            $('#tree').treeview({
                  data: data,
                  showCheckbox:false,
                  multiSelect:false,
                  onNodeSelected:function(event,node){ //选中节点其子节点,父节点
                      var ids = [];
                      var parentNode = $('#tree').treeview('getParent',node); //父节点//              
                      var nodes = node.nodes; //子节点
                      if(nodes){
                          for(var i=0; i<nodes.length; i++){
                              ids.push(nodes[i].id)
                              var childNode = nodes[i].nodes;
                              if(childNode){
                                  for(var j=0; j<childNode.length; j++){
                                      ids.push(childNode[j].id)
                                  }
                              }                        
                          }
                      }else{
                          ids.push(node.id)
                      }
                      //console.info(ids);
                      $('#ids').val(ids);
                      setTimeout(function () {
                        log.reload();
                    }, 1000)                                
                  },
                  onNodeUnselected:function(event,node){
                    $('#ids').val('');
                    setTimeout(function () {
                        log.reload();
                    }, 1000)  
                  }
              });
          
         }
    };

    这里得到节点的id的时候,有点小问题

    @RequestMapping(value = "view")
        public ModelAndView toLog(ModelAndView modelAndView) throws Exception {
            modelAndView.setViewName("system/log");
            try{
                List<TreeObject> list = getTree(Long.parseLong("-1"));
                TreeObject treeObject = new TreeObject();
                treeObject.setId(-1L);
                treeObject.setText("全部菜单");
                State rootState = new State();
                rootState.setExpanded(true);
                rootState.setChecked(false);
                treeObject.setState(rootState);            
                
                //只显示递归产生的两层
                TreeObject treeObj = new TreeObject();
                for(int i = 0; i < list.size(); i++){
                    List<TreeObject> list2 = list.get(i).getNodes();
                    if(list2 != null && list2.size() > 0){
                        for(int j = 0; j < list2.size(); j++){
                            if(list2.get(j).getNodes() != null){
                                list2.get(j).setNodes(null);
                            }
                        }
                    }
                }            
                treeObject.setNodes(list);
                modelAndView.addObject("powerList", "[" + JSON.toJSONString(treeObject) + "]");
            }catch(Exception ex){
                logger.error("进入日志管理页面出错!",ex);
            }
            return modelAndView;
        }
        
        //递归树
        public List<TreeObject> getTree(Long parentid) {
            List<TreeObject> list = new ArrayList<TreeObject>();
            List<TreeObject> total = new ArrayList<TreeObject>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("parent_id", parentid);
            map.put("sys_type", 1);
            List<Power> powerList = powerService.getPowerByGroupLevel(map);
            for (Power power : powerList) {
                TreeObject tb = new TreeObject();
                tb.setId(power.getPower_id());
                tb.setText(power.getPower_name());
                State state = new State();
                state.setChecked(false);
                state.setExpanded(false);
                if (power.getChildcnt() > 0) {
                    List<TreeObject> child = getTree(power.getPower_id());
                    tb.setNodes(child);
                }
                tb.setState(state);
                list.add(tb);            
            }
            total.addAll(list);
            return total;
        }    
  • 相关阅读:
    重构前的程序:通过rsync命令抓取日志文件
    标准输入、输出和错误和文件重定向
    错误处理的思考
    测试和恢复性的争论:面向对象vs.函数式编程
    哈佛经济学家关于工作效率的意外发现
    追求代码质量: 监视圈复杂度
    天猫程序猿高端算法找妹子
    代码度量工具——SourceMonitor的学习和使用
    我们能从java的HelloWorld学到什么?
    Java高新技术第二篇:反射技术
  • 原文地址:https://www.cnblogs.com/bluedeblog/p/7525455.html
Copyright © 2020-2023  润新知