1

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 sample coding of the page

https://jsbin.com/lufugo/1/edit?html,js,output

what I want to do is on a particular day if a room is selected, I want to remove that room select option from the other selection box of the same day.

Can some one help me out please.

2
  • First, for sure you need to do a custom filter and why are using this ngModel so complex? Commented Jul 16, 2016 at 21:43
  • @developer033 can you help me to write the filter? sample code would be helpful. Thanks Commented Jul 16, 2016 at 21:49

1 Answer 1

2

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 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>

Note: If you have any doubts you can ask me.

I hope it helps!!

Sign up to request clarification or add additional context in comments.

4 Comments

WOW. Thanks a lot dear. You saved my day. @developer033
Pleasure to help :)
When I use something similar to your code, it removes the selected one from the first dropdown too. What magic have you used to keep the selection in the first drop down but remove it from the others?
No worries, I worked it out, I needed a set of available qs per row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.