【问题标题】:Filter a list of products with multiple selections过滤具有多项选择的产品列表
【发布时间】:2023-03-05 22:06:01
【问题描述】:

我正在构建一个类似于 Amazon 的产品过滤器,但似乎无法让多个过滤器完美运行。目标是组合过滤器以仅显示符合所选过滤器的产品。

这是我目前的过滤:

$scope.$watch('search', function(newVal, oldVal) {

    if(newVal !== oldVal) {

        var filteredProducts = $scope.products

        if(newVal.types) {
            filteredProducts = $filter('filter')(filteredProducts, { type: newVal.types })
        }
        if(newVal.vendors) {
            filteredProducts = $filter('filter')(filteredProducts, { vendor: newVal.vendors })
        }
        if(newVal.colors) {
            filteredProducts = $filter('filter')(filteredProducts, { variants: { options: newVal.colors } })
        }

        if(filteredProducts.length) {
            $('.product').hide();
            _.each(filteredProducts, function(elm) {
                $('.product[data-id="'+elm.id+'"]').show();
            })              
        } else {
            $('.product').show();
            filteredProducts = $scope.products
        }

    }
}, true)

这是一个半工作示例:

http://codepen.io/rustydev/pen/NxpRqq/

理想情况下,仅当过滤列表包含这些选项时才会启用复选框。

谢谢!

【问题讨论】:

  • 每次进入手表时,您都会设置filteredProducts = $scope.products。因此,您的过滤器每次都基于整个产品列表。

标签: angularjs


【解决方案1】:

只需在集合上运行一个函数,该函数可以根据产品的options 获取值。您需要样品吗?

解决方案:

代替:

$('.product').show();

做:

$('.product').hide();

http://codepen.io/iamisti/pen/WrpGRw?editors=101

说明: 如果您的过滤器没有处理任何数据,则不应显示您的产品。你应该把它们隐藏起来。

【讨论】:

  • 一个样本会很棒。谢谢。
  • 添加了一个例子,代码不是很“干净”,例如:有点回调地狱,但你可以让它更好。我只是向你展示了这是可能的。
  • 谢谢。如果您选择“棕色”,然后选择“衬衫”,则结果不正确。
  • 我认为您在按产品收集不同值时遇到问题。
  • 然后等一下,我再举一个例子:P
猜你喜欢
  • 2013-10-20
  • 2014-11-26
  • 1970-01-01
  • 2015-02-19
  • 2014-12-04
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
相关资源
最近更新 更多