Binding an OBJECT ARRAY to <ul> in AngularJs - angularjs

I am populating a dropdownbutton from a array. The whole key value pair is being shown as you see in the picture.
1) I need to show only the value of Label.
2) I want to have "ALL LOCATIONS" as a default item.
Here is how the Locations look like :
Locations:Array[2]
0:Object
$$hashKey:"object:772"
Id:1600
Label:"California"
__proto__:Object
1:Object
$$hashKey:"object:773"
Id:1600
Label:"Atlanta"
__proto__:Object
HTML :
<div class="btn-group btn-toolbar btn-margin-left" style="float: right;">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
ALL LOCATIONS {{selectedItem}}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li ng-repeat="Label in Summary.Locations">
<a ng-click="dropboxitemselected(Label)">
{{Label}}</a>
</li>
</ul>
</div>
js :
WotcDashBoardModuleService.GetDashBoardSummary(Ein).then(function (response) {
$scope.Summary = response.data.WotcSummary;
$scope.selectedItem;
$scope.dropboxitemselected = function (item) {
$scope.selectedItem = item;
//alert($scope.selectedItem);
}
console.log($scope.Summary);
});

Summary.Locations is your array of objects, so Label gets bound to each element individually. That means you need to show {{Label.Label}} instead of just {{Label}} and you'll see the name.

This part:
ALL LOCATIONS {{selectedItem}}
Should be replaced with:
<span ng-if="!selectedItem">ALL LOCATIONS</span>
<span ng-if="selectedItem" ng-bind="selectedItem.Label"></span>
Or with:
<span>{{ selectedItem ? selectedItem.Label : 'ALL LOCATIONS' }}</span>

Related

Uib accordion toggle issue

I am using uibAccordion where the toggling is not happening properly,
If I click 1st repeated element all others also opening. Below is the code.
<ul>
<li ng-repeat="(key, value) in scenariosViewAll.collectionBookObject">
<div class="desc">
<uib-accordion close-others="true" class="">
<div uib-accordion-group id="{{$index+1}}" class="panel-default" is-open="status.open" is-disabled="true" prevent-click="false" ng-init="status.open = (key == scenariosViewAll.shared.currentSelectedAccordion) ? true : false" ng-class="{'opened': status.open}" ng-if="value.length > 0">
<uib-accordion-heading>
<span class="accordion-toggle-wrapper">
<span class="accordion-title">
{{key}}
</span>
<span class="pull-right acc-icon-set">
<span class="collection-indicator">{{value.length}}</span>
<span class="arrow-toggle">
<button class="btn btn-icon" aria-hidden="true" ng-click="status.open = !status.open;">
<i class="icon" ng-class="{'icon-chevron-up': status.open, 'icon-chevron-down': !status.open}"></i>
</button>
</span>
</span>
</span>
</uib-accordion-heading>
</div>
</uib-accordion>
</div>
</li>
</ul>
I have tried passing id as {{index+1}} and also close-others="true".
But it's not working
Here
ng-init="status.open = (key == scenariosViewAll.shared.currentSelectedAccordion) ? true : false"
I really don't know what you are binding status.open to, but clearly this is wrong and really ambiguous
Simplify it, and give is-open a property that refers to a different value for each iterated key/value, for example
ng-init="value.ui.isOpen = false;"
is-open="value.ui.isOpen"
If you are raelly lazy, and don't need to access these variables inside your controller, just don't bind it to anything
ng-init="isOpen = false;" // by now this is redundant and can be removed
is-open="isOpen"
this will work because ngRepeat created a new scope for every iteration it goes, so a new isOpen will be created per such scope.
I got solution by passing index:
<div uib-accordion-group class="panel-default" ng-init="status.open = false;" is-open="status.open[$index]" prevent-click="false" ng-class="{'opened': status.open}" ng-if="value.length > 0">

selecting element in uib-dropdown menu

I Have a HTML code like this.
<div ng-if="!hide" class="dropdown pull-right" uib-dropdown>
<a uib-dropdown-toggle href="" >
<div class="btn btn-primary-outline btn-circle btn-xs pull-right comment-button">
<span class="icon icon-chevron-down"></span>
</div>
</a>
<ul class="dropdown-menu " style="text-align: center;" role="menu" uib-dropdown-menu>
<li role="divider" ng-if="showDelete">delete </li>
<li role="divider"> report</li>
</ul>
</div>
When using in protractor facing an issue with uib-dropdown selection.
I written code like this:
var dropDown = element(by.css("div[uib-dropdown]"));
dropDown.element(by.css("a[uib-dropdown-toggle]"));
dropDown.element(by.css("a[ng-click=deleteItem($index)]")).click();
browser.sleep(5000);
this.selectMenuOption = function (option) { //menu item to click
var dropdown = element(by.className('dropdown pull-right'));
dropdown.click();
dropdown.element(by.tagName('ul')).all(by.tagName('li')).filter(function (elem) {
return elem.getText().then(function (val) {
return val.toUpperCase() === option.toUpperCase();
})
}).first().click();
}
The a[ng-click=deleteItem($index)] is actually an invalid CSS selector, you needed to put the attribute value into quotes:
dropDown.element(by.css('a[ng-click="deleteItem($index)"]')).click();
Though, I'd go for a partial match that appears to be more readable:
dropDown.element(by.css('a[ng-click*=deleteItem]')).click();
where *= means "contains".
Or, you can even go for a "link text" locator:
dropDown.element(by.linkText('delete')).click();
dropDown.element(by.partialLinkText('delete')).click();

