|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div class="d-flex ga-4 mb-4 align-center"> |
| 4 | + <v-switch v-model="openAll" label="Open all groups" hide-details></v-switch> |
| 5 | + <v-btn size="small" variant="tonal" @click="opened = []">Close all</v-btn> |
| 6 | + </div> |
| 7 | + |
| 8 | + <v-data-iterator |
| 9 | + v-model:opened="opened" |
| 10 | + :group-by="[{ key: 'category' }]" |
| 11 | + :group-key="({ value }) => value" |
| 12 | + :items="items" |
| 13 | + :items-per-page="-1" |
| 14 | + :open-all-groups="openAll" |
| 15 | + > |
| 16 | + <template v-slot:default="{ groupedItems, toggleGroup, isGroupOpen }"> |
| 17 | + <v-row> |
| 18 | + <template v-for="groupOrItem in groupedItems" :key="groupOrItem.type === 'group' ? groupOrItem.id : groupOrItem.raw.name"> |
| 19 | + <v-col v-if="groupOrItem.type === 'group'" cols="12"> |
| 20 | + <v-card |
| 21 | + variant="tonal" |
| 22 | + @click="toggleGroup(groupOrItem)" |
| 23 | + > |
| 24 | + <v-card-title class="d-flex align-center"> |
| 25 | + <v-icon |
| 26 | + :icon="isGroupOpen(groupOrItem) ? 'mdi-chevron-down' : 'mdi-chevron-right'" |
| 27 | + class="me-2" |
| 28 | + ></v-icon> |
| 29 | + {{ groupOrItem.value }} |
| 30 | + <v-chip class="ms-2" size="small" variant="outlined"> |
| 31 | + {{ groupOrItem.items.length }} |
| 32 | + </v-chip> |
| 33 | + </v-card-title> |
| 34 | + </v-card> |
| 35 | + </v-col> |
| 36 | + |
| 37 | + <v-col v-else cols="12" md="4" sm="6"> |
| 38 | + <v-card> |
| 39 | + <v-card-title>{{ groupOrItem.raw.name }}</v-card-title> |
| 40 | + <v-card-subtitle>{{ groupOrItem.raw.origin }}</v-card-subtitle> |
| 41 | + <v-card-text>{{ groupOrItem.raw.calories }} cal</v-card-text> |
| 42 | + </v-card> |
| 43 | + </v-col> |
| 44 | + </template> |
| 45 | + </v-row> |
| 46 | + </template> |
| 47 | + </v-data-iterator> |
| 48 | + </div> |
| 49 | +</template> |
| 50 | + |
| 51 | +<script setup> |
| 52 | + import { ref } from 'vue' |
| 53 | +
|
| 54 | + const opened = ref([]) |
| 55 | + const openAll = ref(false) |
| 56 | +
|
| 57 | + const items = [ |
| 58 | + { name: 'Frozen Yogurt', calories: 159, category: 'Dairy', origin: 'USA' }, |
| 59 | + { name: 'Ice cream sandwich', calories: 237, category: 'Dairy', origin: 'USA' }, |
| 60 | + { name: 'Cheese', calories: 402, category: 'Dairy', origin: 'France' }, |
| 61 | + { name: 'Butter', calories: 717, category: 'Dairy', origin: 'France' }, |
| 62 | + { name: 'Eclair', calories: 262, category: 'Pastry', origin: 'France' }, |
| 63 | + { name: 'Cupcake', calories: 305, category: 'Pastry', origin: 'USA' }, |
| 64 | + { name: 'Croissant', calories: 231, category: 'Pastry', origin: 'France' }, |
| 65 | + { name: 'Baklava', calories: 334, category: 'Pastry', origin: 'Turkey' }, |
| 66 | + { name: 'Oreo', calories: 160, category: 'Cookie', origin: 'USA' }, |
| 67 | + { name: 'Macaron', calories: 404, category: 'Cookie', origin: 'France' }, |
| 68 | + { name: 'Biscotti', calories: 410, category: 'Cookie', origin: 'Italy' }, |
| 69 | + { name: 'KitKat', calories: 518, category: 'Candy', origin: 'UK' }, |
| 70 | + { name: 'Snickers', calories: 488, category: 'Candy', origin: 'USA' }, |
| 71 | + { name: 'Haribo', calories: 340, category: 'Candy', origin: 'Germany' }, |
| 72 | + ] |
| 73 | +</script> |
| 74 | + |
| 75 | +<script> |
| 76 | + export default { |
| 77 | + data: () => ({ |
| 78 | + opened: [], |
| 79 | + openAll: false, |
| 80 | + items: [ |
| 81 | + { name: 'Frozen Yogurt', calories: 159, category: 'Dairy', origin: 'USA' }, |
| 82 | + { name: 'Ice cream sandwich', calories: 237, category: 'Dairy', origin: 'USA' }, |
| 83 | + { name: 'Cheese', calories: 402, category: 'Dairy', origin: 'France' }, |
| 84 | + { name: 'Butter', calories: 717, category: 'Dairy', origin: 'France' }, |
| 85 | + { name: 'Eclair', calories: 262, category: 'Pastry', origin: 'France' }, |
| 86 | + { name: 'Cupcake', calories: 305, category: 'Pastry', origin: 'USA' }, |
| 87 | + { name: 'Croissant', calories: 231, category: 'Pastry', origin: 'France' }, |
| 88 | + { name: 'Baklava', calories: 334, category: 'Pastry', origin: 'Turkey' }, |
| 89 | + { name: 'Oreo', calories: 160, category: 'Cookie', origin: 'USA' }, |
| 90 | + { name: 'Macaron', calories: 404, category: 'Cookie', origin: 'France' }, |
| 91 | + { name: 'Biscotti', calories: 410, category: 'Cookie', origin: 'Italy' }, |
| 92 | + { name: 'KitKat', calories: 518, category: 'Candy', origin: 'UK' }, |
| 93 | + { name: 'Snickers', calories: 488, category: 'Candy', origin: 'USA' }, |
| 94 | + { name: 'Haribo', calories: 340, category: 'Candy', origin: 'Germany' }, |
| 95 | + ], |
| 96 | + }), |
| 97 | + methods: { |
| 98 | + groupKey ({ value }) { |
| 99 | + return value |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +</script> |
0 commit comments