tree in html-2
/1: complete /2: edited
by Rohit Bisht
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<div class="tree well">
<ul class="tree">
<li>
<span class="Collapsable"><i class="icon-folder-open"></i> Parent</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span class="Collapsable"><i class="icon-minus-sign"></i> Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span class="Collapsable"><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
</li>
</ul>
</li>
<li>
<span class="Collapsable"><i class="icon-minus-sign"></i> Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span class="Collapsable"><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
</li>
<li>
<span class="Collapsable"><i class="icon-minus-sign"></i> Grand Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span class="Collapsable"><i class="icon-minus-sign"></i> Great Grand Child</span> <a href="">Goes somewhere</a>
<ul>
<li>
<span class="Collapsable"><i class="icon-leaf"></i> Great great Grand Child</span> <a href="">Goes somewhere</a>
</li>
<li>
...
CSS
.tree {
min-height: 20px;
padding: 19px;
margin-bottom: 20px;
background-color: #fbfbfb;
border: 1px solid #999;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05)
}
.tree li {
list-style-type: none;
margin: 0;
padding: 10px 5px 0 5px;
position: relative
}
.tree li::before,
.tree li::after {
content: '';
left: -20px;
position: absolute;
right: auto
}
.tree li::before {
border-left: 1px solid #999;
bottom: 50px;
height: 100%;
top: 0;
}
.tree li::after {
box-sizing: border-box;
border-left: 1px solid #999;
border-bottom: 1px solid #999;
border-bottom-left-radius: 10px;
height: 25px;
top: 0px;
width: 25px;
}
.tree li span {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border: 1px solid #999;
border-radius: 5px;
display: inline-block;
padding: 3px 8px;
text-decoration: none;
}
.tree li.parent_li > span {
cursor: pointer;
}
.tree > ul > li::before,
.tree > ul > li::after {
border: 0;
}
.tree li:last-child::before {
height: 14px;
}
.tree li:not(.parent_li) span {
position: relative;
}
.tree li span:before {
content: "";
position: absolute;
/* border-width - 1px */
left: -4px;
bottom: calc(50% - 4px);
border-color: transparent;
border-left-color: #999;
border-style: inherit;
border-width: 4px;
}
/* another styles for parent_li immediate span children */
.tree li.parent_li > span:before {
bottom: initial;
left: 2px;
top: 20.5px;
}
/* hide arrow for immediate children */
.tree > ul > li > span:before {
display: none;
}
.tree li.parent_li > span:hover,
.tree li.parent_li > span:hover + ul li span {
background: #eee;
border: 1px solid #94a0b4;
color: #000;
}
JavaScript
$(function() {
$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
$('.tree li.parent_li > span').on('click', function(e) {
var children = $(this).parent('li.parent_li').find(' > ul > li');
if (children.is(":visible")) {
children.hide('fast');
$(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
} else {
children.show('fast');
$(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
}
e.stopPropagation();
});
});
$(document).ready(function() {
var toggle = function() {
$(this).parent().children().toggle();
$(this).toggle();
};
$(".Collapsable").click(toggle);
$(".Collapsable").each(toggle);
//$('li:has(> ul)').css("color","red");
});