[英]Select all checkbox with Vue.js (issues with for looping inside a .vue file)
[英]Select all checkbox vue
我正在嘗試創建以下內容:
我收到一組對象。 每個對象由一個 id、一個選項和一個組名組成。 我需要以一種方式輸出數據,首先我有一個帶有組名稱屬性值的復選框,然后是具有相同組屬性的復選框列表。 例如,假設我收到以下數據:
[
{id:1, text: 'Football', group: 'sport'}
{id:2, text: 'Basketball', group: 'sport'}
{id:3, text: 'Cookie', group: 'food'}
{id:4, text: 'Soup', group: 'food'}
]
所以輸出應該如下:
<checkbox which when clicked should select all checkboxes belonging to this group>Sport</checkbox>
<checkbox>Football</checkbox>
<checkbox>Basketball</checkbox>
<checkbox which when clicked should select all checkboxes belonging to this group>Food</checkbox>
<checkbox>Cookie</checkbox>
<checkbox>Soup</checkbox>
到目前為止,我有以下幾點:
export default { data() { return { actionTypes: actionTypes, relationTypes: relationTypes, optionsList: [ {id:1, text: 'Football', group: 'sport'} {id:2, text: 'Basketball', group: 'sport'} {id:3, text: 'Cookie', group: 'food'} {id:4, text: 'Soup', group: 'food'} ] optionsGroupsList: ['Sport', 'Food'], localValue: [] },
<link href="https://cdn.jsdelivr.net/npm/vuesax/dist/vuesax.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div v-for="group in optionsGroupsList" class="checkbox-group"> <vs-checkbox :vs-value="group" :key="group" > {{ group }} </vs-checkbox> <vs-checkbox v-for="option in optionsList" v-if="option.group === group" :vs-value="option.id" :key="option.id" v-model="localValue" > {{ option.text }} </vs-checkbox> </div>
所以我確實收到了必要的輸出,但我不知道如何使組復選框以選擇屬於它們(該組)的復選框的方式工作
我真的很感激任何想法,提前致謝!
nestedOptions: [
{
name: "sport",
options: [
{ id: 1, text: "Football", group: "sport" },
{ id: 2, text: "Basketball", group: "sport" },
]
},
{
name:"food"
options: [...]
}
]
export default {
name: "groupedOptionsComponent",
data() {
return {
selectedOptions: [],
selectedGroups: [],
optionsList: [
{ id: 1, text: "Football", group: "sport" },
{ id: 2, text: "Basketball", group: "sport" },
{ id: 3, text: "Cookie", group: "food" },
{ id: 4, text: "Soup", group: "food" }
],
optionsGroupsList: ["sport", "food"]
};
},
computed: {
nestedOptions() {
return this.optionsGroupsList.map(groupName => ({
name: groupName,
options: this.optionsList.filter(({ group }) => group === groupName)
}));
}
},
watch: {
selectedGroups: function(groups) {
this.optionsList.forEach(option => {
if (
groups.includes(option.group) &&
!this.selectedOptions.includes(option.id)
) {
this.selectedOptions.push(option.id);
} else {
this.selectedOptions = this.optionsList
.filter(({ group }) => groups.includes(group))
.map(({ id }) => id);
}
});
}
}
};
使您的模板看起來像這樣:
<template>
<div>
<div v-for="(group, index) in nestedOptions" class="checkbox-group" :key="index">
<vs-checkbox :vs-value="group.name" v-model="selectedGroups">{{ group.name }}</vs-checkbox>
<vs-checkbox
v-for="option in group.options"
:vs-value="option.id"
:key="option.id"
v-model="selectedOptions"
>{{ option.text }}</vs-checkbox>
</div>
</div>
</template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.