【问题标题】:How to make my button (clear filters) only appears when filters are use如何使我的按钮(清除过滤器)仅在使用过滤器时出现
【发布时间】:2017-05-12 10:01:10
【问题描述】:

这确实是我的问题。我有一个按钮可以清除所有过滤器,但我只希望在使用任何过滤器时显示它。我怎样才能做到这一点?我需要在使用范围过滤器或过滤器酒店时出现。谢谢!!

清除过滤器的按钮

 .filterheader
  .filter-ctn
    .filterheader
      h4 Filtrar
      span(class="delete-filter" ng-click="$ctrl.hotelsResultController.$onInit()")<i class="fa fa-times" aria-hidden="true"></i> Eliminar Filtros
    .filtercontainer

hotel-root-component.js

(function () {
    'use strict';

    angular
        .module('hotelsResult')
        .component('hotelsRoot', {
            bindings: {},
            controller: hotelsResultController,
            templateUrl: "hotel-result/hotels-root.html"
        });

    hotelsResultController.$inject = ['hotelsService'];

    function hotelsResultController(hotelsService) {
        const self = this;

        self.filterOnHotels = [];

        this.$onInit = function () {
            hotelsService.getHotels().then(function (hotels) {
                self.hotels = hotels;
                self.filterOnHotels = hotels;
            });
            this.filters = {
              "name" : "",
              "targetName" : "",
              "price":{
                "priceMin" : 0,
                "priceMax": 3250,
              },
              "stars":{
                "all": true,
                "five": false,
                "four": false,
                "three" : false,
                "two" : false,
                "one" : false
              },
            }
        };

        this.getHotels = function () {
            return self.filterOnHotels;
        };
    }

})();

filter-header.component.js

(

    function (){

      'use strict';

      angular
      .module('filters')
      .component('filterHeader', {
        bindings:{},
        require: {
            hotelsResultController : '^hotelsRoot'
        },
        controller: filterHeaderController,
        templateUrl: "hotel-result/filters/filterheader/filterheader.html"
      });

        function filterHeaderController() {}
    })();

filter-hotel.component.js

(function (){
  'use strict';

  angular
  .module('filters')
  .component('filterHotel', {
    bindings:{
      "filters" : '<'
    },
    templateUrl: 'hotel-result/filters/filterhotel/filterhotel.html'
  }).filter('filterHotel', function() {
      var self = this;
      return function (hotels,targetName) {
          return hotels.filter(function (hotel) {
              return hotel.name.toLowerCase().indexOf(targetName.toLowerCase()) != -1;
          })
      }
    })
})();

filter-range.component.js

(function (){

  'use strict';

  angular
  .module('filters')
  .component('filterNight',{
    controller: filterNightController,
    require: {
      hotelsResultController : '^hotelsRoot'
    },
    bindings:{
      "filters" : '<'
    },
    templateUrl: "hotel-result/filters/filternight/filternight.html"
  }).filter('filterNight',function () {
      return function (hotels,price) {
          return hotels.filter(function (hotel) {
              return (hotel.price >= price.priceMin && hotel.price <= price.priceMax);
          });
      }
    });

  function filterNightController (){
    let self = this;

    this.slider = {
      value: 150,
      options: {
        minRange: 200,
        noSwitching: true,
        pushRange: true,
        onChange : this.filterNight
      }
    };

  }
})();

item-list.jade

ul
    li(ng-repeat="hotel in $ctrl.hotels | filterHotel: $ctrl.filters.targetName | filterStar: $ctrl.filters.stars | filterNight: $ctrl.filters.price")  
        item(item='hotel')

【问题讨论】:

    标签: javascript angular filter


    【解决方案1】:

    您可以通过以下方式保留过滤后的结果:

    ul
    li(ng-repeat="hotel in filterResult = ($ctrl.hotels | filterHotel: $ctrl.filters.targetName | filterStar: $ctrl.filters.stars | filterNight: $ctrl.filters.price)")  
        item(item='hotel')
    

    并将结果与​​原始数据进行比较,如果使用过滤器,则它们将彼此不同。

    span(class="delete-filter" ng-if="filterResult.length !== $ctrl.hotels.length" ng-click="$ctrl.hotelsResultController.$onInit()")<i class="fa fa-times" aria-hidden="true"></i> Eliminar Filtros
    

    参考下面的例子:

    angular.module("app", [])
      .controller("myCtrl", function($scope) {
        $scope.data = [
          {
            id: 1,
            data: 'data for item1'
          },
          {
            id: 2,
            data: 'data for item2'
          },
          {
            id: 3,
            data: 'data for item3'
          },
          {
            id: 4,
            data: 'data for item4'
          }
        ];
      })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <div ng-app="app" ng-controller="myCtrl">
      <input type="text" ng-model="test">
      <button ng-if="filterResult.length !== data.length">Filter Used</button>
      
      <div ng-repeat="item in filterResult = (data | filter: test)">
        <span>{{item.id}} - {{item.data}}</span>
      </div>
    </div>

    【讨论】:

    • 谢谢你!很好的主意!让我做,我给你答案,提前谢谢!!
    • @HernanBessera 基本上这应该有效。检查我添加的示例。
    • 我会感谢人!现在它永远不会改变,因为基本上它们总是一样的。
    • @HernanBessera 那么它不是未经过滤的吗?或者您的意思是检查是否曾经调用过过滤器?
    • 我无法访问酒店,因为我需要调用 ng-if="filterResult.length !== $ctrl.hotelsResultController.hotels.length"
    猜你喜欢
    • 2020-07-15
    • 2016-08-26
    • 2021-05-31
    • 1970-01-01
    • 2012-03-14
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    相关资源
    最近更新 更多