Home Web Front-end Bootstrap Tutorial A brief discussion on how to use the styled tree plug-in bootstrap-treeview

A brief discussion on how to use the styled tree plug-in bootstrap-treeview

Jan 29, 2021 am 10:02 AM
bootstrap

A brief discussion on how to use the styled tree plug-in bootstrap-treeview

Related recommendations: "bootstrap basic tutorial"

bootstrap-treeview is a very cool jQuery multi-view based on bootstrap Level list tree plug-in, this jQuery plug-in is based on Twitter Bootstrap. The purpose of writing this article is to record the usage method for future reference.

1. Plug-in official website: https://jquery-plugins.net/bootstrap-tree-view
2. Demo example: http:// jonmiles.github.io/bootstrap-treeview/

3. Dependencies:

Bootstrap v3.3.4 (>= 3.0.0)
jQuery v2.1.3 (>= 1.9.0)
Copy after login

4. Usage:

1. HTML file introduces dependencies:

<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/bootstrap-treeview.min.css" />
<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js" ></script>
<script type="text/javascript" src="js/bootstrap-treeview.min.js" ></script>
Copy after login

2. Sets a DOM element to receive tree data:

<div id="tree"></div>
Copy after login

3. Usage format:

$(&#39;#tree&#39;).treeview(options);  //其中options选项允许用户定制treeview的默认外观和行为。它们在初始化时作为一个对象被传递给插件。
Copy after login

4. Usage example ( js dynamically obtains background data and renders the tree structure):

$(function() {
    $(&#39;#tree&#39;).treeview({
        data: getTree()//节点数据
    });
})

