'angular js ui-select option repeat not working for object array
I am using Angular ui-select. My model & ui-select option array are different. While changing value its not updated and not displaying options. I am storing the selected object Id in "pmpo", I want to show the selected object from the pmptnk object array when loading. But is not working. Some one tell what I am doing wrong.
My object from Model
pmpo:877
pmptnk:[0:
632:{id: "632", pid: "2993", ESID: "9154", lb1: "Undddd", lb2: "219"}
877:{id: "877", pid: "2993", ESID: "9154", lb1: "Pcddd", lb2: "29"}
654:{id: "654", pid: "2993", ESID: "9154", lb1: "kukuu", lb2: "246"}]
In view file
<div ng-if="item.pmptnk.length > 0">
<ui-select ng-model="item.pmpo" click-out-side="closeThis($event)">
<ui-select-match placeholder="Select " search-placeholder="Filter Tanks"
uib-tooltip="{{item.pmpo > 0 ? $select.selected.lb1 : 'Select Tank'}}" tab-select="true">
<span ng-bind="$select.selected.lb1"></span>
</ui-select-match>
<ui-select-choices repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
<span ng-bind="obj.lb1"></span>
</ui-select-choices>
<ui-select-no-choice>
No results matched "{{$select.search}}"
</ui-select-no-choice>
</ui-select>
</div>
Solution 1:[1]
I worked on your code.I tried it out in different way. Below is my code snippet:
<div ng-if="item.pmptnk.length > 0">
<ui-select ng-model="item.selected" click-out-side="closeThis($event)">
<ui-select-match
placeholder="Select "
search-placeholder="Filter Tanks"
uib-tooltip="{{item.pmpo > 0 ? $select.selected.lb1 : 'Select Tank'}}"
tab-select="true"
>
<span ng-bind="$select.selected.lb1"></span>
</ui-select-match>
<ui-select-choices repeat="obj.tid as obj in (item.pmptnk)">
<span ng-bind="obj.lb1"></span>
</ui-select-choices>
<ui-select-no-choice>
No results matched "{{$select.search}}"
</ui-select-no-choice>
</ui-select>
</div>
and i changed my model as below:
$scope.item = {};
$scope.item.pmpo = 877;
$scope.item.pmptnk = [
{ id: "632", pid: "2993", ESID: "9154", lb1: "Undddd", lb2: "219" },
{ id: "877", pid: "2993", ESID: "9154", lb1: "Pcddd", lb2: "29" },
{ id: "654", pid: "2993", ESID: "9154", lb1: "kukuu", lb2: "246" },
];
for (var i = 0; i < $scope.item.pmptnk.length; i++) {
if ($scope.item.pmptnk[i].id == $scope.item.pmpo) {
$scope.item.selected = $scope.item.pmptnk[i].tid;
break;
}
}
This worked fine for me.
Solution 2:[2]
As per docs of ui-select-choices
, the repeat
attribute
Specify the list of items to provide as choices. Syntax is similar to ngRepeat.
And according to the ng-repeat
doc
It is possible to get ngRepeat to iterate over the properties of an object using the following syntax:
<div ng-repeat="(key, value) in myObj"> ... </div>
So, from this, we can conclude that you should change your syntax from this:
<ui-select-choices repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
to this:
<ui-select-choices repeat="(key, value) in (item.pmptnk[item.pmpo])">
where value
will be 877
, 2993
and so on and key
will be id
, pid
and so on.
Solution 3:[3]
Should not it be ng-repeat instead of just repeat?
<ui-select-choices repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
change this line to the following one
<ui-select-choices ng-repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
It will fix the issue hopefully.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | boop_the_snoot |
Solution 2 | lealceldeiro |
Solution 3 | Abdul Aleem Khan |