search
HomeWeb Front-endJS TutorialSharing experience in using Bootstrap tree control
Sharing experience in using Bootstrap tree controlDec 26, 2017 pm 01:59 PM
bootstrapshareexperience

jquery tree control is a very lightweight control based on jquery+bootstrap, completely handwritten with js and styles. It has simple functions and good user experience. It is more practical for displaying some simple hierarchical relationships. Trees are used in many projects to show hierarchical relationships, and some trees are used to select items and then call the selected items elsewhere. Today, the editor of Script House brings you experience sharing on using Bootstrap tree control. Friends who need it can refer to it. I hope it can help everyone.

jquery tree control

Preface: Many times we need to use trees in projects. Some trees are just for displaying hierarchical relationships, some are for displaying and editing hierarchical relationships, and some trees are for displaying and editing hierarchical relationships. It is to select the item and then call the selected item elsewhere. No matter what, the tree control is one of the indispensable components in many projects. Today, the blogger plans to share here based on his own experience and some good tree controls found on the Internet, hoping to help everyone find the most suitable control. Still the same sentence: There is no best control, only the most suitable.

1. JQuery tree control

Jquery tree control is a very lightweight control based on JQuery+bootstrap, completely handwritten through js and styles. It can be found in many places on the Internet. See its shadow. It has simple functions and a good user experience. It is more practical to display some simple hierarchical relationships, but it is not easy to add, delete or modify nodes. If you have to do it, you may need to encapsulate it yourself.

1. First glimpse

Collapse all

Expand one level

Expand all

2. Code example

This control is also very simple to implement. You only need to reference jQuery and bootstrap components.

