首页 > 其他分享 >bootstrap-treeview

bootstrap-treeview

时间:2022-09-07 17:05:05浏览次数:92  
标签:node function bootstrap path nodes treeview data options

目录

#文档

https://www.npmjs.com/package/bootstrap-treeview
https://github.com/jonmiles/bootstrap-treeview

#使用
1. 引用
<!-- Required Stylesheets -->
<link href="bootstrap.css" rel="stylesheet">
 
<!-- Required Javascript -->
<script src="jquery.js"></script>
<script src="bootstrap-treeview.js"></script>
2. 定义容器
<div id="categoryTree"></div>
3. 初始化
var expandedNode=null;

$(function() {
    //
    searchCategoryNodes("");
});

function loadNode(data){
    var nodes = getNodeArray(data);
    for(var i = 0 ; i < nodes.length ; i++){
        $("#categoryTree").treeview("addNode", [expandedNode.nodeId, { node: nodes[i]}]);
    }
}

function initCategoryTree(data){
    var me = this;
    $('#categoryTree').treeview({
        levels : 1,
        color : "#515151",
        showBorder: false,
        data : getNodeArray(data),
        onhoverColor:"#EDEDED",
        onNodeSelected:function(event,data){
            
        },
        onNodeExpanded:function(event,data){

            if(data.nodes.length>0){ // 如果存在子节点,删除重新查询
                $("#categoryTree").treeview("deleteChildrenNode", data.nodeId);
            }
            me.expandedNode = data;
            var nodeId = data.nodeRef.replace("workspace://SpacesStore/","");
            alert(data.path);
            me.searchCategoryNodes(data.path);
        },
        onNodeCollapsed:function(event,data){
            
        }
    });
}

function searchCategoryNodes(path){
    
    var me = this;
    $.ajax({
        url:getBasePath()+"/file/searchCategoryNodes",
        type:'POST', //GET
        //async:true,    //或false,是否异步
        data:{
            path:path
        },
        //contentType: "application/json",
        dataType:'json',    //返回的数据格式:json/xml/html/script/jsonp/text
        success:function(data,textStatus,jqXHR){
            if(path == ""){
                initCategoryTree(data);
            }else{
                loadNode(data);
            }
        },
        error:function(xhr,textStatus){
            //error
        },
    });    
}

function getNodePath(nodeName){
    var path = "";
    if (this.expandedNode != null) {
        path = this.expandedNode.path +"/"+nodeName;
    }else{
        path = nodeName;
    }
    return path;
}
function getNodeArray(data){
    
    var array = new Array(),node;
    for (var i = 0; i < data.length; i++) {
        
        var path = getNodePath(data[i].path);
        
        node = {
            nodeRef:data[i].nodeRef,
            text : data[i].name,
            path :getNodePath(data[i].name),
            expandIcon:"glyphicon glyphicon-plus",
            collapseIcon:"glyphicon glyphicon-minus",
            /*"color":"red"*/
            nodes:[] //默认使节点都显示可expandicon,不设置为空。没有子节点不显示expandicon
            
        };
        array.push(node);
    }
    return array;
}
function getBasePath() {
    var location = (window.location + '').split('/');
    var basePath = location[0] + '//' + location[2] + '/' + location[3];
    return basePath;
}
4. 效果图

image

#自定义函数

示例:自定义addNodedeleteChildrenNode方法

  1. Tree主函数return添加以下代码_
// add and delete Children Node
addNode : $.proxy(this.addNode, this),
deleteChildrenNode : $.proxy(this.deleteChildrenNode, this),
  1. 添加Tree的prototype方法
/**
 * 给节点添加子节点
 * 
 * @param {Object|Number}
 *            identifiers - A valid node, node id or array of node
 *            identifiers
 * @param {optional
 *            Object} options.node;
 */
Tree.prototype.addNode = function(identifiers, options) {

    this.forEachIdentifier(identifiers, options, $.proxy(
            function(node, options) {
                this.setAddNode(node, options);
            }, this));
    this.nodes = [];//重置nodes为空,避免重复数据添加。
    this.setInitialStates({
        nodes : this.tree
    }, 0);
    this.render();
}

/**
 * 添加子节点
 */
Tree.prototype.setAddNode = function(node, options) {
    if (node.nodes == null)
        node.nodes = [];
    if (options.node) {
        node.nodes.push(options.node);
    }
    ;
};

/**
 * 删除子节点 置空子节点 刷新节点
 * 
 * @param node
 * @param options
 */
Tree.prototype.deleteChildrenNode = function(identifiers, options) {
    this.forEachIdentifier(identifiers, options, $.proxy(
            function(node, options) {
                if (node.nodes != null) {
                    node.nodes = null;
                    this.setInitialStates({
                        nodes : this.tree
                    }, 0);
                    this.render();
                }
            }, this));
};

标签:node,function,bootstrap,path,nodes,treeview,data,options
From: https://www.cnblogs.com/meideprac/p/16666403.html

相关文章

  • Jx.Cms开发笔记(七)-升级BootstrapBlazor到6.9.x
    由于BootstrapBlazor升级到6.9以后的升级还是非常大的,比如图标库升级到了6.1.2,bs升级到了5.2.0。所以这里记录一下升级过程。升级BootstrapBlazor主程序直接升级BootstarpB......
  • 'documentationPluginsBootstrapper'报错
    springboot2.6.3集成swagger3报错org.springframework.context.ApplicationContextException:Failedtostartbean'documentationPluginsBootstrapper';nestede......
  • documentationPluginsBootstrapper
    springboot集成knife4j时报异常documentationPluginsBootstrapper实际是因为使用springboot2.6.0后,配置swagger,不论是2.9.2还是3.0.0都报错只需要配置如图  mvc:#......
  • 如何使用 Bootstrap 处理 CSS
    如何使用Bootstrap处理CSS大家好!如果您像我一样开始使用CSS编码并使用它进行任何大型项目,那么您肯定会因为响应式布局、溢出和选择器特异性而感到数不清的头痛。这就......
  • 如何使用 Bootstrap 处理 CSS
    如何使用Bootstrap处理CSS大家好!如果您像我一样开始使用CSS编码并使用它进行任何大型项目,那么您肯定会因为响应式布局、溢出和选择器特异性而感到数不清的头痛。这就......
  • Springcloud bootstrap配置时候注册不了服务
    在开发中以前用的是application.yml是可以注册到nacos服务中的,但是改成bootstrap配置后却注册不了服务了。解决方案就是在pom中引入<dependency><groupId>o......
  • jQuery筛选器,bootstrap
    jQuery筛选器方法 jQuery筛选器方法基于当前元素向上,向下等查找元素  1.下一个元素$("#id").next()#id的下一个元素$("#id").next()#id下面的所有......
  • Bootstrap基础介绍一
    前端框架Bootstrap该框架已经帮你写好了很多页面样式,你如果需要使用,只需要下载它对应文件,之后直接cv拷贝即可在使用Bootstrap的时候所有的页面样式都只需要你通过class来......
  • uniGUI学习之UniTreeview(56)
    UniTreeview中能改变一级目录的字体和颜色functionbeforeInit(sender,config){ID="#"+config.id;Ext.util.CSS.createStyleSheet(`${ID}.x-tree-node-text{c......
  • 基于.NET6、FreeSql、若依UI、LayUI、Bootstrap构建插件式的CMS
    近几年,.net生态日益强大,特别是跨平台技术,性能提升,那真的是强大无比。为了日常能够快速开发,笔者基于基于.NET6、FreeSql、若依UI、LayUI、Bootstrap构建插件式的CMS,请大家......