项目组

v-item-group 提供从任何组件创建一组可选项的能力。这是组件的基础功能,如 v-tabsv-carousel

用例

v-item-group 的核心用法是创建应由 model 控制的任何事物的组。

template


  1. <template>
  2. <v-item-group>
  3. <v-container>
  4. <v-row>
  5. <v-col
  6. v-for="n in 3"
  7. :key="n"
  8. cols="12"
  9. md="4"
  10. >
  11. <v-item v-slot:default="{ active, toggle }">
  12. <v-card
  13. :color="active ? 'primary' : ''"
  14. class="d-flex align-center"
  15. dark
  16. height="200"
  17. @click="toggle"
  18. >
  19. <v-scroll-y-transition>
  20. <div
  21. v-if="active"
  22. class="display-3 flex-grow-1 text-center"
  23. >
  24. Active
  25. </div>
  26. </v-scroll-y-transition>
  27. </v-card>
  28. </v-item>
  29. </v-col>
  30. </v-row>
  31. </v-container>
  32. </v-item-group>
  33. </template>

Item groups(项目组) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Item groups(项目组) - 图2

实战场

template script


  1. <template>
  2. <v-container>
  3. <v-row justify="space-around">
  4. <v-switch
  5. v-model="mandatory"
  6. label="Mandatory"
  7. ></v-switch>
  8. <v-switch
  9. v-model="multiple"
  10. label="Multiple"
  11. ></v-switch>
  12. <v-col cols="12">
  13. <v-select
  14. v-model="type"
  15. :items="types"
  16. label="Item type"
  17. ></v-select>
  18. </v-col>
  19. </v-row>
  20. <v-item-group
  21. v-model="selected"
  22. :mandatory="mandatory"
  23. :multiple="multiple"
  24. >
  25. <v-container class="pa-0">
  26. <v-row>
  27. <v-col
  28. v-for="n in 3"
  29. :key="n"
  30. cols="12"
  31. md="4"
  32. >
  33. <v-item v-slot:default="{ active, toggle }">
  34. <v-card
  35. v-if="type === 'cards'"
  36. :color="active ? 'primary' : ''"
  37. class="d-flex align-center"
  38. dark
  39. height="200"
  40. @click="toggle"
  41. >
  42. <v-scroll-y-transition>
  43. <div
  44. v-if="active"
  45. class="display-3 flex-grow-1 text-center"
  46. >
  47. Active
  48. </div>
  49. </v-scroll-y-transition>
  50. </v-card>
  51. <v-img
  52. v-else
  53. src="https://picsum.photos/id/237/200/300"
  54. height="150"
  55. class="text-right pa-2"
  56. @click="toggle"
  57. >
  58. <v-btn
  59. icon
  60. dark
  61. >
  62. <v-icon>
  63. {{ active ? 'mdi-heart' : 'mdi-heart-outline' }}
  64. </v-icon>
  65. </v-btn>
  66. </v-img>
  67. </v-item>
  68. </v-col>
  69. </v-row>
  70. </v-container>
  71. </v-item-group>
  72. </v-container>
  73. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. mandatory: false,
  5. multiple: true,
  6. selected: null,
  7. types: [
  8. 'cards',
  9. 'images',
  10. ],
  11. type: 'cards',
  12. }),
  13. watch: {
  14. multiple (val) {
  15. this.selected = (val)
  16. ? this.selected >= 0 ? [this.selected] : []
  17. : this.selected.pop()
  18. },
  19. },
  20. }
  21. </script>

Item groups(项目组) - 图3

示例

下面是一些简单到复杂的例子。

多选

项目组可以选择多个项目。

template


  1. <template>
  2. <v-item-group multiple>
  3. <v-container>
  4. <v-row>
  5. <v-col
  6. v-for="n in 3"
  7. :key="n"
  8. cols="12"
  9. md="4"
  10. >
  11. <v-item v-slot:default="{ active, toggle }">
  12. <v-card
  13. :color="active ? 'primary' : ''"
  14. class="d-flex align-center"
  15. dark
  16. height="200"
  17. @click="toggle"
  18. >
  19. <v-scroll-y-transition>
  20. <div
  21. v-if="active"
  22. class="display-3 flex-grow-1 text-center"
  23. >
  24. Active
  25. </div>
  26. </v-scroll-y-transition>
  27. </v-card>
  28. </v-item>
  29. </v-col>
  30. </v-row>
  31. </v-container>
  32. </v-item-group>
  33. </template>

Item groups(项目组) - 图4

必填项

mandatory 项目组必须至少选择 1 个项目。

