Using angularJS I'm trying to build a list that will be used as a menu. I have an array of JSON and the menu must be built depending on conditions on JSON fields :
In my controller, for now, I have this :
Getting the JSON :
$scope.cartoList = [];
$http.get('frList.json')
.success(function(data) {
$scope.cartoList = data;
$scope.buildMenu();
})
.error(function(data) {
console.log("Error while getting json.");
})
Initialize the menu list :
$scope.initMenu = function () {
for (var i = 0; i < $scope.cartoList.length; i++) {
if ($scope.cartoList[i].informationSystem != "" && $scope.ISList.indexOf($scope.cartoList[i].informationSystem) === -1)
$scope.ISList.push($scope.cartoList[i].informationSystem);
if ($scope.cartoList[i].macroProcess != "" && $scope.macroProcessList.indexOf($scope.cartoList[i].macroProcess) === -1)
$scope.macroProcessList.push($scope.cartoList[i].macroProcess);
}
}
JSON Sample :
[
{ "area" : "Middle",
"block" : "Position",
"created" : "2015-6-15",
"defaultZoom" : "1",
"displayName" : "Architecture",
"fileName" : "AA_APK",
"informationSystem" : "A Group",
"lastupdate" : "2015-6-15",
"level" : "Block",
"macroProcess" : "",
"type" : "AA"
},
{ "area" : "",
"block" : "",
"created" : "2015-6-15",
"defaultZoom" : "1",
"displayName" : "Processus order VM",
"fileName" : "AM_Process_order_VM",
"informationSystem" : "A Group",
"lastupdate" : "2015-6-15",
"level" : "",
"macroProcess" : "Deal period",
"type" : "AM"
},
...
]
HTML :
<ul class="sidebar-menu slimscroll">
<li class="treeview" ng-repeat="IS in ISList"><a href="javascript:void(0)"> <i class="fa fa-folder"></i>{{IS}} <i class="fa fa-angle-left pull-right"></i></a>
<ul class="treeview-menu">
<li class="treeview"><a href="javascript:void(0)"> <i class="fa fa-folder"></i>Processus<i class="fa fa-angle-left pull-right"></i></a>
<ul class="treeview-menu">
<li class="treeview" ng-repeat="mProcess in macroProcessList"><a href="javascript:void(0)"><i class="fa fa-folder"></i>{{mProcess}}<i class="fa fa-angle-left pull-right"></i></a>
<ul class="treeview-menu">
<li ng-repeat="carto in cartoList" ng-if="carto.type == 'AM' && carto.macroProcess == mProcess && carto.informationSystem == IS"><a href="javascript:void(0)" ng-click="changeSVG(carto.fileName)"><i class="fa fa-sitemap"></i>{{carto.displayName}}</a></li>
</ul>
</li>
</ul>
</li>
<!-- Other <li> will come here -->
</ul>
</li>
</ul>
The problem is the menu doesn't drop down when I click on my IS elements.
The treeview and treeview-menu comes from the template I'm using that you can find here : https://almsaeedstudio.com/themes/AdminLTE/index.html
But I don't think this is related.
EDIT : The menu dropdown is handled by JQuery, so it might be related. Maybe the event is not catched because of some conflict between JQuery and angular ?
This should be the relevant part :
$.AdminLTE.tree = function (menu) {
var _this = this;
$("li a", $(menu)).on('click', function (e) {
//Get the clicked link and the next element
var $this = $(this);
var checkElement = $this.next();
//Check if the next element is a menu and is visible
if ((checkElement.is('.treeview-menu')) && (checkElement.is(':visible'))) {
//Close the menu
checkElement.slideUp('normal', function () {
checkElement.removeClass('menu-open');
//Fix the layout in case the sidebar stretches over the height of the window
//_this.layout.fix();
});
checkElement.parent("li").removeClass("active");
}
//If the menu is not visible
else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) {
//Get the parent menu
var parent = $this.parents('ul').first();
//Close all open menus within the parent
var ul = parent.find('ul:visible').slideUp('normal');
//Remove the menu-open class from the parent
ul.removeClass('menu-open');
//Get the parent li
var parent_li = $this.parent("li");
//Open the target menu and add the menu-open class
checkElement.slideDown('normal', function () {
//Add the class active to the parent li
checkElement.addClass('menu-open');
parent.find('li.active').removeClass('active');
parent_li.addClass('active');
//Fix the layout in case the sidebar stretches over the height of the window
_this.layout.fix();
});
}
//if this isn't a link, prevent the page from being redirected
if (checkElement.is('.treeview-menu')) {
e.preventDefault();
}
});
};
innerHTMLand using HTML for the anchor and icon elements, but constructing theulandliusing DOM methods. My suggestion would be to create an array of the HTML strings and set the final innerHTML once.ng-repeatwithng-if