function getTree() {
    //节点上的数据遵循如下的格式:
    var tree = [{
        text: "Node 1", //节点显示的文本值  string
        icon: "glyphicon glyphicon-play-circle", //节点上显示的图标,支持bootstrap的图标  string
        selectedIcon: "glyphicon glyphicon-ok", //节点被选中时显示的图标       string
        color: "#ff0000", //节点的前景色      string
        backColor: "#1606ec", //节点的背景色      string
        href: "#http://www.baidu.com", //节点上的超链接
        selectable: true, //标记节点是否可以选择。false表示节点应该作为扩展标题,不会触发选择事件。  string
        state: { //描述节点的初始状态    Object
            checked: true, //是否选中节点
            /*disabled: true,*/ //是否禁用节点
            expanded: true, //是否展开节点
            selected: true //是否选中节点
        },
        tags: [&#39;标签信息1&#39;, &#39;标签信息2&#39;], //向节点的右侧添加附加信息(类似与boostrap的徽章)    Array of Strings
        nodes: [{
            text: "Child 1",
            nodes: [{
                text: "Grandchild 1"
            }, {
                text: "Grandchild 2"
            }]
        }, {
            text: "Child 2"
        }]
    }, {
        text: "Parent 2",
        nodes: [{
            text: "Child 2",
            nodes: [{
                text: "Grandchild 3"
            }, {
                text: "Grandchild 4"
            }]
        }, {
            text: "Child 2"
        }]
    }, {
        text: "Parent 3"
    }, {
        text: "Parent 4"
    }, {
        text: "Parent 5"
    }];

    return tree;
}
Copy after login

5. Other instructions:

1. Detailed explanation of parameters (available parameters):

var options = {
    data: data, //data属性是必须的,是一个对象数组    Array of Objects.
    color: "", //所有节点使用的默认前景色,这个颜色会被节点数据上的backColor属性覆盖.        String
    backColor: "#000000", //所有节点使用的默认背景色,这个颜色会被节点数据上的backColor属性覆盖.     String
    borderColor: "#000000", //边框颜色。如果不想要可见的边框,则可以设置showBorder为false。        String
    nodeIcon: "glyphicon glyphicon-stop", //所有节点的默认图标
    checkedIcon: "glyphicon glyphicon-check", //节点被选中时显示的图标         String
    collapseIcon: "glyphicon glyphicon-minus", //节点被折叠时显示的图标        String
    expandIcon: "glyphicon glyphicon-plus", //节点展开时显示的图标        String
    emptyIcon: "glyphicon", //当节点没有子节点的时候显示的图标              String
    enableLinks: false, //是否将节点文本呈现为超链接。前提是在每个节点基础上,必须在数据结构中提供href值。        Boolean
    highlightSearchResults: true, //是否高亮显示被选中的节点        Boolean
    levels: 2, //设置整棵树的层级数  Integer
    multiSelect: false, //是否可以同时选择多个节点      Boolean
    onhoverColor: "#F5F5F5", //光标停在节点上激活的默认背景色      String
    selectedIcon: "glyphicon glyphicon-stop", //节点被选中时显示的图标     String

    searchResultBackColor: "", //当节点被选中时的背景色
    searchResultColor: "", //当节点被选中时的前景色

    selectedBackColor: "", //当节点被选中时的背景色
    selectedColor: "#FFFFFF", //当节点被选中时的前景色

    showBorder: true, //是否在节点周围显示边框
    showCheckbox: false, //是否在节点上显示复选框
    showIcon: true, //是否显示节点图标
    showTags: false, //是否显示每个节点右侧的标记。前提是这个标记必须在每个节点基础上提供数据结构中的值。
    uncheckedIcon: "glyphicon glyphicon-unchecked", //未选中的复选框时显示的图标,可以与showCheckbox一起使用
}
Copy after login

2. Detailed explanation of methods (list of available methods):

1.  checkAll(options);//选中所有树节点
2.  checkNode(node | nodeId, options);  //选中一个给定nodeId的树节点
3.  clearSearch();//清除查询结果
4.  collapseAll(options);//折叠所有树节点
5.  collapseNode(node | nodeId, options);//折叠一个给定nodeId的树节点和它的子节点
6.  disableAll(options);//禁用所有树节点
7.  disableNode(node | nodeId, options);//禁用一个给定nodeId的树节点
8.  enableAll(options);//激活所有树节点
9.  enableNode(node | nodeId, options);//激活给定nodeId的树节点
10. expandAll(options);//展开所有节点
11. expandNode(node | nodeId, options);//展开给定nodeId的树节点
12. getCollapsed();//返回被折叠的树节点数组
13. getDisabled();//返回被禁用的树节点数组
14. getEnabled();//返回被激活的树节点数组  
15. getExpanded();//返回被展开的树节点数组
16. getNode(nodeId);//返回与给定节点id相匹配的单个节点对象。
17. getParent(node | nodeId);//返回给定节点id的父节点
18. getSelected();//返回被选定节点的数组。
19. getSiblings(node | nodeId);//返回给定节点的兄弟节点数组
20. getUnselected();//返回未选择节点的数组
21. remove();//删除the tree view component.删除绑定的事件,内部附加的对象,并添加HTML元素。
22. revealNode(node | nodeId, options);//显示给定的树节点,将树从节点扩展到根。
23. search(pattern, options);//在树视图中搜索匹配给定字符串的节点,并在树中突出显示它们。返回匹配节点的数组。
24. selectNode(node | nodeId, options);//选择一个给定的树节点
25. toggleNodeChecked(node | nodeId, options);//Toggles a nodes checked state; checking if unchecked, unchecking if checked.
26. toggleNodeDisabled(node | nodeId, options);//切换节点的禁用状态;
27. toggleNodeExpanded(node | nodeId, options);//切换节点的展开与折叠状态
28. toggleNodeSelected(node | nodeId, options);//切换节点的选择状态
29. uncheckAll(options);//不选所有节点
30. uncheckNode(node | nodeId, options);//不选给定nodeId的节点
31. unselectNode(node | nodeId, options);//不选给定nodeId的节点

说明:可以通过两种方式来调用方法:

1、插件包装器:插件的包装器可以作为访问底层方法的代理。

$(&#39;#tree&#39;).treeview(&#39;methodName&#39;, args);  

其中,多个参数必须使用数组对象来传入。

2、直接使用treeview:你可以通过下面两种方法中的一种来获取treeview对象实例:

//该方法返回一个treeview的对象实例
$(&#39;#tree&#39;).treeview(true).methodName(args);

//对象实例也保存在DOM元素的data中, 可以使用&#39;treeview&#39;的id来访问它。
$(&#39;#tree&#39;).data(&#39;treeview&#39;).methodName(args);
Copy after login

3. Detailed explanation of events (list of available events):

1.  nodeChecked (event, node) - 一个节点被checked.
2.  nodeUnchecked (event, node) - 一个节点被unchecked.
3.  nodeCollapsed (event, node) - 一个节点被折叠.
4.  nodeDisabled (event, node) - 一个节点被禁用.
5.  nodeEnabled (event, node) - 一个节点被启用.
6.  nodeExpanded (event, node) - 一个节点被展开.
7.  nodeSelected (event, node) - 一个节点被选择.
8.  nodeUnselected (event, node) - 取消选择一个节点.
9.  searchComplete (event, results) - 搜索完成之后触发.
10. searchCleared (event, results) - 搜索结果被清除之后触发.

说明:事件的调用有两种方式:

第 1 种:在参数中使用回调函数来绑定任何事件:
$(&#39;#tree&#39;).treeview({
    //命名约定:以on为前缀,并将事件名的第一个字母转为大写,例如: nodeSelected -> onNodeSelected
    onNodeSelected:function(event, data) {
        // 事件代码...
    }
});      

第 2 种:使用标准的jQuery .on()方法来绑定事件:
$(&#39;#tree&#39;).on(&#39;nodeSelected&#39;,function(event, data) {
    // 事件代码...
});
Copy after login

6. Complete demo download

https://download.csdn.net/download/security_2015/10281802

For more programming-related knowledge, please visit: Programming Learning! !

The above is the detailed content of A brief discussion on how to use the styled tree plug-in bootstrap-treeview. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles