简体   繁体   English

Vue中搜索结果的复选框过滤器

[英]Checkbox Filter for Search Result in Vue

I am trying to create a query table with checkbox filter functionality.我正在尝试创建一个带有复选框过滤器功能的查询表。 You can simply run the following code snippet to understand more easily.您可以简单地运行以下代码片段以更容易理解。

When user ticks the checkboxes, the following table should be filtered accordingly.当用户勾选复选框时,应相应过滤下表。

Now, I have following questions:现在,我有以下问题:

  1. How can I make all checkboxes are checked in default?如何使所有复选框默认选中? (When user click "Search fruit", they can originally see all checkboxes are checked and all fruits are shown.) I tried to input checked attributed in the HTML, but it doesn't work. (当用户单击“搜索水果”时,他们最初可以看到所有复选框已选中并显示所有水果。)我尝试在 HTML 中输入checked属性,但它不起作用。

  2. How to bind selected checkboxes with the following table to have a filter functionality?如何将选定的复选框与下表绑定以具有过滤功能? I tried to use v-on:click on each checkbox, but it seems not working...我尝试使用v-on:click每个复选框,但它似乎不起作用......

The following is the simple code snippet:以下是简单的代码片段:

 new Vue({ el: '#app', data: { isSearched: false, searchList: [], data: [ { "name": "apple", "color": "red" }, { "name": "banana", "color": "yellow" }, { "name": "strawberry", "color": "red" }, { "name": "avocado", "color": "green" }, { "name": "kiwi", "color": "green" }, ], selected: [] }, computed: { colors() { let colors = []; this.searchList?.forEach(fruit => { if(colors.indexOf(fruit.color) === -1){ colors.push(fruit.color); } }); return colors; }, selectAll: { get(){ return this.colors.length == this.selected.length; }, set(value){ let selected = []; if (value) { this.colors.forEach(color => { selected.push(color); }); } this.selected = selected; } } }, methods: { search() { this.searchList = this.data; this.isSearched = true; } } });
 table, td, th{ border: 1px solid black; border-collapse: collapse; }
 <script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script> <div id="app"> <button @click="search">Search Fruit</button> <br/><br/> <div v-if="isSearched"> <table> <tr> <th colspan="2">Filter</th> </tr> <tr> <th> <input type="checkbox" v-model="selectAll" /> </th> <th align="left">All Color</th> </tr> <tr v-for="color in colors"> <td> <input type="checkbox" v-model="selected":value="color" checked/> </td> <td>{{ color }}</td> </tr> </table> <br/> <table> <tr> <th>Name</th> <th>Color</th> </tr> <tr v-for="fruit in searchList"> <td>{{ fruit.name }}</td> <td>{{ fruit.color }}</td> </tr> </table> </div> </div>

The reason why checked attribute does not work initially is here :最初checked属性不起作用的原因在这里

v-model will ignore the initial value, checked, or selected attributes found on any form elements. v-model 将忽略在任何表单元素上找到的初始值、选中或选定属性。 It will always treat the Vue instance data as the source of truth.它将始终将 Vue 实例数据视为事实来源。 You should declare the initial value on the JavaScript side, inside the data option of your component.您应该在组件的数据选项内的 JavaScript 端声明初始值。

Therefore this.selected = [...this.colors];因此this.selected = [...this.colors]; is in search method in the following code snippet.在以下代码段中的search方法中。 Also, searchList is a computed property and colors computed property has been changed for filter.此外, searchList是一个计算属性,并且colors计算属性已更改为过滤器。

 new Vue({ el: '#app', data: { isSearched: false, data: [{ "name": "apple", "color": "red" }, { "name": "banana", "color": "yellow" }, { "name": "strawberry", "color": "red" }, { "name": "avocado", "color": "green" }, { "name": "kiwi", "color": "green" }, ], selected: [] }, computed: { colors() { let colors = []; this.data.forEach(fruit => { if (colors.indexOf(fruit.color) === -1) { colors.push(fruit.color); } }); return colors; }, selectAll: { get() { return this.colors.length == this.selected.length; }, set(value) { let selected = []; if (value) { this.colors.forEach(color => { selected.push(color); }); } this.selected = selected; } }, searchList() { const filteredList = []; this.selected.forEach(color => { this.data.forEach(x => { if (x.color === color) { filteredList.push(x); } }); }); return filteredList; } }, methods: { search() { this.isSearched = true; this.selected = [...this.colors]; } } });
 table, td, th { border: 1px solid black; border-collapse: collapse; }
 <script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script> <div id="app"> <button @click="search">Search Fruit</button> <br/><br/> <div v-if="isSearched"> <table> <tr> <th colspan="2">Filter</th> </tr> <tr> <th> <input type="checkbox" v-model="selectAll" /> </th> <th align="left">All Color</th> </tr> <tr v-for="color in colors"> <td> <input type="checkbox" v-model="selected":value="color" /> </td> <td>{{ color }}</td> </tr> </table> <br/> <table> <tr> <th>Name</th> <th>Color</th> </tr> <tr v-for="fruit in searchList"> <td>{{ fruit.name }}</td> <td>{{ fruit.color }}</td> </tr> </table> <br/> <br/> <br/> <br/> <br/> <br/> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM