TreeSelect 树形选择

含有下拉菜单的树形选择器,结合了 el-treeel-select 两个组件的功能。

基础用法

树状选择器

TreeSelect 树形选择 2.1.8 - 图1

  1. <template>
  2. <el-tree-select v-model="value" :data="data" :render-after-expand="false" />
  3. <el-divider />
  4. show checkbox:
  5. <el-tree-select
  6. v-model="value"
  7. :data="data"
  8. :render-after-expand="false"
  9. show-checkbox
  10. />
  11. </template>
  12. <script lang="ts" setup>
  13. import { ref } from 'vue'
  14. const value = ref()
  15. const data = [
  16. {
  17. value: '1',
  18. label: 'Level one 1',
  19. children: [
  20. {
  21. value: '1-1',
  22. label: 'Level two 1-1',
  23. children: [
  24. {
  25. value: '1-1-1',
  26. label: 'Level three 1-1-1',
  27. },
  28. ],
  29. },
  30. ],
  31. },
  32. {
  33. value: '2',
  34. label: 'Level one 2',
  35. children: [
  36. {
  37. value: '2-1',
  38. label: 'Level two 2-1',
  39. children: [
  40. {
  41. value: '2-1-1',
  42. label: 'Level three 2-1-1',
  43. },
  44. ],
  45. },
  46. {
  47. value: '2-2',
  48. label: 'Level two 2-2',
  49. children: [
  50. {
  51. value: '2-2-1',
  52. label: 'Level three 2-2-1',
  53. },
  54. ],
  55. },
  56. ],
  57. },
  58. {
  59. value: '3',
  60. label: 'Level one 3',
  61. children: [
  62. {
  63. value: '3-1',
  64. label: 'Level two 3-1',
  65. children: [
  66. {
  67. value: '3-1-1',
  68. label: 'Level three 3-1-1',
  69. },
  70. ],
  71. },
  72. {
  73. value: '3-2',
  74. label: 'Level two 3-2',
  75. children: [
  76. {
  77. value: '3-2-1',
  78. label: 'Level three 3-2-1',
  79. },
  80. ],
  81. },
  82. ],
  83. },
  84. ]
  85. </script>

TIP

由于 render-after-expand 属性是默认开启的,所选择的标签名可能不会及时更新显示,您可以把该属性设置为 false 来显示正确的名称

选择任意级别

当属性 check-strictly=true 时,任何节点都可以被选择,否则只有子节点可被选择。

TreeSelect 树形选择 2.1.8 - 图2

  1. <template>
  2. <el-tree-select
  3. v-model="value"
  4. :data="data"
  5. check-strictly
  6. :render-after-expand="false"
  7. />
  8. <el-divider />
  9. show checkbox(only click checkbox to select):
  10. <el-tree-select
  11. v-model="value"
  12. :data="data"
  13. check-strictly
  14. :render-after-expand="false"
  15. show-checkbox
  16. />
  17. <el-divider />
  18. show checkbox with `check-on-click-node`:
  19. <el-tree-select
  20. v-model="value"
  21. :data="data"
  22. check-strictly
  23. :render-after-expand="false"
  24. show-checkbox
  25. check-on-click-node
  26. />
  27. </template>
  28. <script lang="ts" setup>
  29. import { ref } from 'vue'
  30. const value = ref()
  31. const data = [
  32. {
  33. value: '1',
  34. label: 'Level one 1',
  35. children: [
  36. {
  37. value: '1-1',
  38. label: 'Level two 1-1',
  39. children: [
  40. {
  41. value: '1-1-1',
  42. label: 'Level three 1-1-1',
  43. },
  44. ],
  45. },
  46. ],
  47. },
  48. {
  49. value: '2',
  50. label: 'Level one 2',
  51. children: [
  52. {
  53. value: '2-1',
  54. label: 'Level two 2-1',
  55. children: [
  56. {
  57. value: '2-1-1',
  58. label: 'Level three 2-1-1',
  59. },
  60. ],
  61. },
  62. {
  63. value: '2-2',
  64. label: 'Level two 2-2',
  65. children: [
  66. {
  67. value: '2-2-1',
  68. label: 'Level three 2-2-1',
  69. },
  70. ],
  71. },
  72. ],
  73. },
  74. {
  75. value: '3',
  76. label: 'Level one 3',
  77. children: [
  78. {
  79. value: '3-1',
  80. label: 'Level two 3-1',
  81. children: [
  82. {
  83. value: '3-1-1',
  84. label: 'Level three 3-1-1',
  85. },
  86. ],
  87. },
  88. {
  89. value: '3-2',
  90. label: 'Level two 3-2',
  91. children: [
  92. {
  93. value: '3-2-1',
  94. label: 'Level three 3-2-1',
  95. },
  96. ],
  97. },
  98. ],
  99. },
  100. ]
  101. </script>

