[英]Hide/show table rows based on checkbox
I have some checkboxes which I'd like to use to show/hide rows in a table whose contents match the values of the checkbox which is selected. 我有一些复选框,我想使用它们来显示/隐藏表中的行,这些表的内容与所选复选框的值匹配。
Checkboxes: 复选框:
<input type='checkbox' name='foo1' value='foo1' v-model="selectedType"/> foo1
<input type='checkbox' name='foo2' value='foo2' v-model="selectedType"/> foo2
<input type='checkbox' name='bar1' value='bar1' v-model="selectedType"/> bar1
I have an object which I used to construct a table using v-for: 我有一个用于使用v-for构造表的对象:
<table>
<template v-for="sampleItem in sampleObj">
<tr>
<td>{{sampleItem.item}}</td>
<td>{{sampleItem.description}}</td>
</tr>
</template>
</table>
JS: JS:
new Vue({
data: {
selectedType: [],
sampleObj = [{'item': 'item1', 'description': 'foo1 blah'},
{'item': 'item2', 'description': 'foo2 vlah'},
{'item': 'item3', 'description': 'bar1 nlah'},
{'item': 'item4', 'description': 'bar2 clah'},
];
}
});
By default, the checkboxes are unchecked. 默认情况下,未选中该复选框。 So, only the row which has a cell with description 'bar2' is initially visible. 因此,最初只有具有单元格描述为“ bar2”的行才可见。 Then when I toggle the other checkboxes, the other rows should also become visible (the descriptions don't match the checkbox values verbatim but has a few words following it. I can do some string processing here). 然后,当我切换其他复选框时,其他行也应该变为可见(说明与逐字复选框值不匹配,但后面跟着几个单词。我可以在此处进行一些字符串处理)。
I thought I could use the v-if directive in the tag to look at value of selectedType, but I am not sure how I can accomplish this. 我以为可以在标记中使用v-if指令来查看selectedType的值,但是我不确定如何完成此操作。
Pseudo-code: 伪代码:
<tr v-if="selectedType ~= /sampleItem.description/">
...
...
</tr>
How could I accomplish this? 我该怎么做?
You actually have two conditions you need for the v-if
: if there is no checkbox matching the description, you want the row to display; v-if
实际上有两个条件:如果没有与描述匹配的复选框,则希望显示该行; if there is a checkbox, it has to be checked. 如果有一个复选框,则必须选中它。
I put the checkbox values into data, where they belong. 我将复选框值放入它们所属的数据中。 I made a method for the test. 我做了测试方法。 It first looks to see whether the description matches any checkbox value, then it checks whether the matched value is selected. 它首先查看说明是否匹配任何复选框值,然后检查是否选择了匹配的值。
new Vue({ el: '#app', data: { selectedType: [], sampleObj: [{ 'item': 'item1', 'description': 'foo1 blah' }, { 'item': 'item2', 'description': 'foo2 vlah' }, { 'item': 'item3', 'description': 'bar1 nlah' }, { 'item': 'item4', 'description': 'bar2 clah' }, ], cbValues: ['foo1', 'foo2', 'bar1'] }, methods: { isVisible(row) { const matchedValue = this.cbValues.find(v => row.description.indexOf(v) >= 0); if (!matchedValue) { return true; } return this.selectedType.includes(matchedValue); } } });
td { border: thin solid black; }
<script src="//unpkg.com/vue@latest/dist/vue.js"></script> <div id="app"> <div v-for="val in cbValues"> <label> <input type='checkbox' :value='val' v-model="selectedType"> {{val}} </label> </div> <table> <template v-for="sampleItem in sampleObj"> <tr v-if="isVisible(sampleItem)"> <td>{{sampleItem.item}}</td> <td>{{sampleItem.description}}</td> </tr> </template> </table> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.