javascript - 如何使用 angularjs 使用过滤器进行 ng-repeat?
问题描述
在这里,我添加了我想根据用户输入搜索的代码我试过了,但是
filter: first_name_model
我没有得到正确的结果
例如:运行代码片段
输入“2”并查看结果(我们只需要获取一条记录,但它会显示所有记录)
输入“as”并查看结果(我们只需要获取一条记录(asif yohana),但它显示了 2 条记录)
我只想过滤这两个字段
1.没有
2.显示名称
可能需要使用“或”条件。
帮助我前进
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>
推荐阅读
- node.js - 在 Windows 上使用 pm2 时如何传递节点参数
- google-bigquery - 在 BigQuery 中复制外部表或其字段的更快方法?
- mysql - 如何在 AWS RDS 中设置当前日期时间?
- php - XPath 中的空格 (PHP)
- json - 从 Pandas 数据框中存在的嵌套 json 中检索 json 值
- php - fseek():流不支持搜索
- pandas - 在熊猫的行和列中对角复制值?
- flutter - 如何使用复选框在 chart_flutter 中隐藏一行图形?
- android - 通知 startForeground() 第二次不工作
- c++ - 被误解的函数的参数