in this tutorial, I am going to tell you about treeview bootstrap.I will implement json/array based tree view menu using bootstrap 4.
The Treeview help to create data o information into into hierarchical format.there would be a single root element and n number parents and childs.The Parent can have a leaf element or element that have another child leaf.
I am using Boostrap 3 treeview as a base tree view, I will convert this treeview into the Bootstrap 4 view based.
As we know, The bootstrap 4 does not own any icon library as like bootstrap 3 have font-awesome.You can use free icon libraries to choose from, such as Font Awesome and Google Material Design Icons.
But one is alternative solution to integrate font-awesome with boostrap 4.You need to add below font-awesome icon libs cdn path into the head section of html file.
Bootstrap Treeview Example
We will create bootstrap treeview example using bootstrap4 and font-awesome.I am taking static data but you can convert into dynamic tree menu list using any programming language like PHP,nodejs,Python etc.This tutorial have following dependencies -
<ul> <li>Bootstrap 4</li> <li>jQuery</li> <li>Font-awesome</li> </ul>
Let's create index.html file and write all the code into the file.
1 - Import Dependency Libs
import the bootstrap 4 css/js and font-awesome libraries into the head section fo the index.html file.
<link href="cdnjs.cloudflare.com/ajax/libs/twitter-boo…" rel="stylesheet">
<link rel="stylesheet" href="use.fontawesome.com/releases/v5.7.0/css/al…" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="cdnjs.cloudflare.com/ajax/libs/jquery/3.4.…"></script>
2 - Create HTML Tree View
We will add HTML element that will display treeview bootstrap menu.
<div class="row">
<div class="col-md-4">
<ul id="tree1">
</p>
<li>
<a href="#">TECH</a>
<ul>
<li>Company Maintenance</li>
<li>
Employees
<ul>
<li>
Reports
<ul>
<li>Report1</li>
<li>Report2</li>
<li>Report3</li>
</ul>
</li>
<li>Employee Maint.</li>
</ul>
</li>
<li>Human Resources</li>
</ul>
</li>
<li>
XRP
<ul>
<li>Company Maintenance</li>
<li>
Employees
<ul>
<li>
Reports
<ul>
<li>Report1</li>
<li>Report2</li>
<li>Report3</li>
</ul>
</li>
<li>Employee Maint.</li>
</ul>
</li>
<li>Human Resources</li>
</ul>
</li>
</ul>
</div>
<div class="col-md-4">
<ul id="tree2">
<li>
<a href="#">TECH</a>
<ul>
<li>Company Maintenance</li>
<li>
Employees
<ul>
<li>
Reports
<ul>
<li>Report1</li>
<li>Report2</li>
<li>Report3</li>
</ul>
</li>
<li>Employee Maint.</li>
</ul>
</li>
<li>Human Resources</li>
</ul>
</li>
<li>
XRP
<ul>
<li>Company Maintenance</li>
<li>
Employees
<ul>
<li>
Reports
<ul>
<li>Report1</li>
<li>Report2</li>
<li>Report3</li>
</ul>
</li>
<li>Employee Maint.</li>
</ul>
</li>
<li>Human Resources</li>
</ul>
</li>
</ul>
</div>
</div>
3 - Added JQuery code hide and show Bootstrap menu
We will write some jquery code.we will add and remove class using jquery method.
</script>
$.fn.extend({
treed: function (o) {
var openedClass = 'fa-minus-circle';
var closedClass = 'fa-plus-circle';
if (typeof o != 'undefined'){
if (typeof o.openedClass != 'undefined'){
openedClass = o.openedClass;
}
if (typeof o.closedClass != 'undefined'){
closedClass = o.closedClass;
}
};
//initialize each of the top levels
var tree = $(this);
tree.addClass("tree");
tree.find('li').has("ul").each(function () {
var branch = $(this); //li with children ul
branch.prepend("<i class='indicator fas " + closedClass + "'></i>");
branch.addClass('branch');
branch.on('click', function (e) {
if (this == e.target) {
var icon = $(this).children('i:first');
icon.toggleClass(openedClass + " " + closedClass);
$(this).children().children().toggle();
}
})
branch.children().children().toggle();
});
//fire event from the dynamically added icon
tree.find('.branch .indicator').each(function(){
$(this).on('click', function () {
$(this).closest('li').click();
});
});
//fire event to open branch if the li contains an anchor instead of text
tree.find('.branch>a').each(function () {
$(this).on('click', function (e) {
$(this).closest('li').click();
e.preventDefault();
});
});
//fire event to open branch if the li contains a button instead of text
tree.find('.branch>button').each(function () {
$(this).on('click', function (e) {
$(this).closest('li').click();
e.preventDefault();
});
});
}
});
//Initialization of treeviews
$('#tree1').treed();
$('#tree2').treed({openedClass:'fa-folder-open', closedClass:'fa-folder'});
</script>
4- CSS styles
We will write some css styles into the head section of index.html file.
.tree, .tree ul {
margin:0;
padding:0;
list-style:none;
margin-left:10px;
}
.tree ul {
margin-left:1em;
position:relative
}
.tree ul ul {
margin-left:.5em
}
.tree ul:before {
content:"";
display:block;
width:0;
position:absolute;
top:0;
bottom:0;
left:0;
border-left:1px solid
}
.tree li {
margin:0;
padding:0 1em;
line-height:2em;
color:#369;
font-weight:700;
position:relative
}
.tree ul li:before {
content:"";
display:block;
width:10px;
height:0;
border-top:1px solid;
margin-top:-1px;
position:absolute;
top:1em;
left:0
}
.tree ul li:last-child:before {
background:#fff;
height:auto;
top:1em;
bottom:0
}
.indicator {
margin-right:5px;
}
.tree li a {
text-decoration: none;
color:#369;
}
.tree li button, .tree li button:active, .tree li button:focus {
text-decoration: none;
color:#369;
border:none;
background:transparent;
margin:0px 0px 0px 0px;
padding:0px 0px 0px 0px;
outline: 0;
}
Conclusions
We have created beautiful bootstrap treeview menu.I have bootstrap 4, jQuery 3 and font-awesome libraries.You use this menu for sidebar menu, header menu or any pages where do you want to display data in hierarchical manner.