按钮组

v-btn-toggle 组件是专门针对 v-btn 构建的 v-item-group 的简单包装器。

用例

切换按钮允许您创建一个样式化的按钮组,可以在单个 v-model 下选择或切换

template script


  1. <template>
  2. <v-container fluid>
  3. <v-row>
  4. <v-col
  5. cols="12"
  6. sm="6"
  7. class="py-2"
  8. >
  9. <p>Exclusive</p>
  10. <v-btn-toggle v-model="toggle_exclusive">
  11. <v-btn>
  12. <v-icon>mdi-format-align-left</v-icon>
  13. </v-btn>
  14. <v-btn>
  15. <v-icon>mdi-format-align-center</v-icon>
  16. </v-btn>
  17. <v-btn>
  18. <v-icon>mdi-format-align-right</v-icon>
  19. </v-btn>
  20. <v-btn>
  21. <v-icon>mdi-format-align-justify</v-icon>
  22. </v-btn>
  23. </v-btn-toggle>
  24. </v-col>
  25. <v-col
  26. cols="12"
  27. sm="6"
  28. class="py-2"
  29. >
  30. <p>Multiple</p>
  31. <v-btn-toggle
  32. v-model="toggle_multiple"
  33. dense
  34. background-color="primary"
  35. dark
  36. multiple
  37. >
  38. <v-btn>
  39. <v-icon>mdi-format-bold</v-icon>
  40. </v-btn>
  41. <v-btn>
  42. <v-icon>mdi-format-italic</v-icon>
  43. </v-btn>
  44. <v-btn>
  45. <v-icon>mdi-format-underline</v-icon>
  46. </v-btn>
  47. <v-btn>
  48. <v-icon>mdi-format-color-fill</v-icon>
  49. </v-btn>
  50. </v-btn-toggle>
  51. </v-col>
  52. <v-col
  53. cols="12"
  54. sm="6"
  55. class="py-2"
  56. >
  57. <p>No Options Selected</p>
  58. <v-btn-toggle v-model="toggle_none">
  59. <v-btn>
  60. <v-icon>mdi-format-align-left</v-icon>
  61. </v-btn>
  62. <v-btn>
  63. <v-icon>mdi-format-align-center</v-icon>
  64. </v-btn>
  65. <v-btn>
  66. <v-icon>mdi-format-align-right</v-icon>
  67. </v-btn>
  68. <v-btn>
  69. <v-icon>mdi-format-align-justify</v-icon>
  70. </v-btn>
  71. </v-btn-toggle>
  72. </v-col>
  73. <v-col
  74. cols="12"
  75. sm="6"
  76. class="py-2"
  77. >
  78. <p>Mandatory</p>
  79. <v-btn-toggle
  80. v-model="toggle_one"
  81. shaped
  82. mandatory
  83. >
  84. <v-btn>
  85. <v-icon>mdi-format-align-left</v-icon>
  86. </v-btn>
  87. <v-btn>
  88. <v-icon>mdi-format-align-center</v-icon>
  89. </v-btn>
  90. <v-btn>
  91. <v-icon>mdi-format-align-right</v-icon>
  92. </v-btn>
  93. <v-btn>
  94. <v-icon>mdi-format-align-justify</v-icon>
  95. </v-btn>
  96. </v-btn-toggle>
  97. </v-col>
  98. <v-col
  99. cols="12"
  100. sm="6"
  101. class="py-2"
  102. >
  103. <p>Text Options</p>
  104. <v-btn-toggle
  105. v-model="text"
  106. tile
  107. color="deep-purple accent-3"
  108. group
  109. >
  110. <v-btn value="left">
  111. Left
  112. </v-btn>
  113. <v-btn value="center">
  114. Center
  115. </v-btn>
  116. <v-btn value="right">
  117. Right
  118. </v-btn>
  119. <v-btn value="justify">
  120. Justify
  121. </v-btn>
  122. </v-btn-toggle>
  123. </v-col>
  124. <v-col
  125. cols="12"
  126. sm="6"
  127. class="py-2"
  128. >
  129. <p>Text &amp; Icon Options</p>
  130. <v-btn-toggle
  131. v-model="icon"
  132. borderless
  133. >
  134. <v-btn value="left">
  135. <span class="hidden-sm-and-down">Left</span>
  136. <v-icon right>mdi-format-align-left</v-icon>
  137. </v-btn>
  138. <v-btn value="center">
  139. <span class="hidden-sm-and-down">Center</span>
  140. <v-icon right>mdi-format-align-center</v-icon>
  141. </v-btn>
  142. <v-btn value="right">
  143. <span class="hidden-sm-and-down">Right</span>
  144. <v-icon right>mdi-format-align-right</v-icon>
  145. </v-btn>
  146. <v-btn value="justify">
  147. <span class="hidden-sm-and-down">Justify</span>
  148. <v-icon right>mdi-format-align-justify</v-icon>
  149. </v-btn>
  150. </v-btn-toggle>
  151. </v-col>
  152. </v-row>
  153. </v-container>
  154. </template>
  1. <script>
  2. export default {
  3. data () {
  4. return {
  5. text: 'center',
  6. icon: 'justify',
  7. toggle_none: null,
  8. toggle_one: 0,
  9. toggle_exclusive: 2,
  10. toggle_multiple: [0, 1, 2],
  11. }
  12. },
  13. }
  14. </script>

