使用 ng 模型按键入(键,值)对过滤
Filter by key in (key, val) pair using ng-model
我有以下数据:
[
{
"rub": {
"item1": 979,
"item2": 32,
"item3": 845
},
"shop": "shop1",
},
{
"rub": {
"item232": 84,
"item213": 348
},
"shop": "shop2"
}
]
我尝试使用 ng-model
按键在表中过滤它。但它根本没有过滤。
<table class="table ng-cloak" ng-repeat="rub in rubs | filter:isActive" ng-if='isActive'>
<input type="text" class="form-control" placeholder="Товар" ng-model="rub.rub[key]">
<thead>
<tr>
<th>#</th>
<th>Товар</th>
<th>Число</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='(key, val) in rub.rub'>
<td>{{ $index }}</td>
<td>{{ key }}</td>
<td>{{ val }}</td>
</tr>
</tbody>
</table>
我的控制器:
curryControllers.controller('CurryRubricsCtrl', ['$scope', '$routeParams', '$http', '$route',
function($scope, $routeParams, $http, $route) {
$scope.cityId = $routeParams.cityId;
$http.get('cities.json').success(function(data) {
$scope.cities = data;
$http.get('json/shop_data.json').success(function(data2) {
$scope.rubs = data2;
$scope.isActive = function(item) {
return item.shop === $scope.cityId;
};
});
});
我尝试将$scope.searchRub = ''
添加到控制器并在 html 模板中放置一个表单。
<form>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-search"></i></div>
<input type="text" class="form-control" placeholder="Поиск" ng-model="searchRub">
</div>
</div>
</form>
在这里添加了这个"搜索Rub"过滤器:<td> {{ key | filter:searchRub }} </td>
这也无济于事。
您希望搜索框对一个独立的值进行建模,然后可以使用该值进行筛选,而不是尝试将其建模为已在使用的对象的键。
<input type="text" class="form-control" placeholder="Товар" ng-model="search”>
有许多方法可以使用此值进行筛选,但最简单的方法是使用 ng-show:
<tr ng-repeat='(key, val) in rub.rub' ng-show="search ? search===key : true">
这是扑通。 我已经对cityId进行了硬编码,以避免在演示中使用routeParams。
https://plnkr.co/edit/sI8HAbNKBBJGMx0FediD?p=preview
在搜索框中键入"item1"。
您可以使用Underscore.js来实现这一点,
data = [
{
"rub": {
"item1": 979,
"item2": 32,
"item3": 845
},
"shop": "shop1"
},
{
"rub": {
"item232": 84,
"item213": 348
},
"shop": "shop2"
}
]
$scope.specific_key = []
_.filter(data, function(val){$scope.specific_key.push(val['rub'])});
console.log($scope.specific_key);
在$scope.specific_key
中,它会返回包含键'rub'。
相关文章:
- 使用下拉列表过滤 ng 重复,而不复制下拉选项
- 按功能过滤 ng 重复
- Angular JS-过滤ng重复
- 在angularjs中过滤ng重复
- 按输入日期过滤 ng 重复
- 根据本地存储中的列表过滤 ng-repeat
- 过滤NG重复,但保留初始长度/计数
- 使用 AngularJS 过滤 ng-repeat 中的特殊字符
- 使用 ng 模型有条件地过滤 ng 选项
- 使用 Firebase 数据过滤 ng 重复
- 按天数过滤 ng 重复
- 角度 - 按模型值过滤 ng 重复
- 需要删除订单通过过滤 ng 根据条件重复
- 当对象属性为空时,使用AngularJS过滤ng重复
- 基于角度中的多个值过滤ng重复
- 如何用另一个字符串数组过滤ng重复中的字符串数组
- 在Angularjs应用程序中,使用嵌套对象属性过滤ng repeat不起作用
- Angularjs应用程序中使用AND运算过滤ng个基于重复的多个属性
- 如何在Angular中过滤ng repeat中的元素
- 在Angular中过滤ng-repeat