Angularjs Filter Out Select Option Based On Another Selection
Hello Angular experts, I have been banging my head for half of the day to make a list of selections where its options can be hide or disable based on other selections. This is the
Solution 1:
First of all, I extremely recommend you to use ngOptions instead of ngRepeat
. ngOptions
was made exactly for this kind of things.
Well, to achieve what you want I think the simplest way is to create a new property (which, in my solution, I called it as isAvailable
- boolean -), then you can easily manipulate your items based on this property.
Take a look on my solution:
(function() {
"use strict";
angular.module('app', [])
.controller('mainCtrl', function($scope) {
$scope.roomAllocation = {
"dates":[
{
"date":"2016-07-16",
"dayRooms":[
{
"room":1,
"occupancy":2,
"roomType":"Standard",
"availableRooms":[
{
"id":15,
"roomNumber":200
},
{
"id":16,
"roomNumber":201
},
{
"id":17,
"roomNumber":202
},
{
"id":18,
"roomNumber":203
}
]
},
{
"room":2,
"occupancy":3,
"roomType":"Standard",
"availableRooms":[
{
"id":15,
"roomNumber":200
},
{
"id":16,
"roomNumber":201
},
{
"id":17,
"roomNumber":202
},
{
"id":18,
"roomNumber":203
}
]
}
]
},
{
"date":"2016-07-17",
"dayRooms":[
{
"room":1,
"occupancy":2,
"roomType":"Standard",
"availableRooms":[
{
"id":15,
"roomNumber":200
},
{
"id":16,
"roomNumber":201
},
{
"id":17,
"roomNumber":202
},
{
"id":18,
"roomNumber":203
}
]
},
{
"room":2,
"occupancy":1,
"roomType":"Standard",
"availableRooms":[
{
"id":15,
"roomNumber":200
},
{
"id":16,
"roomNumber":201
},
{
"id":17,
"roomNumber":202
},
{
"id":18,
"roomNumber":203
}
]
}
]
}
]
};
// Function to set all rooms as available on initializationfunctionset_availables() {
$scope.roomAllocation.dates.forEach(function(date) {
date.dayRooms.forEach(function(dayRoom) {
dayRoom.availableRooms = dayRoom.availableRooms.map(function(avalRoom) {
avalRoom.isAvailable = true;
return avalRoom;
});
});
});
}
set_availables();
$scope.newRoomObject = {};
// Fires on change of the select
$scope.disable_room = function(dateIndex, roomIndex) {
var currDate = $scope.roomAllocation.dates[dateIndex];
// The current number room selectedvar selectedRoomNumber = $scope.newRoomObject[currDate.date][roomIndex + 1].roomNumber;
// Setting property isAvaliable to true / false
currDate.dayRooms.forEach(function(value, index) {
if (index != roomIndex) {
value.availableRooms = value.availableRooms.map(function(avalRoom) {
avalRoom.isAvailable = avalRoom.roomNumber != selectedRoomNumber;
return avalRoom;
});
}
});
}
});
})();
divspan {
margin-right: 15px;
}
<htmlng-app="app"><head><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script></head><bodyng-controller="mainCtrl"><divng-repeat="date in roomAllocation.dates track by $index"><divng-repeat="rooms in date.dayRooms track by $index"><spanng-bind="date.date"></span><spanng-bind="'Room ' + '#' + rooms.room"></span><spanng-bind="rooms.roomType"></span><spanng-bind="'Occ: ' + rooms.occupancy"></span><span><selectng-options="room as room.roomNumber for room in rooms.availableRooms | filter: { isAvailable: true }"ng-model="newRoomObject[date.date][rooms.room]"ng-change="disable_room($parent.$index, $index)"><optionvalue=""disabled>Select Room</option></select></span></div><hr></div></body></html>
Note: If you have any doubts you can ask me.
I hope it helps!!
Post a Comment for "Angularjs Filter Out Select Option Based On Another Selection"