how to select table view pending by default

176 views Asked by At

HTML

<select class="form-control" id="selectrequest" ng-model="selected request" ng-change="vm.selected_requested()">
    <option value="pending" > Pending </option>
    <option value="approved"> Approved </option>
    <option value="rejected"> Rejected </option>
</select>

JS

vm.model=[{
    name:'abc',
    lastname:'xyz',
    status:pending
}]

//need to select pending items by default vm.selected_requested = function (){

}
2

There are 2 answers

1
Venkatesh Konatham On BEST ANSWER

Hi I have implemented this functionality as you requested. Now you can see the samples of filtering from view as well from controller. Let me know if you need any further help

  var mainApp = angular.module("mainApp", []); 
       mainApp.controller('studentController', function($scope,filterFilter) {
        var vm = $scope;
         $scope.model=[{ name:'abc_pending', lastname:'xyz', status:"pending" },
                     { name:'abc1_pending', lastname:'xyz', status:"pending" },
                     { name:'abc2_approved', lastname:'xyz', status:"approved" },
                     { name:'abc3_rejected', lastname:'xyz', status:"rejected" }];   
         
         vm.filteredArray = []; 
         vm.selected_requested = function (){
           vm.filteredArray = filterFilter($scope.model, {status:$scope.selected_request});
        
         }
          });
<html>
   
   <head>
      <title>Angular JS Controller</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
   </head>
   
   <body>
      <h2>AngularJS Sample Application</h2>
      
      <div ng-app = "mainApp" ng-controller = "studentController">
        <select class="form-control" id="selectrequest" ng-model="selected_request" ng-change="selected_requested()">
                    <option value="" > All </option>
                    <option value="pending" > Pending </option>
                    <option value="approved"> Approved </option>
                    <option value="rejected"> Rejected </option>
                </select> 
        <div ng-repeat = "temp in model | filter:(!!selected_request || undefined) && {status: selected_request} ">
           <span ng-bind ="temp.name">
             </span>
           </div>
        <br/>
       Items in filtered Array <br/>
     <div ng-repeat = "temp in filteredArray">
           <span ng-bind ="temp.name">
             </span>
           </div>
      </div> 
      
   </body>
</html>

0
Balaji Marimuthu On

Use angular filter in the selected_requested function to filter the item based on selected option. Or else you can create the custom filter using $filter. Refer to the document https://docs.angularjs.org/api/ng/filter/filter

    angular.module('FilterInControllerModule', []).
        controller('FilterController', ['$scope','filterFilter', function($scope,filterFilter) {
          $scope.data=[]; //use your table data.
         vm.selected_requested = function (){


          $scope.filteredArray = filterFilter($scope.data, {status:selected request});

} 
        }]);