Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
am binding tree view from database,on text hover tree view is expanding, but i want to expand on click

i have written jquery to expand treeview but it is not working

What I have tried:

.cshtml
HTML
<div ng-controller="menuController" class="treeview">
       <table width="150px">
           <tr>
               <td>
                   <script type="text/ng-template" id="treeMenu">
                       <a href="#">{{menu.Name}}</a>
                       <ul ng-if="(SiteMenu | filter:{ParentID : menu.Id}).length > 0">
                           <li ng-repeat="menu in SiteMenu | filter:{ParentID : menu.Id} : true" ng-include="'treeMenu'"></li>
                       </ul>
                   </script>
                   <ul class="main-navigation">
                       <li ng-repeat="menu in SiteMenu | filter:{ParentID : 0} : true" ng-include="'treeMenu'"></li>
                   </ul>
               </td>
           </tr>
       </table>
   </div>


JavaScript
<script>
    $(document).ready(function () {
        $(".treeview li>ul").css('display', 'none');
        $("a").click(function (e) {

            e.preventDefault();
            $(this).toggleClass("collapse expand");
            $(this).closest('li').children('ul').slideToggle();
        });
    });
</script>


}

CSS
<style>
    li ul {
        display: none;
    }

    li:hover > ul {
        display: block;
    }

    .treeview ul {
        font: 14px Arial, Sans-Serif;
        margin: 0px;
        padding-left: 20px;
    }

    .treeview > li > a {
        font-weight: bold;
    }

    .treeview li a {
        padding: 4px;
        font-size: 12px;
        display: inline-block;
        text-decoration: none;
        width: auto;
    }
.collapse {
        display: inline-block;
        cursor: pointer;
    }

    .expand {
       
        display: inline-block;
        cursor: pointer;
    }
</style>
Posted
Updated 3-Jul-16 0:44am
Comments
Karthik_Mahalingam 27-Jun-16 2:17am    
post the angular controller and template code.
Member 11382784 27-Jun-16 2:26am    
angular.module('MyApp')
.controller('menuController', function($scope,TreeviewService){
$scope.SiteMenu = null;
TreeviewService.GetTreeView().then(function (d) {
$scope.SiteMenu = d.data;
}, function () {
alert('Failed');

});

})
.factory('TreeviewService', function ($http) {
var fac = {};
fac.GetTreeView = function () {
return $http.get('/Data/GetTreeView');
}
return fac;
});

Karthik_Mahalingam 27-Jun-16 2:27am    
need json data for GetTreeView
Member 11382784 27-Jun-16 2:31am    
how to write that code sir
Member 11382784 27-Jun-16 2:27am    
public JsonResult GetTreeView()
{
List<sitemenu> all = new List<sitemenu>();
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
all = dc.SiteMenus.OrderBy(a => a.ParentID).ToList();

}
return new JsonResult { Data = all, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

1 solution

Hi there,


did you have a look at angular.treeview? If this fits your needs, you don't have to do all the work yourself:





If you have further questions, just let me know.

Best, K.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900