filtering.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <el-input v-model="filterText" placeholder="Filter keyword" />
  3. <el-tree ref="treeRef" class="filter-tree" :data="data" :props="defaultProps" default-expand-all :filter-node-method="filterNode" />
  4. </template>
  5. <script lang="ts" setup>
  6. import { ref, watch } from 'vue'
  7. import type { ElTree } from 'element-plus'
  8. interface Tree {
  9. id: number
  10. label: string
  11. children?: Tree[]
  12. }
  13. const filterText = ref('')
  14. const treeRef = ref<InstanceType<typeof ElTree>>()
  15. const defaultProps = {
  16. children: 'children',
  17. label: 'label',
  18. }
  19. watch(filterText, val => {
  20. treeRef.value!.filter(val)
  21. })
  22. const filterNode = (value: string, data: Tree) => {
  23. if (!value) return true
  24. return data.label.includes(value)
  25. }
  26. const data: Tree[] = [
  27. {
  28. id: 1,
  29. label: 'Level one 1',
  30. children: [
  31. {
  32. id: 4,
  33. label: 'Level two 1-1',
  34. children: [
  35. {
  36. id: 9,
  37. label: 'Level three 1-1-1',
  38. },
  39. {
  40. id: 10,
  41. label: 'Level three 1-1-2',
  42. },
  43. ],
  44. },
  45. ],
  46. },
  47. {
  48. id: 2,
  49. label: 'Level one 2',
  50. children: [
  51. {
  52. id: 5,
  53. label: 'Level two 2-1',
  54. },
  55. {
  56. id: 6,
  57. label: 'Level two 2-2',
  58. },
  59. ],
  60. },
  61. {
  62. id: 3,
  63. label: 'Level one 3',
  64. children: [
  65. {
  66. id: 7,
  67. label: 'Level two 3-1',
  68. },
  69. {
  70. id: 8,
  71. label: 'Level two 3-2',
  72. },
  73. ],
  74. },
  75. ]
  76. </script>