0

i have two scopes (arrays). One with room data and one with room list. So everytime a user want to add some data for each room have to select it from a dropdown (select option) and add that data he wants. Now, i need to exclude every room tat already have some data from the dropdown and i am trying like thi.

Here is the Room list:

$scope.propertyRooms = [
    {
        id: "1",
        name: "Room 1 Sea View"
    },
    {
        id: "2",
        name: "Room 2 Mountain View"
    },
    {
        id: "3",
        name: "Room 3"
    }

];

Here is the room with data (to exclude):

$scope.ratePlansRoomsSettings = [
            {
                id: "1",
                name: "Room 1 Sea View",
                allotment: "10",
                minPax: "1",
                maxPax: "5",
                maxAdult: "5",
                maxChild: "4",
                maxInf: "1",
                childAllowed: true
            },
            {
                id: "2",
                name: "Room 2 Mountain View",
                allotment: "10",
                minPax: "1",
                maxPax: "10",
                maxAdult: "10",
                maxChild: "8",
                maxInf: "1",
                childAllowed: false
            }
        ];

Here is how i am trying to exclude the selected:

$scope.getAvailableRooms = function(){
    var selectedRooms = [];

    for(i = 0; i < $scope.propertyRooms.length; i++){
        var found = false;
        for(j = 0; j < $scope.ratePlansRoomsSettings.length; j++){
            if($scope.ratePlansRoomsSettings[j].id == $scope.propertyRooms[i].id){
                found = true;
                break;
            }
        }
        if(!found){
            selectedRooms.push($scope.propertyRooms[i]);
        }
        return $scope.propertyRooms;

    }

}

here is the select options:

<select ng-model="room.name" ng-options="p.name as p.name for p in getAvailableRooms()" ng-hide="room.name" class="form-control"></select>

The problem is that the select options give me all the time all the rooms from the propertyRooms scope instead those only that are not selected.

I am not receiving any errors but i beleive i am wrong in the for function.

7
  • shouldnt you return selectedRooms instead of the unfiltered $scope.propertyRooms ? Commented Nov 4, 2014 at 9:09
  • @Jeyp I have tried that but i am getting an empty option. even in the console.log it's empty. Commented Nov 4, 2014 at 9:11
  • 1
    Also, shouldnt the return be after the loop, it is currently IN the loop, isn't it? Commented Nov 4, 2014 at 9:12
  • omg yes, that's it i feel so noob :P Commented Nov 4, 2014 at 9:14
  • Hehe, I think every developer once had a problem like this :P However, have you though of writing a filter for this purpose? Commented Nov 4, 2014 at 9:17

1 Answer 1

1

I think this should work:

$scope.getAvailableRooms = function(){
    var selectedRooms = [];

    for(i = 0; i < $scope.propertyRooms.length; i++){
        var found = false;
        for(j = 0; j < $scope.ratePlansRoomsSettings.length; j++){
            if($scope.ratePlansRoomsSettings[j].id == $scope.propertyRooms[i].id){
                found = true;
                break;
            }
        }
        if(!found){
            selectedRooms.push($scope.propertyRooms[i]);
        }
    }
    return selectedRooms;
}

However, have you considered using a filter for this purpose? You could write your custom filter as described here. Just an example here:

.filter('rooms', function() {
    return function(input) {
      var selectedRooms = [];

      for(i = 0; i < input.length; i++){
          var found = false;
          for(j = 0; j < $scope.ratePlansRoomsSettings.length; j++){
              if($scope.ratePlansRoomsSettings[j].id == input[i].id){
                  found = true;
                  break;
              }
          }
          if(!found){
              selectedRooms.push(input[i]);
          }
      }
      return selectedRooms;
    };
  })
Sign up to request clarification or add additional context in comments.

1 Comment

That was it thank you! Regarding the filters it is not what i want to do but anyway the answer was what i did wrong ;) :P

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.