gpt4 book ai didi

javascript - 嵌套 ng-repeat 的内循环中的 orderBy

转载 作者:行者123 更新时间:2023-11-30 17:08:20 24 4
gpt4 key购买 nike

我想过滤过滤类别中的网站,并按照排名顺序显示。代码片段是:

<div ng-repeat="category in categories | filter:{will return more than one category}">
<div ng-repeat="website in websites | orderBy:'rank'| filter:{ parent_id : category.id }:true">
{{website.title}},{{website.rank}}
</div>
</div>

但是在嵌套循环中,由于orderby是在内层循环中,所以在外层循环的每一次迭代中都起作用,但是整体的结果并没有按照rank排序。假设有三个类别,过滤器给出 cat1 和 cat2。如果排名 6、2、5 的网站是 cat1 和 9,1 在 cat2 中,那么结果将是 2、5、6、1、9。我希望结果是 1、2、5、6、9。如何我应该这样做吗?

我应该在某个函数中传递类别并编写 js 代码来获取过滤网站的数组,然后对它们进行排序并将它们返回到模板,还是有其他更好的方法在模板本身中执行此操作?

最佳答案

我认为你想做的事,不能按原样去做。无论如何你可以使用 custom filter .

新答案

此方法为您提供了一个类别选择机制,作为您如何使用此自定义过滤器的另一个示例。

angular.module('app',[])

// categories
.value('categories', [ { id: 0, title:"first" }, { id: 1, title:"second" }, { id: 2, title:"third" } ])

// websites
.value('websites', [ { rank: 3, parent_id: 2, title: "Alice" },
{ rank: 1, parent_id: 1, title: "Bob" },
{ rank: 9, parent_id: 1, title: "Carol" },
{ rank: 2, parent_id: 0, title: "David" },
{ rank: 4, parent_id: 0, title: "Emma" },
{ rank: 5, parent_id: 0, title: "Foo" } ])

// controller,
.controller('ctrl', ['$scope', 'categories', 'websites', function($scope, categories, websites) {
// categories injected
$scope.categories = categories;
// websites injected
$scope.websites = websites;
// categories selection helper, useful for preselection
$scope.selection = { 0: true, 1:false } // 2: false (implicit)

}])


// categories filter, categories injected.
.filter('bycat', ['categories', function(categories) {

// websites is the result of orderBy :'rank', selection helper passed as paramenter.
return function(websites, selection) {

// just an Array.prototype.filter
return websites.filter(function(website) {
// for each category
for(var i=0; i < categories.length; i++) {
var cat = categories[i];
// if category is selected and website belongs to category
if (selection[cat.id] && cat.id == website.parent_id) {
// include this website
return true;
}
}
// exclude this website
return false;
});
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
<span ng-repeat="category in categories">
<label class="checkbox" for="{{category.id}}">
<input type="checkbox" ng-model="selection[category.id]" name="group" id="{{category.id}}" />
{{category.title}}
</label>
</span>
<div ng-repeat="website in websites | orderBy:'rank'| bycat: selection">
<p>Rank:{{website.rank}} - {{website.title}} ({{categories[website.parent_id].title}})</p>
</div>
</div>

旧答案

Se代码注释。

angular.module('app',[])

// categories will be injected in custom filter.
.value('categories', [ { id: 1, title:"first" }, { id: 2, title:"second" } ])

.controller('ctrl', function($scope) {
// sample websites
$scope.websites = [ { rank: 1, parent_id: 2, title: "Site w/rank 1" },
{ rank: 9, parent_id: 2, title: "Site w/rank 9" },
{ rank: 2, parent_id: 1, title: "Site w/rank 2" },
{ rank: 4, parent_id: 1, title: "Site w/rank 4" },
{ rank: 5, parent_id: 1, title: "Site w/rank 5" } ];

})

// custom filter, categories injected.
.filter('bycat', ['categories', function(categories) {

// websites is the result of orderBy :'rank'
return function(websites, catText) {

// just an Array.prototype.filter
return websites.filter(function(website) {
// if no filter, show all.
if (!catText) return true;

for(var i=0; i < categories.length; i++) {
var cat = categories[i];
// if matches cat.title and id == parent_id, gotcha!
if (cat.title.indexOf(catText) != -1 && cat.id == website.parent_id) {
return true;
}
}
// else were
return false;
});
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
<input type="text" ng-model="filterText">
<p>Try "first" and "second"</p>
<div ng-repeat="website in websites | orderBy:'rank'| bycat: filterText ">
{{website.title}},{{website.rank}}
</div>
</div>

关于javascript - 嵌套 ng-repeat 的内循环中的 orderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27518411/

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