Angular JS issue with nested ng-repeat

I have an array on object like this:
$scope.techniques = [
{
programmes : 'genie' ,
documents :[
{
menuTag: 'Moments et Centroïdes',
titre: "Moments et Centroïdes",
contenu: "moment.html"
}]
},
{
programmes : 'test' ,
documents :[
{
menuTag: 'test de test',
titre: "test",
contenu: "test.html"
}]
}
];
I am trying to build my menu with nested ng-repeat and exemple found on stackoverflow are not working. Cant figure ou why.
Here is my html;
<button ng-repeat="tech in techniques" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{{tech.programmes}}
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li ng-repeat="doc in tech.documents"><a href="../{{doc.contenu}}" ng-cloak>{{doc.menuTag}}</a></li>
</ul>
I can see the programmes but I cant see the documents inside programmes.
What am I doing wrong?
It's because you're trying to access "tech" outside of the first ng-repeat-scope. So it's not beeing nested.
<div ng-repeat="tech in techniques">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
{{tech.programmes}}
<span class="caret"></span>
</button>
<ul ng-repeat="doc in tech.documents" class="dropdown-menu">
<li><a href="../{{doc.contenu}}" ng-cloak>{{doc.menuTag}}</a></li>
</ul>
</div>
Your two ng-repeats are not actually nested. In order to nest two ng-repeats, the second ng-repeat should be within the range of the first ng-repeat. You cannot access the 'tech' outside the button element. To make it work, wrap both button and ul inside a div, and define the first ng-repeat on the div instead of button.
Because you are not under the iteration of $scope.techniques.
<button ng-repeat="tech in techniques" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{{tech.programmes}}
<span class="caret"></span>
</button>
<!-- tech does not existe anymore-->
<ul ng-repeat="doc in tech.documents" class="dropdown-menu">
<a href="../{{doc.contenu}}" ng-cloak>{{doc.menuTag}}</a></li>
</ul>

How to use ng-click in Angular ui-tree for drag and drop as well as performing some function

My Angular code
<div ui-tree="rootTree" data-drag-enabled="false">
<ol ui-tree-nodes="" ng-model="course.sections" ng-class="{hidden: collapsed}">
<li ng-repeat="section in course.sections" ui-tree-node> <div ui-tree-handle class="tree-node tree-node-content" ng-click="editSection(section)" >
<a class="btn btn-success btn-xs"ng-if="section.sectionInstances && section.sectionInstances.length > 0" data-nodrag ng-click="toggle(this)">
<span class="glyphicon glyphicon-chevron-down" ng-class="{'glyphicon-chevron-right': collapsed,'glyphicon-chevron-down': !collapsed }">
</span>
</a>
{{section.sec_name}}
<a class="pull-right btn btn-danger btn-xs" data-nodrag ng-click="removeSection(section)">
<span class="glyphicon glyphicon-remove">
</span>
</a>
<a class="pull-right btn btn-primary btn-xs" data-nodrag ng-click="addSectionInstance(section)" style="margin-right: 8px;">
<span class="glyphicon glyphicon-plus">
</span>
</a>
</div>
</li>
</ol>
</div>
If i use data-drag-enabled="false", then ng-click function is calling. I want both the function drag and drop as well as ng-clik function.
If you are just wanting to perform functionality on 'dragStart', then it appears according to the angular-ui-tree documentation, that you can define treeOptions in your controller and assign them to your tree directive attribute value.
myAppModule.controller('MyController', function($scope) {
$scope.treeOptions = {
dragStart: function(sourceNodeScope, destNodesScope, destIndex) {
// Do things here
},
dragEnd: function(sourceNodeScope, destNodesScope, destIndex) {
}
};
});
<div ui-tree="treeOptions">
<ol ui-tree-nodes ng-model="nodes">
<li ng-repeat="node in nodes" ui-tree-node>{{node.title}}</li>
</ol>
</div>
same trouble here, few years after.
Sean answer doesn't seem to answer the question.
issue:
* with drag n drop enable: I can't use ng-click to remove/edit a node, but I can drag n drop it.
* w/o drag n drop enable: my ng-click works.
Solution:
move the "ui-tree-handle" from the global div to specific location w/o any ng-click in.