<link>
 <link>

 <script></script>
 <script>
  $(function(){
   $(&#39;.tree li:has(ul)&#39;).addClass(&#39;parent_li&#39;).find(&#39; > span&#39;).attr(&#39;title&#39;, &#39;Collapse this branch&#39;);
   $(&#39;.tree li.parent_li > span&#39;).on(&#39;click&#39;, function (e) {
    var children = $(this).parent(&#39;li.parent_li&#39;).find(&#39; > ul > li&#39;);
   if (children.is(":visible")) {
    children.hide(&#39;fast&#39;);
    $(this).attr(&#39;title&#39;, &#39;Expand this branch&#39;).find(&#39; > i&#39;).addClass(&#39;icon-plus-sign&#39;).removeClass(&#39;icon-minus-sign&#39;);
   } else {
    children.show(&#39;fast&#39;);
    $(this).attr(&#39;title&#39;, &#39;Collapse this branch&#39;).find(&#39; > i&#39;).addClass(&#39;icon-minus-sign&#39;).removeClass(&#39;icon-plus-sign&#39;);
   }
   e.stopPropagation();
   });
  });
 </script>
<p>
 </p>
      
  •    顶级节点1 Goes somewhere   
         
    •     一级节点1     
            
      •      二级节点1_1      
      •    
         
    •    
    •     一级节点2     
            
      •     二级节点2_1      
      •     
      •      二级节点2_2      
               
        •      三级节点2_1       
                  
          •       四级节点2_1        
          •       
          •        四级节点2_2        
          •      
               
        •      
        •      三级节点2_2       
        •      
        •       三级节点2_3       
        •     
            
      •     
      •     二级节点2_3      
      •    
         
    •   
      
  •   
  •    顶级节点2    
         
    •     一级节点2_1     
    •   
      
  •  
 

These codes are downloaded directly from the Internet. The icons on the nodes can be modified through the style, and the icon style is also based on bootstrap. If you need to add nodes dynamically, you can encapsulate the components yourself to create HTML, which should be relatively simple to implement. From the above, we can see that this component is lightweight. If you need to add, delete, modify or select nodes, don't worry, the following controls may be more suitable.

2. File tree editing plug-in Treed

This component was found on the Internet. After the initial demonstration, the members of the group agreed that the effect was great. Because it is displayed in a tree shape, it conveniently provides the addition, deletion and modification of nodes. The scaling effect of nodes is also better. Okay, let’s take a look. Demo and download address: http://www.jq22.com/jquery-info401

1. First sight of Treed

default expansion level

Click the "+" sign on the left and the node on the right to expand the child nodes

Multi-level expansion

On the left, you can edit the node name

Press Enter to wrap the line to add a sibling node, and delete the line wrap to delete the node.

Press the Enter key and then press the Tab key to add a new child node

It is very powerful. Reviewing the element shows that it is implemented using the svg tag of HTML5, so it has certain requirements for the browser.

3. Universal tree control - zTree

ztree is a traditional tree component, and its powerful functions determine its status in the tree control world. Whether it is tree display, editing, or node single and multi-selection, it provides a powerful functional API. However, because its interface is not very good-looking, many companies will hesitate when using it. Fortunately, since the flat style was introduced, ztree has also made certain beautifications. For example, its Metro style components are very similar to the bootstrap style. Today, let’s introduce some usage of this component in combination with Metro-style ztree. Demo and download address: http://www.jq22.com/jquery-info941

1. Component usage

This component can be added directly through Nuget.

2. Component effects

Collapse all nodes

Expand nodes

The node is selected. Of course, it can be turned into a single selection if necessary.

可以增删改节点

点击文本框出现树

3、代码示例

3.1 直接在界面显示文本框

需要引用的js和css文件

 <script></script>
 <script></script>
 <link>
 <script></script>
 <link>

页面html 

<p>
  </p>
     

    Js初始化

    var setting = {
     view: {
     addHoverDom: addHoverDom,
     removeHoverDom: removeHoverDom,
     selectedMulti: false
     },
     check: {
     enable: true
     },
     data: {
     simpleData: {
      enable: true
     }
     },
     edit: {
     enable: false
     }
    };
    var zNodes = [
     { id: 1, pId: 0, name: "父节点1", open: true },
     { id: 11, pId: 1, name: "父节点11" },
     { id: 111, pId: 11, name: "叶子节点111" },
     { id: 112, pId: 11, name: "叶子节点112" },
     { id: 113, pId: 11, name: "叶子节点113" },
     { id: 114, pId: 11, name: "叶子节点114" },
     { id: 12, pId: 1, name: "父节点12" },
     { id: 121, pId: 12, name: "叶子节点121" },
     { id: 122, pId: 12, name: "叶子节点122" },
     { id: 123, pId: 12, name: "叶子节点123" },
     { id: 124, pId: 12, name: "叶子节点124" },
     { id: 13, pId: 1, name: "父节点13", isParent: true },
     { id: 2, pId: 0, name: "父节点2" },
     { id: 21, pId: 2, name: "父节点21", open: true },
     { id: 211, pId: 21, name: "叶子节点211" },
     { id: 212, pId: 21, name: "叶子节点212" },
     { id: 213, pId: 21, name: "叶子节点213" },
     { id: 214, pId: 21, name: "叶子节点214" },
     { id: 22, pId: 2, name: "父节点22" },
     { id: 221, pId: 22, name: "叶子节点221" },
     { id: 222, pId: 22, name: "叶子节点222" },
     { id: 223, pId: 22, name: "叶子节点223" },
     { id: 224, pId: 22, name: "叶子节点224" },
     { id: 23, pId: 2, name: "父节点23" },
     { id: 231, pId: 23, name: "叶子节点231" },
     { id: 232, pId: 23, name: "叶子节点232" },
     { id: 233, pId: 23, name: "叶子节点233" },
     { id: 234, pId: 23, name: "叶子节点234" },
     { id: 3, pId: 0, name: "父节点3", isParent: true }
    ];
    $(document).ready(function () {
     $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });
    function addHoverDom(treeId, treeNode) {
     var sObj = $("#" + treeNode.tId + "_span");
     if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
     var addStr = "<span></span>";
     sObj.after(addStr);
     var btn = $("#addBtn_" + treeNode.tId);
     if (btn) btn.bind("click", function () {
     var zTree = $.fn.zTree.getZTreeObj("treeDemo");
     zTree.addNodes(treeNode, { id: (100 + newCount), pId: treeNode.id, name: "new node" + (newCount++) });
     return false;
     });
    };
    function removeHoverDom(treeId, treeNode) {
     $("#addBtn_" + treeNode.tId).unbind().remove();
    };

    3.2 默认隐藏,触发某个事件的时候出现树。这种场景在项目里面非常常见,一般用于选择树节点。所以博主把这个也单独拿出来说下吧。

    p默认是隐藏的。

        
      <p>
      </p>
        

      在js里面初始化树以及注册showMenu()方法

      var setting2 = {
       check: {
       enable: true,
       chkStyle: "radio",
       radioType: "all"
       },
       view: {
       dblClickExpand: false
       },
       data: {
       simpleData: {
        enable: true
       }
       },
       callback: {
       onClick: onClickNode,
       onCheck: onCheck
       }
      };
      var zNodes = [
       { id: 1, pId: 0, name: "父节点1", open: true },
       { id: 11, pId: 1, name: "父节点11" },
       { id: 111, pId: 11, name: "叶子节点111" },
       { id: 112, pId: 11, name: "叶子节点112" },
       { id: 113, pId: 11, name: "叶子节点113" },
       { id: 114, pId: 11, name: "叶子节点114" },
       { id: 12, pId: 1, name: "父节点12" },
       { id: 121, pId: 12, name: "叶子节点121" },
       { id: 122, pId: 12, name: "叶子节点122" },
       { id: 123, pId: 12, name: "叶子节点123" },
       { id: 124, pId: 12, name: "叶子节点124" },
       { id: 13, pId: 1, name: "父节点13", isParent: true },
       { id: 2, pId: 0, name: "父节点2" },
       { id: 21, pId: 2, name: "父节点21", open: true },
       { id: 211, pId: 21, name: "叶子节点211" },
       { id: 212, pId: 21, name: "叶子节点212" },
       { id: 213, pId: 21, name: "叶子节点213" },
       { id: 214, pId: 21, name: "叶子节点214" },
       { id: 22, pId: 2, name: "父节点22" },
       { id: 221, pId: 22, name: "叶子节点221" },
       { id: 222, pId: 22, name: "叶子节点222" },
       { id: 223, pId: 22, name: "叶子节点223" },
       { id: 224, pId: 22, name: "叶子节点224" },
       { id: 23, pId: 2, name: "父节点23" },
       { id: 231, pId: 23, name: "叶子节点231" },
       { id: 232, pId: 23, name: "叶子节点232" },
       { id: 233, pId: 23, name: "叶子节点233" },
       { id: 234, pId: 23, name: "叶子节点234" },
       { id: 3, pId: 0, name: "父节点3", isParent: true }
      ];
      //初始化
      $(document).ready(function () {
       $.fn.zTree.init($("#treeDemo2"), setting2, zNodes);
      });
      //显示菜单
      function showMenu() {
       $("#menuContent2").css({ left: "15px", top: "34px" }).slideDown("fast");
       $("body").bind("mousedown", onBodyDown);
      }
      //隐藏菜单
      function hideMenu() {
       $("#menuContent2").fadeOut("fast");
       $("body").unbind("mousedown", onBodyDown);
      }
      function onBodyDown(event) {
       if (!(event.target.id == "menuBtn" || event.target.id == "menuContent2" || event.target.id == "txt_ztree" || $(event.target).parents("#menuContent2").length > 0)) {
       hideMenu();
       }
      }
      //节点点击事件
      function onClickNode(e, treeId, treeNode) {
       var zTree = $.fn.zTree.getZTreeObj("treeDemo");
       zTree.checkNode(treeNode, !treeNode.checked, null, true);
       return false;
      }
      //节点选择事件
      function onCheck(e, treeId, treeNode) {
       var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
       nodes = zTree.getCheckedNodes(true),
       v = "";
       var parentid = "";
       var parentlevel = "";
       for (var i = 0, l = nodes.length; i  0) {
       v = v.substring(0, v.length - 1);
       parentid = parentid.substring(0, parentid.length - 1);
       parentlevel = parentlevel.substring(0, parentlevel.length - 1);
       }
       else {
       return;
       }
       hideMenu();
      }

      由于以上都是静态数据,如果需要从后台取数据动态加载树节点,可以去后台构造zNodes这种结构的数组即可。

      四、总结

      以上三种树形组件,各有千秋,可以根据需求选用不同的组件。

      相关推荐:

      JS组件系列--Bootstrap 树控件使用经验分享_html/css_WEB-ITnose

      javascript 树控件 比较好用_javascript技巧

      基于jQuery的树控件实现代码(asp.net+json)_jquery

      The above is the detailed content of Sharing experience in using Bootstrap tree control. For more information, please follow other related articles on the PHP Chinese website!

      Statement
      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
      10款好看又实用的Bootstrap后台管理系统模板(快来下载)10款好看又实用的Bootstrap后台管理系统模板(快来下载)Aug 06, 2021 pm 01:55 PM

      一个好的网站,不能只看外表,网站后台同样很重要。本篇文章给大家分享10款好看又实用的Bootstrap后台管理系统模板,可以帮助大家快速建立强大有美观的网站后台,欢迎下载使用!如果想要获取更多后端模板,请关注php中文网后端模板栏目!

      bootstrap与jquery是什么关系bootstrap与jquery是什么关系Aug 01, 2022 pm 06:02 PM

      bootstrap与jquery的关系是:bootstrap是基于jquery结合了其他技术的前端框架。bootstrap用于快速开发Web应用程序和网站,jquery是一个兼容多浏览器的javascript库,bootstrap是基于HTML、CSS、JAVASCRIPT的。

      7款实用响应式Bootstrap电商源码模板(快来下载)7款实用响应式Bootstrap电商源码模板(快来下载)Aug 31, 2021 pm 02:13 PM

      好看又实用的Bootstrap电商源码模板可以提高建站效率,下面本文给大家分享7款实用响应式Bootstrap电商源码,均可免费下载,欢迎大家使用!更多电商源码模板,请关注php中文网电商源码​栏目!

      8款Bootstrap企业公司网站模板(源码免费下载)8款Bootstrap企业公司网站模板(源码免费下载)Aug 24, 2021 pm 04:35 PM

      好看又实用的企业公司网站模板可以提高您的建站效率,下面PHP中文网为大家分享8款Bootstrap企业公司网站模板,均可免费下载,欢迎大家使用!更多企业站源码模板,请关注php中文网企业站源码栏目!

      bootstrap中sm是什么意思bootstrap中sm是什么意思May 06, 2022 pm 06:35 PM

      在bootstrap中,sm是“小”的意思,是small的缩写;sm常用于表示栅格类“.col-sm-*”,是小屏幕设备类的意思,表示显示大小大于等于768px并且小于992px的屏幕设备,类似平板设备。

      bootstrap默认字体大小是多少bootstrap默认字体大小是多少Aug 22, 2022 pm 04:34 PM

      bootstrap默认字体大小是“14px”;Bootstrap是一个基于HTML、CSS、JavaScript的开源框架,用于快速构建基于PC端和移动端设备的响应式web页面,并且默认的行高为“20px”,p元素行高为“10px”。

      bootstrap是免费的吗bootstrap是免费的吗Jun 21, 2022 pm 05:31 PM

      bootstrap是免费的;bootstrap是美国Twitter公司的设计师“Mark Otto”和“Jacob Thornton”合作基于HTML、CSS、JavaScript 开发的简洁、直观、强悍的前端开发框架,开发完成后在2011年8月就在GitHub上发布了,并且开源免费。

      bootstrap modal 如何关闭bootstrap modal 如何关闭Dec 07, 2020 am 09:41 AM

      bootstrap modal关闭的方法:1、连接好bootstrap的插件;2、给按钮绑定模态框事件;3、通过“ $('#myModal').modal('hide');”方法手动关闭模态框即可。

      See all articles

      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

      AI Hentai Generator

      AI Hentai Generator

      Generate AI Hentai for free.

      Hot Article

      R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
      2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
      R.E.P.O. Best Graphic Settings
      2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
      R.E.P.O. How to Fix Audio if You Can't Hear Anyone
      2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

      Hot Tools

      PhpStorm Mac version

      PhpStorm Mac version

      The latest (2018.2.1) professional PHP integrated development tool

      DVWA

      DVWA

      Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

      SecLists

      SecLists

      SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

      Safe Exam Browser

      Safe Exam Browser

      Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

      MinGW - Minimalist GNU for Windows

      MinGW - Minimalist GNU for Windows

      This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.