TIP

当使用 show-checkbox时,由于 check-on-click-node 默认值是 false,这时候只能通过 checkbox 来选中,当然您也可以将其设置成 true,这样点击整个 node 都可以用来完成选择

多选

通过点击或复选框选择多个选项。

TreeSelect 树形选择 2.1.8 - 图3

  1. <template>
  2. <el-tree-select
  3. v-model="value"
  4. :data="data"
  5. multiple
  6. :render-after-expand="false"
  7. />
  8. <el-divider />
  9. show checkbox:
  10. <el-tree-select
  11. v-model="value"
  12. :data="data"
  13. multiple
  14. :render-after-expand="false"
  15. show-checkbox
  16. />
  17. <el-divider />
  18. show checkbox with `check-strictly`:
  19. <el-tree-select
  20. v-model="valueStrictly"
  21. :data="data"
  22. multiple
  23. :render-after-expand="false"
  24. show-checkbox
  25. check-strictly
  26. check-on-click-node
  27. />
  28. </template>
  29. <script lang="ts" setup>
  30. import { ref } from 'vue'
  31. const value = ref()
  32. const valueStrictly = ref()
  33. const data = [
  34. {
  35. value: '1',
  36. label: 'Level one 1',
  37. children: [
  38. {
  39. value: '1-1',
  40. label: 'Level two 1-1',
  41. children: [
  42. {
  43. value: '1-1-1',
  44. label: 'Level three 1-1-1',
  45. },
  46. ],
  47. },
  48. ],
  49. },
  50. {
  51. value: '2',
  52. label: 'Level one 2',
  53. children: [
  54. {
  55. value: '2-1',
  56. label: 'Level two 2-1',
  57. children: [
  58. {
  59. value: '2-1-1',
  60. label: 'Level three 2-1-1',
  61. },
  62. ],
  63. },
  64. {
  65. value: '2-2',
  66. label: 'Level two 2-2',
  67. children: [
  68. {
  69. value: '2-2-1',
  70. label: 'Level three 2-2-1',
  71. },
  72. ],
  73. },
  74. ],
  75. },
  76. {
  77. value: '3',
  78. label: 'Level one 3',
  79. children: [
  80. {
  81. value: '3-1',
  82. label: 'Level two 3-1',
  83. children: [
  84. {
  85. value: '3-1-1',
  86. label: 'Level three 3-1-1',
  87. },
  88. ],
  89. },
  90. {
  91. value: '3-2',
  92. label: 'Level two 3-2',
  93. children: [
  94. {
  95. value: '3-2-1',
  96. label: 'Level three 3-2-1',
  97. },
  98. ],
  99. },
  100. ],
  101. },
  102. ]
  103. </script>

禁用选项

使用 disabled 字段禁用选项。

TreeSelect 树形选择 2.1.8 - 图4

  1. <template>
  2. <el-tree-select v-model="value" :data="data" />
  3. </template>
  4. <script lang="ts" setup>
  5. import { ref } from 'vue'
  6. const value = ref()
  7. const data = [
  8. {
  9. value: '1',
  10. label: 'Level one 1',
  11. disabled: true,
  12. children: [
  13. {
  14. value: '1-1',
  15. label: 'Level two 1-1',
  16. disabled: true,
  17. children: [
  18. {
  19. disabled: true,
  20. value: '1-1-1',
  21. label: 'Level three 1-1-1',
  22. },
  23. ],
  24. },
  25. ],
  26. },
  27. {
  28. value: '2',
  29. label: 'Level one 2',
  30. children: [
  31. {
  32. value: '2-1',
  33. label: 'Level two 2-1',
  34. children: [
  35. {
  36. value: '2-1-1',
  37. label: 'Level three 2-1-1',
  38. },
  39. ],
  40. },
  41. {
  42. value: '2-2',
  43. label: 'Level two 2-2',
  44. children: [
  45. {
  46. value: '2-2-1',
  47. label: 'Level three 2-2-1',
  48. },
  49. ],
  50. },
  51. ],
  52. },
  53. {
  54. value: '3',
  55. label: 'Level one 3',
  56. children: [
  57. {
  58. value: '3-1',
  59. label: 'Level two 3-1',
  60. children: [
  61. {
  62. value: '3-1-1',
  63. label: 'Level three 3-1-1',
  64. },
  65. ],
  66. },
  67. {
  68. value: '3-2',
  69. label: 'Level two 3-2',
  70. children: [
  71. {
  72. value: '3-2-1',
  73. label: 'Level three 3-2-1',
  74. },
  75. ],
  76. },
  77. ],
  78. },
  79. ]
  80. </script>

可筛选

使用关键字筛选或自定义筛选方法。 filterMethod可以自定义数据筛选的方法, filterNodeMethod可以自定义节点数据筛选的方法。

TreeSelect 树形选择 2.1.8 - 图5

  1. <template>
  2. <el-tree-select v-model="value" :data="data" filterable />
  3. <el-divider />
  4. filter method:
  5. <el-tree-select
  6. v-model="value"
  7. :data="data"
  8. :filter-method="filterMethod"
  9. filterable
  10. />
  11. <el-divider />
  12. filter node method:
  13. <el-tree-select
  14. v-model="value"
  15. :data="data"
  16. :filter-node-method="filterNodeMethod"
  17. filterable
  18. />
  19. </template>
  20. <script lang="ts" setup>
  21. import { ref } from 'vue'
  22. const value = ref()
  23. const sourceData = [
  24. {
  25. value: '1',
  26. label: 'Level one 1',
  27. children: [
  28. {
  29. value: '1-1',
  30. label: 'Level two 1-1',
  31. children: [
  32. {
  33. value: '1-1-1',
  34. label: 'Level three 1-1-1',
  35. },
  36. ],
  37. },
  38. ],
  39. },
  40. {
  41. value: '2',
  42. label: 'Level one 2',
  43. children: [
  44. {
  45. value: '2-1',
  46. label: 'Level two 2-1',
  47. children: [
  48. {
  49. value: '2-1-1',
  50. label: 'Level three 2-1-1',
  51. },
  52. ],
  53. },
  54. {
  55. value: '2-2',
  56. label: 'Level two 2-2',
  57. children: [
  58. {
  59. value: '2-2-1',
  60. label: 'Level three 2-2-1',
  61. },
  62. ],
  63. },
  64. ],
  65. },
  66. {
  67. value: '3',
  68. label: 'Level one 3',
  69. children: [
  70. {
  71. value: '3-1',
  72. label: 'Level two 3-1',
  73. children: [
  74. {
  75. value: '3-1-1',
  76. label: 'Level three 3-1-1',
  77. },
  78. ],
  79. },
  80. {
  81. value: '3-2',
  82. label: 'Level two 3-2',
  83. children: [
  84. {
  85. value: '3-2-1',
  86. label: 'Level three 3-2-1',
  87. },
  88. ],
  89. },
  90. ],
  91. },
  92. ]
  93. const data = ref(sourceData)
  94. const filterMethod = (value) => {
  95. data.value = [...sourceData].filter((item) => item.label.includes(value))
  96. }
  97. const filterNodeMethod = (value, data) => data.label.includes(value)
  98. </script>

自定义内容

自定义树节点的内容。

