使用 vuejs 选中/取消选中子组件中的复选框

mic*_*ael 5 javascript checkbox vue.js vue-component vuejs2

语境 :

我是第一次在项目中使用 VueJS,所以我可能没有以正确的方式使用它。

我有一个父组件和一个子组件:

父级: ResearchProducts.vue:显示可以按类别过滤的产品列表。过滤器是与产品类别相关的复选框。

Child : CategoryDisplay.vue : 这是处理视图和类别行方法的组件。

我的问题 :

当我单击子组件内的类别复选框并选中该复选框时,该类别将添加到父组件中的过滤器列表中。这有效。

当我取消选中该复选框时,该类别将从该列表中删除。这也有效。

现在,我已将所有类别显示为父组件中的按钮,以使用户更容易看到过滤器。我的问题是我希望这个按钮在我点击它时取消选中子组件内的相关复选框。

这是我的实际代码:

ResearchProducts.vue :

<template>
 <div>
     <template v-for="category in categories">
          <category-display
                 :category="category"
                 :key="'category_'+category.id"
                 :checked="checked"
                 @categorySelected="categorySelected"
               ></category-display>
     </template>
     <button
               v-for="filter in listFilters"
               :key="'filter_'+filter.slug"
               class="btn btn-light btn-sm mr-2 mb-2"
               @click="removeFilter(filter)"
             >
               {{ filter.name }}
      </button>
 </div>
</template>
<script>
export default {

  data() {

    return {

      categories: { // HERE A COLLECTION CONTAINING ALL MY CATEGORIES },

      selectedCategories: [],

      listFilters: []

    };

  },

  methods: {

    categorySelected(category) {

      var newCategory = {

        type: "category",

        slug: category.slug,

        name: category.name

      };

      if (category.checked) {

        if (!this.selectedCategories.includes(category.slug)) {

          this.selectedCategories.push(category.slug);

          this.listFilters.push(newCategory);

        }

      } else {

        if (this.selectedCategories.includes(category.slug)) {

          const index = this.selectedCategories.indexOf(category.slug);

          if (index > -1) {

            this.selectedCategories.splice(index, 1);

          }

        }

        this.listFilters = this.listFilters.filter(function(item) {

          for (var key in newCategory) {

            if (item[key] === undefined || item[key] == newCategory[key])

              return false;

          }

          return true;

        });

      }

    },

    removeFilter(filter) {
     // THERE, I NEED TO UNCHECK THE RELATED CHECKBOX IN CHILD COMPONENT
     this.listFilters = this.listFilters.filter(function(item) {
        for (var key in filter) {
          if (item[key] === undefined || item[key] == filter[key]) return false;
        }
        return true;
      });

    }

  }

};
</script>
Run Code Online (Sandbox Code Playgroud)

类别Display.vue :

<template>
  <b-form-checkbox-group class="w-100">
    <b-form-checkbox :value="category.slug" class="w-100" @input="selection" v-model="selected" ref="checked">
      {{ category.name }}
      <span class="badge badge-secondary float-right">{{ category.products_count }}</span>
    </b-form-checkbox>
  </b-form-checkbox-group>
</template>

<script>
export default {
  props: {
    category: {
      type: Object,
      required: true
    }
  },

  data() {
    return {
      selected: false
    }
  },

  methods: {
    selection() {
      var response = false;
      if(this.selected.length !== 0){
        response = true;
      }
      this.$emit('categorySelected', {slug: this.category.slug, name: this.category.name, checked: response});
    }
  }
};
</script>
Run Code Online (Sandbox Code Playgroud)

AJT*_*T82 6

这是一个简化的示例供您参考。您可以使用sync修饰符来实现父级和子级之间的双向绑定。setter与带有 a和的子级中的计算属性一起getter

因此将所有类别传递给子级,并同步所选类别:

<HelloWorld :cats.sync="selectedCategories" :categories="categories"/>
Run Code Online (Sandbox Code Playgroud)

子组件采用类别、迭代并显示复选框。这里我们使用计算属性,当单击复选框时,会将setter更改发送给父级:

<label v-for="c in categories" :key="c.id">
  <input v-model="temp" type="checkbox" :value="c">
  {{ c.name }}
</label>
Run Code Online (Sandbox Code Playgroud)

脚本:

computed: {
  temp: {
    get: function() {
      return this.cats;
    },
    set: function(newValue) {
      this.$emit("update:cats", newValue);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

并且父级只是简单地迭代selectedCategoriesand 如您所愿,当父级发生更改时selectedCategories,子级将自动意识到何时删除了项目。

这是一个完整的示例供您参考:SANDBOX