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)' //这是我本地测试的颜色 可以略过
                    });
            }
    });      
}

 

posted @ 2018-07-31 15:23  礼拜16  阅读(5482)  评论(0编辑  收藏  举报