【问题标题】:Filter product by checkbox vue js通过复选框vue js过滤产品
【发布时间】:2019-07-20 04:34:18
【问题描述】:

我正在尝试在 laravel 中使用 vue js 实现复选框过滤器,但它没有按预期工作。对于第一个复选框 All ,当我选中和取消选中复选框时,它可以工作并显示/隐藏所有产品。但是对于科技、娱乐和小说,它不会根据单击的复选框来过滤产品。

<template>
    <div class="container">
        <div class="row ">
            <div class="col-md-4">
                <div class="filter">
                    <label><input type="checkbox" v-model="selectedCategory" value="0" /> All</label>
                    <label><input type="checkbox" v-model="selectedCategory" value="1" /> Tech</label>
                    <label><input type="checkbox" v-model="selectedCategory" value="2" /> Entertainment</label>
                    <label><input type="checkbox" v-model="selectedCategory" value="3" /> Fictional</label>
                </div>

    </div>
            </div>
            <div class="col-md-8">
                <div class="card" v-for="product in filteredProduct">
                    <div class="card-header">{{product. name}}</div>

                    <div class="card-body">
                        <img :src="product.image">
                        <p>{{product.price}}</p>
                        <p>{{product.category_id}}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {

        data:function(){
            return {
                products:[],
                selectedCategory: ["0"],

            }

        },
        computed: {
        filteredProduct: function() {
        var app = this;
        var category = app.selectedCategory;
        console.log(category)

        if(category.includes("0")) {
            return app.products;
        } else {

            return app.products.filter(function(p) {

            return category.includes(p.category_id);
        });

            }
        }
    },
        mounted() {
            console.log('Product mounted.')
            var app = this;
            axios.get('/products')
                .then(function (resp) {
                    app.products = resp.data;
                })
                .catch(function (resp) {
                    console.log(resp);
                    alert("Could not load");
                });
        }
    }
</script>

任何帮助将不胜感激

【问题讨论】:

    标签: javascript laravel vue.js vuejs2


    【解决方案1】:

    我的猜测是 category_id 中的 products 数组的类型是 number。但是,selectedCategory 数组具有字符串格式的数字。 includes 使用 严格相等 检查提供的值是否存在于数组中。这就是它适用于"0" 的原因,因为它是作为字符串提供的。如果你把它改成p.category_id.toString(),就可以了:

    这是一个有效的 sn-p:

    new Vue({
      el: '#app',
      data: function() {
        return {
          products: [{ category_id: 1, name: "Tech 1" }, { category_id: 1,name: "Tech 2"},{ category_id: 2, name: "Entertainment 1" },{ category_id: 3, name: "Fictional 1" }],
          selectedCategory: ["0"],
        }
      },
      computed: {
        filteredProduct: function() {
          const app = this,
            category = app.selectedCategory;
    
          if (category.includes("0"))
            return app.products;
          else
            return app.products.filter(p =>
              category.includes(p.category_id.toString())
            );
        }
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    
    <div class="container" id="app">
      <div class="row ">
        <div class="col-md-4">
          <div class="filter">
            <label><input type="checkbox" v-model="selectedCategory" value="0" /> All</label>
            <label><input type="checkbox" v-model="selectedCategory" value="1" /> Tech</label>
            <label><input type="checkbox" v-model="selectedCategory" value="2" /> Entertainment</label>
            <label><input type="checkbox" v-model="selectedCategory" value="3" /> Fictional</label>
          </div>
    
        </div>
      </div>
      <div class="col-md-8">
        <div class="card" v-for="product in filteredProduct">
          <div class="card-header">{{product. name}}</div>
        </div>
      </div>
    </div>

    【讨论】:

    • 我想删除所有复选框,并希望首先在页面中显示所有产品,并希望仅基于技术、小说等复选框而不是所有复选框来过滤数据。所以如果用户不检查技术检查,如果他点击技术复选框,则显示所有结果并过滤数据。如何做到这一点?
    • @David 如果选择了Tech,您要取消选择All吗?
    • @David 嗨,请点击答案左侧的灰色复选标记接受答案
    猜你喜欢
    • 1970-01-01
    • 2020-09-10
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2019-03-01
    • 2019-09-30
    • 1970-01-01
    相关资源
    最近更新 更多