gpt4 book ai didi

javascript - 通过 ng-repeat 的 Angular 多重过滤器

转载 作者:行者123 更新时间:2023-11-28 01:15:59 25 4
gpt4 key购买 nike

我有具有各种属性的对象列表。以及我应该用作过滤器的对象列表。

  $scope.filters = [
{ title: "title1", options: [
{ text: "all", tags: ["1","2","3"] },
{ text: "1", tags: ["1"] },
{ text: "2", tags: ["2"] },
{ text: "3", tags: ["3"] }
]
},
{ title: "title2", options: [
{ text: "all", tags: ["a","b","c"] },
{ text: "a", tags: ["a"] },
{ text: "b", tags: ["b"] },
{ text: "c", tags: ["c"] }
]
}]
$scope.products = [
{ name: "foo", tags: ["1", "a","main"] },
{ name: "bar", tags: ["2", "b", "second"] },
{ name: "baz", tags: ["1", "c", "second"] },
{ name: "tap", tags: ["3", "c", "main"] }
]

我将过滤器显示为选择元素。

 <div ng-repeat="filter in filters">
<label for="filter">{{::filter.title}}</label>
<select name="filter" ng-options="choice as choice.text for choice in filter.options track by choise.tags"></select>
</div>

但是我如何在这里使用过滤器呢? http://plnkr.co/edit/S18xKkXPieWEBvMD3FqJ?p=preview

要使选定的选项在主 Controller 中可用,我可以编写 ng-model="$parent.selectedOption" 但如果任何其他选择发生更改,它将被重写,我不确定是不是像这样篡改父作用域是正确的做法。

编辑:

我想使用一组过滤器(通过选择定义)来过滤产品。

例如:

select1 = "2"
select2 = "b"

然后过滤的产品应该只包含那些 tags 属性包含来自选择的值

最佳答案

我已更新 plnkr 以根据下拉列表中选择的值显示产品。我还对 js 变量进行了一些更改以实现此目的。但它应该给你一个实现这个的想法。希望能帮助到你。 http://plnkr.co/edit/i0edlLRkIpmgW3fNyKsN?p=preview

    // Code goes here
var app = angular.module("myApp", []);


app.controller("MainCtrl", function ($scope) {
$scope.filters = [
{ title: "type", filterBy: "all", options: [
{ text: "all", tags: ["1","2","3"] },
{ text: "1", tags: ["1"] },
{ text: "2", tags: ["2"] },
{ text: "3", tags: ["3"] }
]
},
{ title: "condition", filterBy: "all", options: [
{ text: "all", tags: ["a","b","c"] },
{ text: "a", tags: ["a"] },
{ text: "b", tags: ["b"] },
{ text: "c", tags: ["c"] }
]
},
{ title: "group", filterBy: "all", options: [
{ text: "all", tags: ["main","second"] },
{ text: "main", tags: ["main"] },
{ text: "second", tags: ["second"] }
]
}
];

$scope.products = [
{ name: "foo", type: "1", condition: "a", group: "main"},
{ name: "bar", type: "2", condition: "b", group: "second"},
{ name: "baz", type: "1", condition: "c", group: "second"},
{ name: "tap", type: "3", condition: "c", group: "main"}
];

$scope.filterProduct = function(product) {
var isValid = true;
angular.forEach($scope.filters, function(filter){
if(!(filter.filterBy === "all" || filter.filterBy === product[filter.title])) {
isValid = false;
}
});

return isValid;

}

});
<!DOCTYPE html>
<html>

<head>
<script data-require="angularjs@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
</head>

<body ng-app="myApp">
<div ng-controller="MainCtrl">
<div>
<h2>filters: </h2>
<div ng-repeat="filter in filters">
<label for="filter">{{filter.title}}</label>
<select name="filter" ng-model="filter.filterBy">
<option ng-repeat="option in filter.options" value="{{option.text}}">{{option.text}}</option>
</select>
</div>
</div>
<div>
<h2>data: </h2>
<ul ng-repeat="product in products | filter:filterProduct">
<li>{{::product.name}}</li>
</ul>
</div>
</div>
</body>

</html>

关于javascript - 通过 ng-repeat 的 Angular 多重过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35697712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com