【问题标题】:Filter the list items using checkbox in vue使用vue中的复选框过滤列表项
【发布时间】:2019-04-01 12:51:45
【问题描述】:

我有一些简单的列表项和过滤器面板codesandbox

<template>
  <div id="filter">
    <h1>Filter List</h1>

    <div class="filter-panel">
      <ul class="filter-list">
        <li :key="index" v-for="(ch, index) in checkboxOptions">
          <label>
            <input 
              type="checkbox" 
              v-model="ch.selected" 
               :value="ch.value">
            <span> {{ ch.text }} </span>
          </label>
        </li>
      </ul>
      <hr>
      <ul class="filter-list">
        <li :key="index" v-for="(r, index) in radioOptions">
          <label>
            <input 
              type="radio" 
              v-model="selected" 
               :value="r.value">
            <span> {{ r.text }}</span>
          </label>
        </li>
      </ul>
    </div>


    <transition-group name="fade" mode="out-in" tag="ul" class="catalog-list">
      <li :key="index" v-for="(item, index) in filterItems">
        <h3>{{ item.name }}</h3>
        <span>{{ item.price }}</span>
      </li>
    </transition-group>

  </div>
</template>

<script>

export default {
  name: "FilterList",
  data() {
    return {
      items: [
        { name: "Name 1", price: "200" },
        { name: "Name 2", price: "100" },
        { name: "Name 3", price: "5" }
      ],
      selected: "all",
      radioOptions: [
        { text: 'All', value: 'all' },
        { text: 'Filter Name 1', value: 'Name 1' },
        { text: 'Filter Name 2', value: 'Name 2' },
        { text: 'Filter Name 3', value: 'Name 3' }
      ],
      checkboxOptions: [
        { text: 'Filter 200', value: '200', selected: false },
        { text: 'Filter 100', value: '100', selected: false },
        { text: 'Filter 5', value: '5', selected: false }
      ]
    };
  },
  computed: {
    filterItems() {
      var vm = this,
          name = vm.selected;

          //console.log(vm.checkboxOptions.value);

      if(name === "all"){
        return vm.items;
       } else {
         return this.items.filter(function(n){
          return n.name === name;
        });
       }      
    }
  }
};
</script>

过滤器面板包括:input[type="radio"]input[type="checkbox"]。按名称和价格过滤。

按名称过滤单选按钮有效。我无法为复选框获取 selected 并按价格值过滤项目。

问题:如何使用单选和复选框输入过滤项目列表?如何使用复选框过滤价格?

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    您没有在过滤器功能中使用选中的复选框值。您可以使用:

      computed: {
        filterItems() {
          var vm = this,
              name = vm.selected;
    
              console.log(name);
          var currentItems = vm.items;
          if (name !== 'all') {
             currentItems = currentItems.filter(function(n){
              return n.name === name;
            });
          }
          vm.checkboxOptions.forEach(option => {
            if (option.selected) {
              currentItems = currentItems.filter(item => item.price === option.value)
            }
          })
          return currentItems;      
        }
      }
    

    【讨论】:

    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 2015-05-13
    • 2012-06-12
    • 2017-03-19
    • 2020-10-09
    • 1970-01-01
    • 2020-11-30
    • 2021-10-30
    相关资源
    最近更新 更多