簡體   English   中英

如何使用angularjs使用過濾器進行ng-repeat?

[英]How to do ng-repeat with filter using angularjs?

在這里,我添加了我想根據用戶輸入進行搜索的代碼,但是嘗試了

filter: first_name_model

我沒有得到適當的結果

例如:運行代碼片段

輸入“ 2”並查看結果(我們只需要獲取一條記錄,但它會顯示所有記錄)

輸入“ as”並查看結果(我們只需要獲取一條記錄(asif yohana),但其中顯示2條記錄)

我只想過濾這兩個字段

1.no

2.DisplayName

可能需要使用“ OR”條件。

幫助我前進

 angular.module('myApp', []).controller('namesCtrl', function($scope) { $scope.students = [{ "firstname": "irfan", "lastname": "k", "no":"2", "userid": "5706b916bb729ecc876192d2" }, { "firstname": "asif", "lastname": "Y", "no":"3", "userid": "5706b916bb729ecc87619245" }, { "firstname": "arun", "lastname": "D", "no":"6", "userid": "5706b916bb729ecd876452d2" }, { "firstname": "smart", "lastname": "k", "no":"5", "userid": "5706b916bb729ecc876452d2" }, { "firstname": "rajesh", "lastname": "v", "no":"4", "userid": "5706b916bb729ecc87619245" } ]; $scope.users = [{ "_id": "5706b916bb729ecc87619245", "DisplayName": "irfan kadhar", }, { "_id": "5706b916bb729ecc876192d2", "DisplayName": "asif yohana", }, { "_id": "5706b916bb729ecc876452d2", "DisplayName": "smar kiran", }, { "_id": "5706b916bb729ecd876452d2", "DisplayName": "arun dinesh ", } ] if ($scope.students) { for (var i = 0; i < $scope.students.length; i++) { for (var j = 0; j < $scope.users.length; j++) { if ($scope.students[i].userid == $scope.users[j]._id) { $scope.students[i].AssignedToDisplayName = $scope.users[j].DisplayName; } } } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <div ng-app="myApp" ng-controller="namesCtrl"> filter data <input type = "text" ng-model = "first_name_model"/><br><br><br> <table> <tr> <th>Number</th> <th>DisplayName</th> </tr> <tr ng-repeat="student in students | filter: first_name_model"> <td><input type="text" ng-model ="student.no"></td> <td> <select ng-model="student.userid" placeholder="userid" name="userid{{$index}}"> <option ng-repeat="user in users | orderBy: 'DisplayName'" ng-value="user._id">{{user.DisplayName}}</option> </select> </td> </tr> </table> </div> 

請將輸入的ng-model更改為first_name_model.no

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="namesCtrl">
         filter data <input type = "text" ng-model = "first_name_model.no"/><br><br><br>
         <table>
            <tr>
               <th>Number</th>
               <th>DisplayName</th>
            </tr>

            <tr ng-repeat="student in students | filter: first_name_model">
               <td><input type="text" ng-model ="student.no"></td>

               <td>
                  <select  ng-model="student.userid"  placeholder="Assigned To" name="userid{{$index}}" >
                  <option ng-repeat="user in users | orderBy: 'DisplayName'" ng-value="user._id">
                     {{user.DisplayName}}</md-option>
                     </md-select>
               </td>
            </tr>
         </table>
      </div>

  angular.module('myApp', []).controller('namesCtrl', function($scope) { $scope.search = function(item) { if (!$scope.query || (item.no.toLowerCase().indexOf($scope.query) != -1) || (item.AssignedToDisplayName.toLowerCase().indexOf($scope.query) != -1) ){ return true; } return false; }; $scope.students = [{ "firstname": "irfan", "lastname": "k", "no":"2", "userid": "5706b916bb729ecc876192d2" }, { "firstname": "asif", "lastname": "Y", "no":"3", "userid": "5706b916bb729ecc87619245" }, { "firstname": "arun", "lastname": "D", "no":"6", "userid": "5706b916bb729ecd876452d2" }, { "firstname": "smart", "lastname": "k", "no":"5", "userid": "5706b916bb729ecc876452d2" }, { "firstname": "rajesh", "lastname": "v", "no":"4", "userid": "5706b916bb729ecc87619245" } ]; $scope.users = [{ "_id": "5706b916bb729ecc87619245", "DisplayName": "irfan kadhar", }, { "_id": "5706b916bb729ecc876192d2", "DisplayName": "asif yohana", }, { "_id": "5706b916bb729ecc876452d2", "DisplayName": "smar kiran", }, { "_id": "5706b916bb729ecd876452d2", "DisplayName": "arun dinesh ", } ] if ($scope.students) { for (var i = 0; i < $scope.students.length; i++) { for (var j = 0; j < $scope.users.length; j++) { if ($scope.students[i].userid == $scope.users[j]._id) { $scope.students[i].AssignedToDisplayName = $scope.users[j].DisplayName; } } } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <div ng-app="myApp" ng-controller="namesCtrl"> filter data <input type = "text" ng-model = "query"/><br><br><br> <table> <tr> <th>Number</th> <th>DisplayName</th> </tr> <tr ng-repeat="student in students | filter:search"> <td><input type="text" ng-model ="student.no"></td> <td> <select ng-model="student.userid" placeholder="Assigned To" name="userid{{$index}}"> <option ng-repeat="user in users | orderBy: 'DisplayName'" ng-value="user._id">{{user.DisplayName}}</option> </select> </td> </tr> </table> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM