有两种触发子菜单的方式

    只需为 Cascader 的属性指定选项数组即可渲染出一个级联选择器。通过expand-trigger可以定义展开子级菜单的触发方式。本例还展示了change事件,它的参数为 Cascader 的绑定值:一个由各级菜单的值所组成的数组。

    禁用选项

    通过在数据源中设置 disabled 字段来声明该选项是禁用的

    Cascader 级联选择器 - 图2

    本例中,options指定的数组中的第一个元素含有disabled: true键值对,因此是禁用的。在默认情况下,Cascader 会检查数据中每一项的disabled字段是否为true,如果你的数据中表示禁用含义的字段名不为disabled,可以通过props属性来指定(详见下方 API 表格)。当然,valuelabelchildren这三个字段名也可以通过同样的方式指定。

    1. <el-cascader
    2. :options="optionsWithDisabled"
    3. ></el-cascader>
    4. <script>
    5. export default {
    6. data() {
    7. return {
    8. optionsWithDisabled: [{
    9. value: 'zhinan',
    10. label: '指南',
    11. disabled: true,
    12. children: [{
    13. value: 'shejiyuanze',
    14. label: '设计原则',
    15. children: [{
    16. value: 'yizhi',
    17. label: '一致'
    18. }, {
    19. value: 'fankui',
    20. label: '反馈'
    21. }, {
    22. value: 'xiaolv',
    23. label: '效率'
    24. }, {
    25. value: 'kekong',
    26. label: '可控'
    27. }]
    28. }, {
    29. value: 'daohang',
    30. label: '导航',
    31. children: [{
    32. value: 'cexiangdaohang',
    33. label: '侧向导航'
    34. }, {
    35. value: 'dingbudaohang',
    36. label: '顶部导航'
    37. }]
    38. }]
    39. }, {
    40. value: 'zujian',
    41. label: '组件',
    42. children: [{
    43. value: 'basic',
    44. label: 'Basic',
    45. children: [{
    46. value: 'layout',
    47. label: 'Layout 布局'
    48. }, {
    49. value: 'color',
    50. label: 'Color 色彩'
    51. }, {
    52. value: 'typography',
    53. label: 'Typography 字体'
    54. }, {
    55. value: 'icon',
    56. label: 'Icon 图标'
    57. }, {
    58. value: 'button',
    59. label: 'Button 按钮'
    60. }]
    61. }, {
    62. value: 'form',
    63. label: 'Form',
    64. children: [{
    65. value: 'radio',
    66. label: 'Radio 单选框'
    67. }, {
    68. value: 'checkbox',
    69. label: 'Checkbox 多选框'
    70. }, {
    71. value: 'input',
    72. label: 'Input 输入框'
    73. }, {
    74. value: 'input-number',
    75. label: 'InputNumber 计数器'
    76. }, {
    77. value: 'select',
    78. label: 'Select 选择器'
    79. }, {
    80. value: 'cascader',
    81. label: 'Cascader 级联选择器'
    82. }, {
    83. value: 'switch',
    84. label: 'Switch 开关'
    85. }, {
    86. value: 'slider',
    87. label: 'Slider 滑块'
    88. }, {
    89. value: 'time-picker',
    90. label: 'TimePicker 时间选择器'
    91. }, {
    92. value: 'date-picker',
    93. label: 'DatePicker 日期选择器'
    94. }, {
    95. value: 'datetime-picker',
    96. label: 'DateTimePicker 日期时间选择器'
    97. }, {
    98. value: 'upload',
    99. label: 'Upload 上传'
    100. }, {
    101. value: 'rate',
    102. label: 'Rate 评分'
    103. }, {
    104. value: 'form',
    105. label: 'Form 表单'
    106. }]
    107. }, {
    108. value: 'data',
    109. label: 'Data',
    110. children: [{
    111. value: 'table',
    112. label: 'Table 表格'
    113. }, {
    114. value: 'tag',
    115. label: 'Tag 标签'
    116. }, {
    117. value: 'progress',
    118. label: 'Progress 进度条'
    119. }, {
    120. value: 'tree',
    121. label: 'Tree 树形控件'
    122. }, {
    123. value: 'pagination',
    124. label: 'Pagination 分页'
    125. }, {
    126. value: 'badge',
    127. label: 'Badge 标记'
    128. }]
    129. }, {
    130. value: 'notice',
    131. label: 'Notice',
    132. children: [{
    133. value: 'alert',
    134. label: 'Alert 警告'
    135. }, {
    136. value: 'loading',
    137. label: 'Loading 加载'
    138. }, {
    139. value: 'message',
    140. label: 'Message 消息提示'
    141. }, {
    142. value: 'message-box',
    143. label: 'MessageBox 弹框'
    144. }, {
    145. value: 'notification',
    146. }]
    147. value: 'navigation',
    148. label: 'Navigation',
    149. children: [{
    150. value: 'menu',
    151. label: 'NavMenu 导航菜单'
    152. }, {
    153. value: 'tabs',
    154. label: 'Tabs 标签页'
    155. }, {
    156. value: 'breadcrumb',
    157. label: 'Breadcrumb 面包屑'
    158. }, {
    159. value: 'dropdown',
    160. label: 'Dropdown 下拉菜单'
    161. }, {
    162. value: 'steps',
    163. label: 'Steps 步骤条'
    164. }]
    165. }, {
    166. value: 'others',
    167. label: 'Others',
    168. children: [{
    169. value: 'dialog',
    170. label: 'Dialog 对话框'
    171. }, {
    172. value: 'tooltip',
    173. label: 'Tooltip 文字提示'
    174. }, {
    175. value: 'popover',
    176. label: 'Popover 弹出框'
    177. }, {
    178. value: 'card',
    179. label: 'Card 卡片'
    180. }, {
    181. value: 'carousel',
    182. label: 'Carousel 走马灯'
    183. }, {
    184. value: 'collapse',
    185. label: 'Collapse 折叠面板'
    186. }]
    187. }]
    188. }, {
    189. value: 'ziyuan',
    190. label: '资源',
    191. children: [{
    192. value: 'axure',
    193. label: 'Axure Components'
    194. }, {
    195. value: 'sketch',
    196. label: 'Sketch Templates'
    197. }, {
    198. value: 'jiaohu',
    199. label: '组件交互文档'
    200. }]
    201. }]
    202. };
    203. }
    204. };
    205. </script>

    仅显示最后一级

    属性show-all-levels定义了是否显示完整的路径,将其赋值为false则仅显示最后一级

    Cascader 级联选择器 - 图4

    默认值通过数组的方式指定。

    1. <el-cascader
    2. :options="options"
    3. v-model="selectedOptions"
    4. ></el-cascader>
    5. <script>
    6. export default {
    7. data() {
    8. return {
    9. options: [{
    10. value: 'zhinan',
    11. label: '指南',
    12. children: [{
    13. value: 'shejiyuanze',
    14. label: '设计原则',
    15. children: [{
    16. value: 'yizhi',
    17. label: '一致'
    18. }, {
    19. value: 'fankui',
    20. label: '反馈'
    21. }, {
    22. value: 'xiaolv',
    23. label: '效率'
    24. }, {
    25. value: 'kekong',
    26. label: '可控'
    27. }]
    28. }, {
    29. value: 'daohang',
    30. label: '导航',
    31. children: [{
    32. value: 'cexiangdaohang',
    33. label: '侧向导航'
    34. }, {
    35. value: 'dingbudaohang',
    36. label: '顶部导航'
    37. }]
    38. }]
    39. }, {
    40. value: 'zujian',
    41. label: '组件',
    42. children: [{
    43. value: 'basic',
    44. label: 'Basic',
    45. children: [{
    46. value: 'layout',
    47. label: 'Layout 布局'
    48. }, {
    49. value: 'color',
    50. label: 'Color 色彩'
    51. }, {
    52. value: 'typography',
    53. label: 'Typography 字体'
    54. }, {
    55. value: 'icon',
    56. label: 'Icon 图标'
    57. }, {
    58. value: 'button',
    59. label: 'Button 按钮'
    60. }]
    61. }, {
    62. value: 'form',
    63. label: 'Form',
    64. children: [{
    65. value: 'radio',
    66. label: 'Radio 单选框'
    67. }, {
    68. value: 'checkbox',
    69. label: 'Checkbox 多选框'
    70. }, {
    71. value: 'input',
    72. label: 'Input 输入框'
    73. }, {
    74. value: 'input-number',
    75. label: 'InputNumber 计数器'
    76. }, {
    77. value: 'select',
    78. label: 'Select 选择器'
    79. }, {
    80. value: 'cascader',
    81. label: 'Cascader 级联选择器'
    82. }, {
    83. value: 'switch',
    84. label: 'Switch 开关'
    85. }, {
    86. value: 'slider',
    87. label: 'Slider 滑块'
    88. }, {
    89. value: 'time-picker',
    90. label: 'TimePicker 时间选择器'
    91. }, {
    92. value: 'date-picker',
    93. label: 'DatePicker 日期选择器'
    94. }, {
    95. label: 'DateTimePicker 日期时间选择器'
    96. }, {
    97. value: 'upload',
    98. }, {
    99. value: 'rate',
    100. label: 'Rate 评分'
    101. }, {
    102. value: 'form',
    103. label: 'Form 表单'
    104. }]
    105. }, {
    106. value: 'data',
    107. label: 'Data',
    108. children: [{
    109. value: 'table',
    110. label: 'Table 表格'
    111. }, {
    112. value: 'tag',
    113. label: 'Tag 标签'
    114. }, {
    115. value: 'progress',
    116. label: 'Progress 进度条'
    117. }, {
    118. value: 'tree',
    119. label: 'Tree 树形控件'
    120. }, {
    121. value: 'pagination',
    122. label: 'Pagination 分页'
    123. }, {
    124. value: 'badge',
    125. label: 'Badge 标记'
    126. }]
    127. }, {
    128. value: 'notice',
    129. label: 'Notice',
    130. children: [{
    131. value: 'alert',
    132. label: 'Alert 警告'
    133. }, {
    134. value: 'loading',
    135. label: 'Loading 加载'
    136. }, {
    137. value: 'message',
    138. label: 'Message 消息提示'
    139. }, {
    140. value: 'message-box',
    141. label: 'MessageBox 弹框'
    142. }, {
    143. value: 'notification',
    144. label: 'Notification 通知'
    145. }]
    146. }, {
    147. value: 'navigation',
    148. label: 'Navigation',
    149. children: [{
    150. value: 'menu',
    151. label: 'NavMenu 导航菜单'
    152. }, {
    153. value: 'tabs',
    154. label: 'Tabs 标签页'
    155. }, {
    156. value: 'breadcrumb',
    157. label: 'Breadcrumb 面包屑'
    158. }, {
    159. value: 'dropdown',
    160. label: 'Dropdown 下拉菜单'
    161. }, {
    162. value: 'steps',
    163. label: 'Steps 步骤条'
    164. }]
    165. }, {
    166. value: 'others',
    167. label: 'Others',
    168. children: [{
    169. value: 'dialog',
    170. label: 'Dialog 对话框'
    171. }, {
    172. value: 'tooltip',
    173. label: 'Tooltip 文字提示'
    174. }, {
    175. value: 'popover',
    176. label: 'Popover 弹出框'
    177. }, {
    178. value: 'card',
    179. label: 'Card 卡片'
    180. }, {
    181. value: 'carousel',
    182. label: 'Carousel 走马灯'
    183. }, {
    184. value: 'collapse',
    185. label: 'Collapse 折叠面板'
    186. }]
    187. }]
    188. }, {
    189. value: 'ziyuan',
    190. label: '资源',
    191. children: [{
    192. value: 'axure',
    193. label: 'Axure Components'
    194. }, {
    195. value: 'sketch',
    196. label: 'Sketch Templates'
    197. }, {
    198. value: 'jiaohu',
    199. label: '组件交互文档'
    200. }]
    201. }],
    202. selectedOptions: ['zujian', 'data', 'tag']
    203. };
    204. }
    205. };
    206. </script>

    选择即改变

    点击或移入选项即表示选中该项,可用于选择任意一级菜单的选项。

    动态加载次级选项

    当选中某一级时,动态加载该级下的选项。

    Cascader 级联选择器 - 图6

    本例的选项数据源在初始化时不包含城市数据。利用active-item-change事件,可以在用户点击某个省份时拉取该省份下的城市数据。此外,本例还展示了props属性的用法。

    1. <el-cascader
    2. :options="options"
    3. @active-item-change="handleItemChange"
    4. :props="props"
    5. ></el-cascader>
    6. <script>
    7. export default {
    8. data() {
    9. return {
    10. options: [{
    11. label: '江苏',
    12. cities: []
    13. }, {
    14. label: '浙江',
    15. cities: []
    16. }],
    17. props: {
    18. value: 'label',
    19. children: 'cities'
    20. }
    21. };
    22. },
    23. methods: {
    24. handleItemChange(val) {
    25. console.log('active item:', val);
    26. setTimeout(_ => {
    27. if (val.indexOf('江苏') > -1 && !this.options2[0].cities.length) {
    28. this.options2[0].cities = [{
    29. label: '南京'
    30. }];
    31. } else if (val.indexOf('浙江') > -1 && !this.options2[1].cities.length) {
    32. this.options2[1].cities = [{
    33. label: '杭州'
    34. }];
    35. }
    36. }, 300);
    37. }
    38. }
    39. };
    40. </script>

    可以快捷地搜索选项并选择。

    filterable赋值为true即可打开搜索功能。

    Attributes

    props