Hello Angular专家,
我一直在半天敲打我的头来制作一个选项列表,其中的选项可以根据其他选项隐藏或禁用。这是页面的示例编码
https://jsbin.com/lufugo/1/edit?html,js,output
我想要做的是在某一天,如果选择了一个房间,我想从同一天的另一个选择框中删除该房间选择选项。
请有人帮助我。
答案 0 :(得分:2)
首先,我建议您使用ngOptions代替ngRepeat
。 ngOptions
完全是出于这种事情。
嗯,为了达到你想要的目的,我认为最简单的方法是创建一个新属性(在我的解决方案中,我称之为isAvailable
- 布尔 - ),然后您可以根据此属性轻松操纵您的商品。
看看我的解决方案:
(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 initialization
function set_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 selected
var 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;
});
}
});
}
});
})();
div span {
margin-right: 15px;
}
<html ng-app="app">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>
<body ng-controller="mainCtrl">
<div ng-repeat="date in roomAllocation.dates track by $index">
<div ng-repeat="rooms in date.dayRooms track by $index">
<span ng-bind="date.date"></span> <span ng-bind="'Room ' + '#' + rooms.room"></span> <span ng-bind="rooms.roomType"></span> <span ng-bind="'Occ: ' + rooms.occupancy"></span>
<span>
<select ng-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)">
<option value="" disabled>Select Room</option>
</select>
</span>
</div>
<hr>
</div>
</body>
</html>
注意:如果您有任何疑问,可以问我。
我希望它有所帮助!!