2016-10-18 2 views
0

Ich habe eine Auswahloption in meiner Angular-Seite. Die Optionen werden von der API abgerufen.Filterdaten in Winkelauswahl basierend auf Bedingung

Mein HTML ist:

<div> 
    <form id="edit-profile" novalidate name="editReservationForm" autocomplete="off" class="form-horizontal" ng-controller="getReservationController"> 
     <fieldset> 
<div class="control-group"> 
       <label class="control-label" for="reservation.account.name">Account Name<sup>*</sup></label> 
       <div class="controls"> 
        <select ng-model="reservation.account.id" required> 
         <option ng-repeat="p in accounts" value="{{p.id}} ">{{p.name}}</option> 
        </select> 
       </div> <!-- /controls --> 
      </div> <!-- /control-group --> 
</fieldset> 
    </form> 

</div> 

Mein Controller ist:

myApp.controller('getReservationController', ['$scope', 'reservationServices', 'dataTable', '$rootScope', '$location', '$filter', function ($scope, reservationServices, dataTable, $rootScope, $location, $filter) { 
    reservationServices.getReservations().then(function (result) { 
     $scope.data = result.data; 
     $scope.data.currentUser = $rootScope.userInfo.UserId; 
}); 
}]); 

Services.js:

myApp.factory('reservationServices', ['apiServices', function (apiServices) { 

    var factoryDefinitions = { 
     getReservations: function() { 
      return apiServices.getFromTalentPool('/reservations').success(function (data) { return data; }); 
     }, 
     getAccounts: function() { 
      return apiServices.getFromHRIS('/customeraccount').success(function (data) { return data; }); 
     } 

} 

    return factoryDefinitions; 
} 
]); 

Die Auswahloptionen zeigt alle Konten aus der API. Ich möchte, dass es nur Optionen anzeigt, die die Bedingung erfüllen data.currentUser==p.account.programManager.id

Wie kann ich das erreichen?

Bitte lassen Sie mich wissen, wenn weitere Details in der Frage erforderlich sind.

+0

Sie können Daten schleifen und sie je nach Zustand im Controller in ein Array einfügen. –

+0

Ich bin neu zu kantig und noch in der Lernphase. Kannst du mir bitte zeigen, wie ich das in der Antwort mache? – Phoenix

+0

können Sie Ihre Datenquelle zur Verfügung stellen und wenn Sie Ihre HTTP-calla mime machen können, wäre das eine große Sache –

Antwort

1

Below I-Code-Snippet gemacht haben, die die Nutzer mit p.id=10 in der Dropdown-Liste angezeigt wird, wo können mehrere Benutzer sein, in Ihrem Fall $scope.userId =$rootScope.userInfo.UserId;

Und in HTML Code unten hinzufügen

<select ng-model="reservation.account.id" required> 
    <option ng-repeat="p in accounts" ng-if="p.id ==userId " value="{{p.id}} ">{{p.name}}</option> 
</select> 

angular.module('app', []); 
 
angular.module('app').controller('getReservationController', function($scope) { 
 

 
    $scope.reservation = {}; 
 
    $scope.reservation.id = 10; 
 
    $scope.accounts = [{ 
 
    "id": 10, 
 
    "name": "Admin" 
 
    }, { 
 
    "id": 12, 
 
    "name": "User" 
 
    }, { 
 
    "id": 5, 
 
    "name": "Super user" 
 
    }, { 
 
    "id": 1, 
 
    "name": "Super Admin" 
 
    }, { 
 
    "id": 10, 
 
    "name": "Admin2" 
 
    }]; 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app"> 
 
    <form id="edit-profile" novalidate name="editReservationForm" autocomplete="off" class="form-horizontal" ng-controller="getReservationController"> 
 
    <fieldset> 
 
     <div class="control-group"> 
 
     <label class="control-label" for="reservation.account.name">Account Name<sup>*</sup> 
 
     </label> 
 
     <div class="controls"> 
 
      <select ng-model="reservation.id" required> 
 
      <option ng-repeat="p in accounts" ng-if="p.id=='10'" value="{{p.id}} ">{{p.name}}</option> 
 
      </select> 
 
     </div> 
 
     <!-- /controls --> 
 
     </div> 
 
     <!-- /control-group --> 
 
    </fieldset> 
 
    </form> 
 

 
</div>

+0

Vielen Dank! Das ist, was ich war Suche nach. Sie haben mir sehr geholfen. :) – Phoenix

+0

Ich habe ein anderes Problem mit Datum Zeit Picker. Ich werde die Frage bald posten. Bitte überprüfen Sie es, wenn Sie die Zeit sparen können. Sie scheinen meine Frage gut zu verstehen, obwohl ich die Details nicht liefern konnte. Tnx wieder :) – Phoenix

+0

Prost jederzeit :) Ping den Link, nachdem Sie die Frage stellen;) –

0

Sie können eine Sache auch:

in Controller-Code:

myApp.controller('getReservationController', ['$scope', 'reservationServices', 'dataTable', '$rootScope', '$location', '$filter', function ($scope, reservationServices, dataTable, $rootScope, $location, $filter) { 
    reservationServices.getReservations().then(function (result) { 
     $scope.data = result.data; 
     $scope.data.currentUser = $rootScope.userInfo.UserId; 
     $scope.FinalData = $filter('filter')($scope.data, { currentUser : p.account.programManager.id }); 
}); 
}]); 

dann $ scope.FinalData als Array. Lassen Sie mich für mehr Klarheit wissen.

+0

können Sie $ scope.data.currentUser anstelle von p.account.programManager.id nach Ihrer Anforderung verwenden. –