离子服务工厂的过滤和排序

时间:2015-08-25 15:09:15

标签: javascript html angularjs ionic

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

  1. 当我在搜索框中输入搜索词时,过滤器正常工作并且仅显示已过滤的项目,但我无法再次清除过滤器(当我使用退格键删除搜索词时,项目仍会被过滤)
  2. 订单不再用于排序项目
  3. 这是代码: (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;
        };
    })
    

1 个答案:

答案 0 :(得分:4)

这样做ng-repeat="food in foods | filter: foodSearch.search | orderBy: 'name'"你不必这样做:
$scope.filterFood = function (item){ return item.title.toLowerCase().indexOf($scope.foodSearch.search.toLowerCase())..
角度滤波器自动完成。