JSFiddle - React, Tailwind, and code Playground
by Sajeetharan Sinnathurai
HTML
<div ng-controller="MyCtrl">
<button ng-click="checkSelectedPhones()">
Check selected phones
</button>
<div ng-repeat="phone in phones | unique:'brandname'">
<label>
<input type="checkbox" ng-true-value="'{{phone.brandname}}'" ng-false-value="''" ng-model="selectedBrands[$index]" ng-change="selectBrand(phone)">
{{phone.brandname}}
</label>
</div>
<br>
<div ng-repeat="brand in selectedBrands track by $index" ng-if="brand">
{{brand}}
<div ng-repeat="phone in phones" ng-if="phone.brandname === brand">
<label>
<input type="checkbox" ng-model="phone.selected" >
{{phone.modelname}}
<input type="number" ng-model="phone.price">
</label>
</div>
</div>
</div>
JavaScript
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function($scope) {
$scope.selectedBrands = [];
$scope.selectBrand = function(selectedPhone) {
// If we deselect the brand
if ($scope.selectedBrands.indexOf(selectedPhone.brandname) === -1) {
// Deselect all phones of that brand
angular.forEach($scope.phones, function(phone) {
if (phone.brandname === selectedPhone.brandname) {
phone.selected = false;
}
});
}
}
$scope.checkSelectedPhones = function() {
var modelNames = [];
var aletrMsg= '';
angular.forEach($scope.phones, function(phone) {
if (phone.selected) {
modelNames.push(phone);
aletrMsg += 'Brand : '+ phone.brandname + 'Phone Name: '+ phone.modelname + ' : Price: '+ phone.price +', ';
}
});
alert(modelNames.length ? aletrMsg : 'No phones selected!');
}
$scope.phones = [{
id: "986745",
brandname: "Nokia",
modelname: "Lumia 735 TS"
}, {
id: "896785",
brandname: "Nokia",
modelname: "Nokia Asha 230"
}, {
id: "546785",
brandname: "Nokia",
modelname: "Lumia 510"
}, {
id: "144745",
brandname: "Samsung",
modelname: "Galaxy Trend 840"
}, {
id: "986980",
brandname: "Samsung",
modelname: "Galaxy A5"
}, {
id: "586980",
brandname: "Samsung",
modelname: "Galaxy Note 4 Duos"
}, {
id: "986980",
brandname: "Samsung",
modelname: "Galaxy A5"
}, {
id: "586980",
brandname: "Samsung",
modelname: "Galaxy Note Duos"
}, {
id: "232980",
brandname: "Htc",
modelname: "Htc One X9"
}, {
id: "456798",
brandname: "Htc",
modelname: "Desire 820"
}, {
id: "656798",
brandname: "Htc",
modelname: "Desire 810S"
}];
});
myApp.filter('unique', function() {
return function(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
...