# Add hierarchical tree view in the Categories widget

The *Categories* widget by default has just one view that shows all available categories as a flat list.\
This tutorial demonstrates how to create a view that displays a tree, which preserves the hierarchy of the categories. You first configure the *Categories* widget and then add and implement the new categories view.

[## Enable Hierarchy mode in the Categories widget](https://www.progress.com/documentation/sitefinity-cms/add-hierarchical-tree-view-in-the-categories-widget-mvc#enable-hierarchy-mode-in-the-categories-widget-nbsp-#enable-hierarchy-mode-in-the-categories-widget-nbsp-)

1. Open the widget designer.
2. Under \*Which categories to display?,\*select one of the following options:
   * *Top level categories only*
   * *All categories under particular category...*
   * *Only categories used by content type...*
3. Click the *Advanced* button.
4. In the *FlattenHierarchy* field enter `False`.

   > info  **NOTE**: After setting this option to `False`, the widget will start performing additional calls to the database for each taxon, so that it retrieves its children. For a very large taxonomy, this may decrease the performance of the widget.
5. Click the *Save* button.

[## Add new view to the widget](https://www.progress.com/documentation/sitefinity-cms/add-hierarchical-tree-view-in-the-categories-widget-mvc#add-new-view-to-the-widget#add-new-view-to-the-widget)

1. In the `~/Mvc/Views/HierarchicalTaxonomy/` folder, create a new file named `HierarchicalTaxonomy.[YourViewName].cshtml`.
2. Click *Edit* in the upper right corner of the widget and open the *Settings* tab page.
3. From the *Template* dropdown menu, select the newly added view.

[##  Implement the view](https://www.progress.com/documentation/sitefinity-cms/add-hierarchical-tree-view-in-the-categories-widget-mvc#implement-the-view#implement-the-view)

The view model contains a *Taxa* collection of objects of type `Telerik.Sitefinity.Frontend.Taxonomies.Mvc.Models.TaxonViewModel`. Such objects contain a *SubTaxa* collection that contains the child taxa.

In your new view, you recursively render each level of children. In the code below, you add buttons marked with the attribute `data-sf-toggle` to expand and collapse the inner categories. Each sublist of categories is marked with the attribute `data-sf-submenu`. These attributes help implement the expanding and collapsing functionality with JavaScript.

You insert the JavaScript code directly into the view's file with the help of a `<script>` tag. In the JavaScript below, you attach a `click` event handler to each expand and collapse button. In addition, you add and remove CSS classes to the buttons and the sublists, so that you can show and hide them.

The CSS code formats the tree and applies styles to the expand buttons and the sublists of categories. You can insert this code in the view's file, using a `<style>` tag.

Use the following example for the code of the view:

```Razor
@model Telerik.Sitefinity.Frontend.Taxonomies.Mvc.Models.TaxonomyViewModel

@using Telerik.Sitefinity.Frontend.Taxonomies.Mvc.Models;
<div class="@Model.CssClass dropdown">
    <ul>
        @foreach (var taxon in Model.Taxa)
        {
            @RenderTaxaRecursive(taxon);
        }
    </ul>
</div>

@helper RenderTaxaRecursive(TaxonViewModel taxon)
{
    @:<li>
        if (taxon.SubTaxa.Count > 0)
        {
             <a data-sf-toggle class="sf-closed"></a>
        }
        <a href="@taxon.Url"><span>@taxon.Title</span></a>

        if (Model.ShowItemCount)
        {
            <span>@Html.Raw(string.Format("({0})", taxon.Count))</span>
        }
        <br />

        if (taxon.SubTaxa.Count > 0)
        {
            @:<ul data-sf-submenu class="sf-submenu sf-closed">
                foreach (var childTaxon in taxon.SubTaxa)
                {
                    @RenderTaxaRecursive(childTaxon);
                }
            @:</ul>
        }
    @:</li>
}

<script> 
  
  // Attach click events to all toggle buttons. 
  var nodes = document.querySelectorAll('a[data-sf-toggle]');

    for (var i = 0; i < nodes.length; i++) {
        nodes[i].addEventListener("click", function () {
            var submenu = findSubMenu(this);

            if (!submenu) return false;

            // Change the toggle classes of the buttons and the submenu.
            toggleElement(submenu);
            toggleElement(this);
        });
    }

    // Searches for submenu on the same level as the given element. 
    function findSubMenu(node) {
        var sibling = node.nextSibling;
        while (sibling.nodeType !== 1 || !sibling.hasAttribute("data-sf-submenu")) {
            sibling = sibling.nextSibling;
        }
        return sibling || null;
    }

    // Hides the element if it is visible and vice versa. 
    function toggleElement(node) {
        if (hasClass(node, "sf-opened")) {
            replaceClass(node, "sf-opened", "sf-closed");
        }
        else if (hasClass(node, "sf-closed")) {
            replaceClass(node, "sf-closed", "sf-opened");
        }
    }

    // Checks if the element has the given class. 
    function hasClass(node, className) {
        return node.className.match(new RegExp("(?:^|\\s)" + className + "(?!\\S)"));
    }

    // Replaces the given old class with the new one in the element. 
    function replaceClass(node, oldClass, newClass) {
        node.className =
           node.className.replace(new RegExp("(?:^|\\s)"+ oldClass +"(?!\\S)"), (" " + newClass));
    }
</script>

<style>
    ul.sf-submenu {
        margin-left: 20px;
    }

    ul.sf-opened {
        display: block;
    }

    ul.sf-closed {
        display: none;
    }

    a.sf-opened:before {
        content: "-";
        color: black;
    }

    a.sf-closed:before {
        content: "+";
        color: black;
    }
</style>
```
