Populating Nested Array With Checkbox Inputs In Angular App
In my Angular app, I have the following list of checkboxes, generated within a nested ng-repeat:
it.
If you have questions, don't hesitate to ask.
<divng-repeat="organization in boundaryPartners"><divng-repeat="partner in organization.partners"><label><inputtype="checkbox"value="partner.id"ng-model="partner.selected"ng-change="changeValue(partner, organization, $parent.$index)"/>
{{partner.name}}
</label></div><hr/></div>
$scope.participatingPartners = []
var idsArray = [];
$scope.changeValue = function(partner, organization, index){
if(partner.selected)
addPartner(partner, organization, index);
else
removePartner(partner, organization, index);
};
var addPartner= function(partner, organization, index){
prepareArrayToAdd(organization, index);
if(!existInList(index, partner)){
$scope.participatingPartners[index].partners.push(partner);
}
};
var prepareArrayToAdd = function(organization, index){
if(!$scope.participatingPartners[index])
$scope.participatingPartners[index] = { id: organization.id, name: organization.name, partners: [] };
};
var removePartner= function(partner, organization, index){
idsArray = getIdsArray(index);
var indexToRemove = idsArray.indexOf(partner.id);
if(indexToRemove == -1)
return;
$scope.participatingPartners[index].partners.splice(indexToRemove, 1);
};
var existInList = function(index, partner){
idsArray = getIdsArray(index);
return idsArray.indexOf(partner.id) != -1;
};
var getIdsArray = function(index){
return$scope.participatingPartners[index].partners.map(function(partner){ return partner.id });
};
JSFIddle link.
Hope it helps!
Post a Comment for "Populating Nested Array With Checkbox Inputs In Angular App"