Button groups(按钮组) - 图1

API

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

Button groups(按钮组) - 图2

示例

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

圆角按钮

你能够使用 rounded 属性让 VBtnToggle 变成圆角。

template script


  1. <template>
  2. <v-card
  3. flat
  4. class="py-12"
  5. >
  6. <v-card-text>
  7. <v-row
  8. align="center"
  9. justify="center"
  10. >
  11. <v-col cols="12">
  12. <p class="text-center">Rounded</p>
  13. </v-col>
  14. <v-btn-toggle
  15. v-model="toggle_exclusive"
  16. rounded
  17. >
  18. <v-btn>
  19. <v-icon>mdi-format-align-left</v-icon>
  20. </v-btn>
  21. <v-btn>
  22. <v-icon>mdi-format-align-center</v-icon>
  23. </v-btn>
  24. <v-btn>
  25. <v-icon>mdi-format-align-right</v-icon>
  26. </v-btn>
  27. <v-btn>
  28. <v-icon>mdi-format-align-justify</v-icon>
  29. </v-btn>
  30. </v-btn-toggle>
  31. </v-row>
  32. </v-card-text>
  33. </v-card>
  34. </template>
  1. <script>
  2. export default {
  3. data () {
  4. return {
  5. toggle_exclusive: undefined,
  6. }
  7. },
  8. }
  9. </script>

Button groups(按钮组) - 图3

必填项

mandatory VBtnToggle 总是要有值

template script


  1. <template>
  2. <v-card
  3. flat
  4. class="py-12"
  5. >
  6. <v-card-text>
  7. <v-row
  8. align="center"
  9. justify="center"
  10. >
  11. <v-col cols="12">
  12. <p class="text-center">Mandatory</p>
  13. </v-col>
  14. <v-btn-toggle
  15. v-model="toggle_exclusive"
  16. mandatory
  17. >
  18. <v-btn>
  19. <v-icon>mdi-format-align-left</v-icon>
  20. </v-btn>
  21. <v-btn>
  22. <v-icon>mdi-format-align-center</v-icon>
  23. </v-btn>
  24. <v-btn>
  25. <v-icon>mdi-format-align-right</v-icon>
  26. </v-btn>
  27. <v-btn>
  28. <v-icon>mdi-format-align-justify</v-icon>
  29. </v-btn>
  30. </v-btn-toggle>
  31. </v-row>
  32. </v-card-text>
  33. </v-card>
  34. </template>
  1. <script>
  2. export default {
  3. data () {
  4. return {
  5. toggle_exclusive: undefined,
  6. }
  7. },
  8. }
  9. </script>

Button groups(按钮组) - 图4

多选

multiple VBtnToggle 允许用户选择多个变量并返回数组值。

template script


  1. <template>
  2. <v-card
  3. flat
  4. class="py-12"
  5. >
  6. <v-card-text>
  7. <v-row
  8. align="center"
  9. justify="center"
  10. >
  11. <v-col cols="12">
  12. <p class="text-center">Multiple</p>
  13. </v-col>
  14. <v-btn-toggle
  15. v-model="toggle_exclusive"
  16. multiple
  17. >
  18. <v-btn>
  19. <v-icon>mdi-format-align-left</v-icon>
  20. </v-btn>
  21. <v-btn>
  22. <v-icon>mdi-format-align-center</v-icon>
  23. </v-btn>
  24. <v-btn>
  25. <v-icon>mdi-format-align-right</v-icon>
  26. </v-btn>
  27. <v-btn>
  28. <v-icon>mdi-format-align-justify</v-icon>
  29. </v-btn>
  30. </v-btn-toggle>
  31. <v-col
  32. cols="12"
  33. class="text-center"
  34. >
  35. Model: {{ toggle_exclusive }}
  36. </v-col>
  37. </v-row>
  38. </v-card-text>
  39. </v-card>
  40. </template>
  1. <script>
  2. export default {
  3. data () {
  4. return {
  5. toggle_exclusive: [],
  6. }
  7. },
  8. }
  9. </script>

Button groups(按钮组) - 图5

Alpha Theme

Complete theme experience including enhanced Vue CLI, full documentation, 5 custom components and much more!

ads by Vuetify

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

在工具栏中

v-toolbar 轻松集成自定义按钮

template script


  1. <template>
  2. <v-toolbar dense>
  3. <v-overflow-btn
  4. :items="dropdown_font"
  5. label="Select font"
  6. hide-details
  7. class="pa-0"
  8. ></v-overflow-btn>
  9. <template v-if="$vuetify.breakpoint.mdAndUp">
  10. <v-divider vertical></v-divider>
  11. <v-overflow-btn
  12. :items="dropdown_edit"
  13. editable
  14. label="Select size"
  15. hide-details
  16. class="pa-0"
  17. overflow
  18. ></v-overflow-btn>
  19. <v-divider vertical></v-divider>
  20. <v-spacer></v-spacer>
  21. <v-btn-toggle
  22. v-model="toggle_multiple"
  23. color="primary"
  24. dense
  25. group
  26. multiple
  27. >
  28. <v-btn :value="1" text>
  29. <v-icon>mdi-format-bold</v-icon>
  30. </v-btn>
  31. <v-btn :value="2" text>
  32. <v-icon>mdi-format-italic</v-icon>
  33. </v-btn>
  34. <v-btn :value="3" text>
  35. <v-icon>mdi-format-underline</v-icon>
  36. </v-btn>
  37. <v-btn :value="4" text>
  38. <v-icon>mdi-format-color-fill</v-icon>
  39. </v-btn>
  40. </v-btn-toggle>
  41. <div class="mx-4"></div>
  42. <v-btn-toggle
  43. v-model="toggle_exclusive"
  44. color="primary"
  45. dense
  46. group
  47. >
  48. <v-btn :value="1" text>
  49. <v-icon>mdi-format-align-left</v-icon>
  50. </v-btn>
  51. <v-btn :value="2" text>
  52. <v-icon>mdi-format-align-center</v-icon>
  53. </v-btn>
  54. <v-btn :value="3" text>
  55. <v-icon>mdi-format-align-right</v-icon>
  56. </v-btn>
  57. <v-btn :value="4" text>
  58. <v-icon>mdi-format-align-justify</v-icon>
  59. </v-btn>
  60. </v-btn-toggle>
  61. </template>
  62. </v-toolbar>
  63. </template>
  1. <script>
  2. export default {
  3. data () {
  4. return {
  5. dropdown_font: [
  6. { text: 'Arial' },
  7. { text: 'Calibri' },
  8. { text: 'Courier' },
  9. { text: 'Verdana' },
  10. ],
  11. dropdown_edit: [
  12. { text: '100%' },
  13. { text: '75%' },
  14. { text: '50%' },
  15. { text: '25%' },
  16. { text: '0%' },
  17. ],
  18. toggle_exclusive: 2,
  19. toggle_multiple: [1, 2, 3],
  20. }
  21. },
  22. }
  23. </script>

Button groups(按钮组) - 图6

选中的操作

对类似的操作进行分组,并设计自己的 WYSIWYG 组件。

template script


  1. <template>
  2. <v-card
  3. max-width="400"
  4. class="mx-auto"
  5. >
  6. <v-textarea
  7. v-model="value"
  8. auto-grow
  9. full-width
  10. rows="2"
  11. ></v-textarea>
  12. <v-row
  13. class="px-2 pb-2 ma-0"
  14. justify="space-between"
  15. >
  16. <v-btn-toggle
  17. v-model="formatting"
  18. multiple
  19. >
  20. <v-btn color="white">
  21. <v-icon>mdi-format-italic</v-icon>
  22. </v-btn>
  23. <v-btn color="white">
  24. <v-icon>mdi-format-bold</v-icon>
  25. </v-btn>
  26. <v-btn color="white">
  27. <v-icon>mdi-format-underline</v-icon>
  28. </v-btn>
  29. <v-btn color="white">
  30. <v-row
  31. align="center"
  32. class="flex-column"
  33. justify="center"
  34. >
  35. <v-icon class="cols 12">mdi-format-color-text</v-icon>
  36. <v-sheet
  37. tile
  38. style="margin-top: -4px;"
  39. height="4"
  40. width="26"
  41. color="purple"
  42. ></v-sheet>
  43. </v-row>
  44. </v-btn>
  45. </v-btn-toggle>
  46. <v-btn-toggle v-model="alignment">
  47. <v-btn color="white">
  48. <v-icon>mdi-format-align-center</v-icon>
  49. </v-btn>
  50. <v-btn color="white">
  51. <v-icon>mdi-format-align-left</v-icon>
  52. </v-btn>
  53. <v-btn color="white">
  54. <v-icon>mdi-format-align-right</v-icon>
  55. </v-btn>
  56. </v-btn-toggle>
  57. </v-row>
  58. <v-sheet
  59. class="pa-4 text-center"
  60. color="grey lighten-3"
  61. tile
  62. >
  63. <v-row
  64. class="mb-2"
  65. dense
  66. >
  67. <v-col
  68. v-for="n in numbers"
  69. :key="n"
  70. class="caption grey--text text--darken-1"
  71. v-text="n"
  72. ></v-col>
  73. </v-row>
  74. <v-row dense>
  75. <v-col
  76. v-for="l in letters"
  77. :key="l"
  78. class="title grey--text font-weight-regular text--darken-2"
  79. v-text="l"
  80. ></v-col>
  81. </v-row>
  82. </v-sheet>
  83. </v-card>
  84. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. alignment: 1,
  5. formatting: [],
  6. numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  7. letters: 'qwertyuiop'.split(''),
  8. value: 'Toggle button requirements.\r\rHave at least three toggle buttons in a group\rLabel buttons with text, an icon, or',
  9. }),
  10. }
  11. </script>

Button groups(按钮组) - 图7