TreeSelect 树形选择 2.1.8 - 图6

  1. <template>
  2. <el-tree-select v-model="value" :data="data">
  3. <template #default="{ data: { label } }">
  4. {{ label }}<span style="color: gray">(suffix)</span></template
  5. >
  6. </el-tree-select>
  7. <el-divider />
  8. use render content:
  9. <el-tree-select
  10. v-model="value"
  11. :data="data"
  12. :render-content="renderContent"
  13. />
  14. </template>
  15. <script lang="ts" setup>
  16. import { ref } from 'vue'
  17. const value = ref()
  18. const renderContent = (h, { data }) => {
  19. return h(
  20. 'span',
  21. {
  22. style: {
  23. color: '#626AEF',
  24. },
  25. },
  26. data.label
  27. )
  28. }
  29. const data = [
  30. {
  31. value: '1',
  32. label: 'Level one 1',
  33. children: [
  34. {
  35. value: '1-1',
  36. label: 'Level two 1-1',
  37. children: [
  38. {
  39. value: '1-1-1',
  40. label: 'Level three 1-1-1',
  41. },
  42. ],
  43. },
  44. ],
  45. },
  46. {
  47. value: '2',
  48. label: 'Level one 2',
  49. children: [
  50. {
  51. value: '2-1',
  52. label: 'Level two 2-1',
  53. children: [
  54. {
  55. value: '2-1-1',
  56. label: 'Level three 2-1-1',
  57. },
  58. ],
  59. },
  60. {
  61. value: '2-2',
  62. label: 'Level two 2-2',
  63. children: [
  64. {
  65. value: '2-2-1',
  66. label: 'Level three 2-2-1',
  67. },
  68. ],
  69. },
  70. ],
  71. },
  72. {
  73. value: '3',
  74. label: 'Level one 3',
  75. children: [
  76. {
  77. value: '3-1',
  78. label: 'Level two 3-1',
  79. children: [
  80. {
  81. value: '3-1-1',
  82. label: 'Level three 3-1-1',
  83. },
  84. ],
  85. },
  86. {
  87. value: '3-2',
  88. label: 'Level two 3-2',
  89. children: [
  90. {
  91. value: '3-2-1',
  92. label: 'Level three 3-2-1',
  93. },
  94. ],
  95. },
  96. ],
  97. },
  98. ]
  99. </script>

懒加载

树节点懒加载,更加适合于数据量大的列表。

TreeSelect 树形选择 2.1.8 - 图7

  1. <template>
  2. <el-tree-select v-model="value" lazy :load="load" :props="props" />
  3. </template>
  4. <script lang="ts" setup>
  5. import { ref } from 'vue'
  6. const value = ref()
  7. const props = {
  8. label: 'label',
  9. children: 'children',
  10. isLeaf: 'isLeaf',
  11. }
  12. let id = 0
  13. const load = (node, resolve) => {
  14. if (node.isLeaf) return resolve([])
  15. setTimeout(() => {
  16. resolve([
  17. {
  18. value: ++id,
  19. label: `lazy load node${id}`,
  20. },
  21. {
  22. value: ++id,
  23. label: `lazy load node${id}`,
  24. isLeaf: true,
  25. },
  26. ])
  27. }, 400)
  28. }
  29. </script>

TreeSelect 属性

由于这个组件是el-treeel-select的结合体,他们的原始属性未被更改,故不在此重复。请跳转查看原组件的相应文档。

属性方法事件插槽
treetreetreetree
selectselectselectselect

源代码

组件 TreeSelect 树形选择 2.1.8 - 图8 文档 TreeSelect 树形选择 2.1.8 - 图9

贡献者

TreeSelect 树形选择 2.1.8 - 图10 yujinpan

TreeSelect 树形选择 2.1.8 - 图11 Xc

TreeSelect 树形选择 2.1.8 - 图12 三咲智子

TreeSelect 树形选择 2.1.8 - 图13 zz

TreeSelect 树形选择 2.1.8 - 图14 opengraphica

TreeSelect 树形选择 2.1.8 - 图15 Delyan Haralanov

TreeSelect 树形选择 2.1.8 - 图16 Zhongxiang Wang

TreeSelect 树形选择 2.1.8 - 图17 Wu Chengyu

TreeSelect 树形选择 2.1.8 - 图18 云游君