template


  1. <template>
  2. <v-item-group mandatory>
  3. <v-container>
  4. <v-row>
  5. <v-col
  6. v-for="n in 3"
  7. :key="n"
  8. cols="12"
  9. md="4"
  10. >
  11. <v-item v-slot:default="{ active, toggle }">
  12. <v-card
  13. :color="active ? 'primary' : ''"
  14. class="d-flex align-center"
  15. dark
  16. height="200"
  17. @click="toggle"
  18. >
  19. <v-scroll-y-transition>
  20. <div
  21. v-if="active"
  22. class="display-3 flex-grow-1 text-center"
  23. >
  24. Active
  25. </div>
  26. </v-scroll-y-transition>
  27. </v-card>
  28. </v-item>
  29. </v-col>
  30. </v-row>
  31. </v-container>
  32. </v-item-group>
  33. </template>

Item groups(项目组) - 图5

带有活动类

activeClass 属性允许您在活动项目上设置自定义 CSS 类。

template


  1. <template>
  2. <v-item-group active-class="primary">
  3. <v-container>
  4. <v-row>
  5. <v-col
  6. v-for="n in 3"
  7. :key="n"
  8. cols="12"
  9. md="4"
  10. >
  11. <v-item v-slot:default="{ active, toggle }">
  12. <v-card
  13. class="d-flex align-center"
  14. dark
  15. height="200"
  16. @click="toggle"
  17. >
  18. <v-scroll-y-transition>
  19. <div
  20. v-if="active"
  21. class="display-3 flex-grow-1 text-center"
  22. >
  23. Active
  24. </div>
  25. </v-scroll-y-transition>
  26. </v-card>
  27. </v-item>
  28. </v-col>
  29. </v-row>
  30. </v-container>
  31. </v-item-group>
  32. </template>

Item groups(项目组) - 图6

Freelancer Free

A Free single page Vuetify theme for new developers.

ads by Vuetify

](https://store.vuetifyjs.com/product/freelancer-theme-free?ref=vuetifyjs.com)

自定义组

当图标允许选择或取消选择单个选项(例如将项目标记为收藏)时,它们可以用作切换按钮。

template script


  1. <template>
  2. <v-card
  3. max-width="400"
  4. class="mx-auto"
  5. >
  6. <v-container class="pa-1">
  7. <v-item-group
  8. v-model="selected"
  9. multiple
  10. >
  11. <v-row>
  12. <v-col
  13. v-for="(item, i) in items"
  14. :key="i"
  15. cols="12"
  16. md="6"
  17. >
  18. <v-item v-slot:default="{ active, toggle }">
  19. <v-img
  20. :src="`https://cdn.vuetifyjs.com/images/${item.src}`"
  21. height="150"
  22. class="text-right pa-2"
  23. @click="toggle"
  24. >
  25. <v-btn
  26. icon
  27. dark
  28. >
  29. <v-icon>
  30. {{ active ? 'mdi-heart' : 'mdi-heart-outline' }}
  31. </v-icon>
  32. </v-btn>
  33. </v-img>
  34. </v-item>
  35. </v-col>
  36. </v-row>
  37. </v-item-group>
  38. </v-container>
  39. </v-card>
  40. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. items: [
  5. {
  6. src: 'backgrounds/bg.jpg',
  7. },
  8. {
  9. src: 'backgrounds/md.jpg',
  10. },
  11. {
  12. src: 'backgrounds/bg-2.jpg',
  13. },
  14. {
  15. src: 'backgrounds/md2.jpg',
  16. },
  17. ],
  18. selected: [],
  19. }),
  20. }
  21. </script>

Item groups(项目组) - 图7

纸片

轻松绑定自定义纸片组。

template script


  1. <template>
  2. <v-card>
  3. <v-toolbar
  4. flat
  5. color="blue-grey"
  6. dark
  7. >
  8. <v-toolbar-title>Submit a post</v-toolbar-title>
  9. </v-toolbar>
  10. <v-card-text>
  11. <v-text-field filled label="Title" value="My new post"></v-text-field>
  12. <v-textarea
  13. filled
  14. label="Text"
  15. value="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
  16. ></v-textarea>
  17. <v-divider class="my-2"></v-divider>
  18. <v-item-group multiple>
  19. <v-subheader>Tags</v-subheader>
  20. <v-item
  21. v-for="n in 8"
  22. :key="n"
  23. v-slot:default="{ active, toggle }"
  24. >
  25. <v-chip
  26. active-class="purple--text"
  27. :input-value="active"
  28. @click="toggle"
  29. >
  30. Tag {{ n }}
  31. </v-chip>
  32. </v-item>
  33. </v-item-group>
  34. </v-card-text>
  35. <v-divider></v-divider>
  36. <v-card-actions>
  37. <v-spacer></v-spacer>
  38. <v-btn
  39. color="success"
  40. depressed
  41. >
  42. Post
  43. </v-btn>
  44. </v-card-actions>
  45. </v-card>
  46. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. //
  5. }),
  6. }
  7. </script>

Item groups(项目组) - 图8