gpt4 book ai didi

javascript - Ionic Service Factory 中的过滤和排序

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

抱歉新手问题。我想在我的页面中添加按名称过滤和排序。当我尝试使用此代码时,当我在 Controller 范围内使用变量中的项目列表时,它仍然可以工作,但在 services.js 中使用工厂后,存在两个问题:

  1. 当我在搜索框中输入搜索词时,过滤器起作用,仅显示过滤后的项目,但我无法再次清除过滤器(当我用退格键删除搜索词时,该项目仍然被过滤)<
  2. order by 不再适用于对项目进行排序

这是代码:(HTML)

<ion-view view-title="Foods">
<ion-content class="padding">
<div class="bar bar-header item-input-inset">
<label class="item-input-wrapper">
<i class="icon ion-ios-search placeholder-icon"></i>
<input type="search" placeholder="Search" ng-model="foodSearch.search">
</label>

</div>
<button class="button button-block button-energized" ng-click="showOrderOptions()">
Order by
</button>
<div class="list ">


<a class="item" href="#" ng-hide="isOrderOptionsHide" ng-click="reverse=!reverse;order('name',reverse)">
Name
</a>

<a class="item" href="#" ng-hide="isOrderOptionsHide" ng-click="reverse=!reverse;order('rating',reverse)">
Rating
</a>

</div>
<ion-list>
<ion-item class="list card" ng-repeat="food in foods = (foods | filter: filterFood)"
href="#/tab/foods/{{food.id}}">
<div class="item item-head">
<h2 style="text-align:center;"><b>{{food.name}}</b></h2>
<div class="row">
<div class="col item item-image" style="width:100px;height:100px;">
<img class="" src="{{food.imageSrc}}" >
</div>
<div class="col item item-image">
<h2>{{food.title}}</h2>
<div class="row">
<img src="img/smile.jpg" style="width:100%; height:100%; margin-top: -10px;"class="col col-40">
<h3 class="col col-60" >{{food.rating}}</h3>
</div>
</div>
</div>
</div>
</ion-item>
</ion-list>
</ion-content>
</ion-view>

( Controller .js):

.controller('FoodsCtrl', function($scope,Foods,$filter) {
$scope.foods = Foods.all();
$scope.remove = function(food) {
Foods.remove(food);
};

$scope.foodSearch = {search: ''};

$scope.filterFood = function (item){
return item.title.toLowerCase().indexOf($scope.foodSearch.search.toLowerCase()) >= 0
|| item.name.toLowerCase().indexOf($scope.foodSearch.search.toLowerCase()) >= 0;
}

$scope.isOrderOptionsHide = true;
$scope.reverse=true;
$scope.showOrderOptions = function (){
$scope.isOrderOptionsHide = !$scope.isOrderOptionsHide;
}
var orderBy = $filter('orderBy');

$scope.order = function(predicate, reverse) {
$scope.foods = orderBy($scope.foods, predicate, reverse);
$scope.isOrderOptionsHide = true;
};
})

最佳答案

就这样做
ng-repeat="food in foods | filter: foodSearch.search | orderBy: 'name'"
您不必这样做:
$scope.filterFood = function (item){
return item.title.toLowerCase().indexOf($scope.foodSearch.search.toLowerCase())..

Angular 过滤器会自动执行此操作。

关于javascript - Ionic Service Factory 中的过滤和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32207678/

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