Creating Tree View In Mvc4 using jquery treeview plugin
00:52
Here i am explaining how to create treeview in mvc4 using jquery tree view plug in..
1. download the jquery treeview plug from this link http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
2. add the js files and css files for treeview like below to the view..
<link href="../../Css/jquery.treeview.css" rel="stylesheet"
type="text/css"
/>
<script src="../../js/jquery.js" type="text/javascript"></script>
<script src="../../js/jquery.treeview.js" type="text/javascript"></script>
3.create the tree view nodes like below
<ul
id="treesevers">
<li>
<div>
Sports
</div>
<ul>
<li>
<div>
Cricket
</div>
</li>
<li>
<div>
Tennis
</div>
</li>
<li>
<div>
Football
</div>
</li>
<li>Hockey
</li>
</ul>
</li>
<li>
<div>
Fruits
</div>
<ul>
<li>
<div>
Mango
</div>
</li>
<li>
<div>
Apple
</div>
</li>
<li>
<div>
Grapes
</div>
</li>
</ul>
</li>
</ul>
5.calling the treeview method using jquery
<script type="text/javascript">
$(document).ready(function () {
$("#treesevers").treeview();
});
</script>
Binding data from db to tree view:
Contoller code:
var departments = dc.Department.Select(e => e.departments).ToList();
List<string> departmentlist = new List<string>();
foreach(var department in departments)
{
departmentlist.Add(department);
}
ViewBag.departments = departmentlist;
View Design:
<ul id="treesevers">
<li>
<div>
Department
</div>
<ul>
@{
var deapartmentlist=ViewBag.departments as List<string>;
foreach(string department in deapartmentlist)
{
<li>
<div>
@department
</div>
</li>
}
}
</ul>
</li>
</ul>
Binding data from db to tree view:
Contoller code:
var departments = dc.Department.Select(e => e.departments).ToList();
List<string> departmentlist = new List<string>();
foreach(var department in departments)
{
departmentlist.Add(department);
}
ViewBag.departments = departmentlist;
View Design:
<ul id="treesevers">
<li>
<div>
Department
</div>
<ul>
@{
var deapartmentlist=ViewBag.departments as List<string>;
foreach(string department in deapartmentlist)
{
<li>
<div>
@department
</div>
</li>
}
}
</ul>
</li>
</ul>
Is it possible to do this dynamically?
ReplyDeleteyes it is possible..
DeleteHi Venu,
ReplyDeleteI want to show Department wise Employee list in a tree which i have to retriebve from SQl Db .
How can i do it ?
Hi Rahul I updated post with db values to treeview pls look into that
DeleteThanks,
hi
ReplyDeletei tried to follow your steps but coulnt get this treeview working as expected(getting a treeview in an expanded form which is not collapsible)
I really need to get this working in mvc4.
Could u plz send a sample of our project at rashichoudhary998@gmail.com
Its very urgent!!!!
sent project pls follw that
DeleteThanks
Hi,
ReplyDeleteIs it possible to have check box tree view? How can I do it?
Hi
ReplyDeleteAs Rashi said even i tried this but coulnt get this treeview working as expected(getting a treeview in an expanded form which is not collapsible)
Are u missing any styles in the post.
Because step 4 is missing.
Thanks
Jags
Jquery is an advanced programming language. Treeview is a best approach to show large amount of data in hierarchical fashion. Compare to Server-side treeview client-side treeview helps to improve performance of a web application. How it is if after fetch data from the server will render those data using client script. There are various of languages using which we can create treeview. Lets talk about JavaScript. JavaScript is a client side language. To create a treeview using JavaScript we required to write more them 100 lines of code. The same treeview can achieved easily with less line of codes using JQuery. Look at this example http://jharaphula.com/jquery-treeview-example-using-html-ul-li-elements
ReplyDelete