how to select a li which is nested within another ng-repeat in angular js

(please do not update the english grammer in this question/ I wont be able to approve it and this question wont get resolved.)
This is my UI
It contains various li elements whose values are populated using this angular html
<div class="row">
<li class="no-bullet-li li-12 monaco-font"> {{selectedChangeEligibilityPlan}}</li>
<ul class="ul-plan-1">
<li class="no-bullet-li" ng-repeat="plan in fewThings">
<div ng-class="{ 'selected-class-name': $index == selectedIndex }" ng-click="itemClicked($index)" class="li-alt monaco-font"> p2|{{plan.planName}} | {{plan.planId}}
<a class="iconing-sub" ng-click="addClick(item)" href=""><i class="glyphicon glyphicon-plus"></i></a>
<a ng-click="deleteClick(item)" ng-class="{ active : active.one }" href=""><i class="glyphicon glyphicon-remove iconing_1-sub"></i></a>
</div>
<ul class="ul-plan">
<li class="no-bullet-li li-8 monaco" ng-repeat="item in plan.planIds">
p1| {{ plan.planNames[$index]}} | {{item}}
<a <a ng-click="editClick(item)" href=""><i class="glyphicon glyphicon-pencil iconing"></i></a>
<a ng-click="deleteClick(item)" href=""><i class="glyphicon glyphicon-remove iconing_1"></i></a>
</li>
</ul>
</li>
</ul>
</div>
It uses the nested ng-repeat.
The whole UI is contained within a one controller ( no directives used)
the following code gets triggered when someone clicks the blue lis.
$scope.itemClicked = function ($index) {
console.log($index);
// console.log($(item).closest('li') );
$scope.selectedIndex = $index;
};
here's how to ui looks and its great.
problem arises when I try to do the same logic for the pink ones (nested ng-repeat li). It selects other pink lis in all the other stack too.
here's the screenshot.
second part of question:
I have I have the above UI plus I also have this second UI that is loaded along with this on the same page. It contains a bunch of horizontal rows.
Once the user click the blue pink colored lis it goes into the active state. Then the user can click the row which he likes. upon clicking it the plan name of currently selected li will get replaced.
Here is the html for it.
<div class="row">
<li class="no-bullet-li li-12 monaco-font"> {{selectedChangeEligibilityPlan}}</li>
<ul class="ul-plan-1">
<li class="no-bullet-li" ng-repeat="plan in fewThings">
<div class="li-alt monaco-font"> p2|{{plan.planName}} | {{plan.planId}}
<a class="iconing-sub" ng-click="addClick(item)" href=""><i class="glyphicon glyphicon-plus"></i></a>
<a ng-click="deleteClick(item)" ng-class="{ active : active.one }" href=""><i class="glyphicon glyphicon-remove iconing_1-sub"></i></a>
</div>
<ul class="ul-plan">
<li ng-class="{ 'selected-class-name': $index == selectedIndex }" ng-click="itemClicked($index)" class="no-bullet-li li-8 monaco" ng-repeat="item in plan.planIds">
p1| {{ plan.planNames[$index]}} | {{item}}
<a ng-click="editClick(item)" href=""><i class="glyphicon glyphicon-pencil iconing"></i></a>
<a ng-click="deleteClick(item)" href=""><i class="glyphicon glyphicon-remove iconing_1"></i></a>
</li>
</ul>
</li>
</ul>
</div>
The problem lies in the fact that you are trying to save the state of the data (which one is selected) inside your controller using $index. The $index property isn't unique among different ng-repeats, so when you set your $scope.selectedIndex to $index, each of your sub lists will see that their $index matches, and so will each trigger the ng-class and add the selected-class-name class.
What you could do instead is have each item in the data have a unique index and use that id to store which item is selected in $scope.selectedIndex.
<ul class="ul-plan">
<li ng-class="{ 'selected-class-name': item.id == selectedIndex }" ng-click="itemClicked(item.id)" class="no-bullet-li li-8 monaco" ng-repeat="item in plan.planIds">
p1| {{ plan.planNames[$index]}} | {{item}}
<a ng-click="editClick(item)" href=""><i class="glyphicon glyphicon-pencil iconing"></i></a>
<a ng-click="deleteClick(item)" href=""><i class="glyphicon glyphicon-remove iconing_1"></i></a>
</li>
</ul>
This line looks strange.
<a ng-click="select('one')" href="">
Did you really mean to pass a hardcoded 'one' to the function? Or was it supposed to be the index, like the deleteClick() call.

Resources