bootstrap-treeview 在 bootstrap 4 不兼容解决办法及使用

bootstrap-treeview 是bootstrap的一个树形插件,插件依赖:

bootstrap/3.3.7
jquery/3.3.1

经过验证,它不可以在 bootstrap 高于 3.3.7 版本中使用,当前 treeview 的版本为 bootstrap-treeview/1.2.0  ,  bootstrap/3.3.7

<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap-treeview/1.2.0/bootstrap-treeview.min.js"></script>

其实它这个不兼容是因为这个插件用了 版本3 的图标,而 bootstrap 4 却把这些图标给干掉了,因此需要手动添加这些图标文件 Glyphicons.scss:

@charset 'utf-8';

at-root {
  // Import the fonts
  @font-face {
      font-family: 'Glyphicons Halflings';
      src:  url('../font/bootstrap/glyphicons-halflings-regular.eot');
      src:  url('../font/bootstrap/glyphicons-halflings-regular.eot') format('embedded-opentype'),
        url('../font/bootstrap/glyphicons-halflings-regular.ttf') format('truetype'),
        url('../font/bootstrap/glyphicons-halflings-regular.woff') format('woff'),
        url('../font/bootstrap/glyphicons-halflings-regular.woff2') format('woff2'),
        url('../font/bootstrap/glyphicons-halflings-regular.svg') format('svg');
      font-weight: normal;
      font-style: normal;
  }
}

// Catchall baseclass
.glyphicon {
  position: relative;
  top: 1px;
  display: inline-block;
  font-family: 'Glyphicons Halflings';
  font-style: normal;
  font-weight: normal;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

bootstrap 3 国内打不开不了,这是个英文版,需要FQ:https://getbootstrap.com/docs/3.3/getting-started/ 去下载。

 

在html中,只写一个存放的文件树的容器即可:

<div id="tree"></div>

假定先做一个假数据,使用 tree.json:

[
  {
    "text":"Bakersfield office",
    "nodes": [
      {
        "text":"Bakersfield BD"
      },
      {
        "text":"Bakersfield marketing"
      },
      {
          "text":"Bakersfield HR"
      }
    ]
  },
  {
    "text":"Stockton office",
    "nodes": [
            {
                "text":"Stockton  service"
            },
            {
                "text":"Stockton After sales"
            },
            {
                "text":"Stockton Field service"
            },
            {
                "text":"Stockton Finance"
            },
            {
                "text":"Stockton HR"
            }
    ]
  },
  {
    "text":"Chesapeake office HR"
  }
]               

然后使用 ajax 将数据填入 #tree 的容器中:

function getTree(){
  var url = 'http://127.0.0.1:8020/localhost-cloudClocking-CMS/js/tree.json';
       
        $.ajax({
            type:"get",
            url:url,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function(data) {
                    console.log(data);
                    $('#tree').treeview({
                        levels: 1,
                        data:data,
                        selectedBackColor: 'rgba(#000000,.12)' //这是我本地测试的颜色 可以略过
                    });
            }
    });      
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bootstrap TreeviewBootstrap 分栏可以很容易地结合使用,以创建一个具有多级层次结构的导航菜单。 下面是一个简单的示例: ```html <div class="container"> <div class="row"> <div class="col-md-4"> <div id="treeview"></div> </div> <div class="col-md-8"> <h1>Content</h1> </div> </div> </div> ``` 在这个示例中,我们使用Bootstrap 的分栏组件来创建一个两列布局。左侧列占据了 4 个网格,右侧列占据了 8 个网格。 左侧列中的 div 元素使用了 id="treeview" 属性,这是我们将要使用 Bootstrap Treeview 插件渲染的容器。 接下来,我们需要引入 BootstrapBootstrap Treeview 的 CSS 和 JavaScript 文件,并在 JavaScript 中初始化 Treeview 插件: ```html <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.css"> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.js"></script> <script> $(function() { var data = [ { text: 'Parent 1', nodes: [ { text: 'Child 1', nodes: [ { text: 'Grandchild 1' }, { text: 'Grandchild 2' } ] }, { text: 'Child 2' } ] }, { text: 'Parent 2' }, { text: 'Parent 3' } ]; $('#treeview').treeview({data: data}); }); </script> ``` 在这个示例中,我们创建了一个包含多级层次结构的数据对象,并将其传递给 Treeview 插件的 data 选项。 最后,我们在 JavaScript使用 $('#treeview').treeview({data: data}) 方法将 Treeview 插件应用于 id="treeview" 的 div 元素。 这样,我们就可以在左侧列中创建一个具有多级层次结构的导航菜单,而右侧列中可以放置任何内容,例如显示所选菜单项的详细信息、表单等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值