Sunday, 19 January 2014

Simple Expanding Tree - JQuery and Divs


In this article I am going to show you how to build a simple tree expander using JQuery and HTML's Div tag. I have recently started experimenting with cshtml and JQuery in ASP.Net MVC4, so the solution I provide here may well not be the best, but it works and if you have any feedback then it will be great to hear from you.

Below is an example of what we want to achieve:














So that when clicking on a Year or a Month, a slow expanding animation will reveal nested items and thus will look super cool!

Below shows the code in my view that is responsible for the layout of the data in the tree expander:


@foreach(var item in Model)
{
    <div class="Year">
        @Html.DisplayTextFor(label => item.Name)
    </div>
    
    foreach(var month in item.ChildDates)
    {
        <div class="Month">
            &nbsp; @Html.DisplayTextFor(label => month.Name)
        </div>
        
        foreach (var blog in month.ChildDates)
        {
            <div class="Blog">
                &nbsp;&nbsp; <a>@Html.DisplayTextFor(label => blog.Name)</a>
            </div>
        }
    }
}

The model that I pass into the view is of type List<TreeViewLocation>.

I loop through each nested collection, displaying the name value of each TreeViewLocation and wrapping each displayed value in a Div that can help me identify it later on when we do some JQuery fun.

I believe the TreeViewLocation class originates from a MVC sample project, but I discovered it here:
http://forums.asp.net/p/1827387/5080740.aspx?MvcTreeView+from+Database+


  public class TreeViewLocation
    {
        public TreeViewLocation()
        {
            ChildLocation = new HashSet<TreeViewLocation>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        public virtual int? ParentLocationId { get; set; }

        public virtual ICollection<TreeViewLocation> ChildLocations { get; set; }
    }

As you can see the class is a recursive structure as it holds a collection of other TreeViewLocation's called ChildLocations. This provides us with a tree like structure which is what we need for our tree expander.

In our case, the root TreeViewLocation object represents a Year. The Year.ChildLocations property contains a nested collection of further TreeViewLocation's which represent each Month in that Year. Then Month.ChildLocations represents each Blog in that particular Month for that Year. Makes sense? Good...

Below shows the Javascript/JQuery functions used to create the tree expanding effect:

<script type="text/javascript">
    $(document).ready(function () {
        $('.Month').hide();
        $('.Blog').hide();
    });

    $(".Year").click(function () {
        $(this).nextUntil(".Year", ".Month").toggle("slow");
    });

    $(".Month").click(function () {
        $(this).nextUntil(".Month, .Year", ".Blog").toggle("slow");
    });

As the page is first loaded, I hide all the Month and Blog Divs - leaving only the Years displayed.

The Year and Month Div click events each use the nextUntil function. This help us identify which Divs we want to toggle. For example, when we click a Year, we want to toggle all Month Divs up until the next Year Div.

The toggle function allows us to show a Div if it is hidden and visa-versa.

And there you have it, a simple solution to implementing an animated tree expander. Happy climbing!

2 comments:

  1. 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
    Replies
    1. thanks for the tip! A number of years has went by, so all this information is somewhat redundant now :D

      Delete