• el-select配合el-tree实现下拉选以及数据回显以及搜索


    一、前言

            有时候就会遇到组件配合使用的情况,然后就整理了一下,后面大家需要的话可以直接拿去使用。

    二、源码

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. name: '',
    6. categoryId: null,
    7. data: [
    8. {
    9. id: 1,
    10. pid: null,
    11. parentId: 0,
    12. children: [
    13. {
    14. id: 2,
    15. pid: null,
    16. parentId: 1,
    17. children: [
    18. {
    19. id: 8,
    20. pid: null,
    21. parentId: 2,
    22. children: [],
    23. postCode: '',
    24. postName: '添加1',
    25. sort: null,
    26. tenantId: 120,
    27. status: 1,
    28. createTime: '2023-10-30 14:28:16',
    29. childPost: null,
    30. postType: 2,
    31. nodeType: null,
    32. postAdmin: 10024
    33. },
    34. {
    35. id: 9,
    36. pid: null,
    37. parentId: 2,
    38. children: [],
    39. postCode: '',
    40. postName: '测试添加',
    41. sort: null,
    42. tenantId: 120,
    43. status: 1,
    44. createTime: '2023-11-01 15:30:22',
    45. childPost: null,
    46. postType: 2,
    47. nodeType: null,
    48. postAdmin: 10022
    49. },
    50. {
    51. id: 11,
    52. pid: null,
    53. parentId: 2,
    54. children: [],
    55. postCode: '',
    56. postName: '测试',
    57. sort: null,
    58. tenantId: 120,
    59. status: 1,
    60. createTime: '2023-11-02 13:26:24',
    61. childPost: null,
    62. postType: 2,
    63. nodeType: null,
    64. postAdmin: 10024
    65. },
    66. {
    67. id: 13,
    68. pid: null,
    69. parentId: 2,
    70. children: [],
    71. postCode: '',
    72. postName: '测试111',
    73. sort: null,
    74. tenantId: 120,
    75. status: 1,
    76. createTime: '2023-11-02 13:48:44',
    77. childPost: null,
    78. postType: 2,
    79. nodeType: null,
    80. postAdmin: 10022
    81. }
    82. ],
    83. postCode: null,
    84. postName: '人事部门',
    85. sort: null,
    86. tenantId: 120,
    87. status: 1,
    88. createTime: '2023-10-26 10:55:18',
    89. childPost: null,
    90. postType: 2,
    91. nodeType: null,
    92. postAdmin: null
    93. }
    94. ],
    95. postCode: null,
    96. postName: '回集团根节点',
    97. sort: null,
    98. tenantId: 120,
    99. status: 1,
    100. createTime: '2023-10-26 10:55:18',
    101. childPost: null,
    102. postType: 1,
    103. nodeType: 6,
    104. postAdmin: 10000
    105. }
    106. ],
    107. defaultProps: {
    108. children: 'children',
    109. label: 'postName'
    110. }
    111. }
    112. },
    113. methods: {
    114. filterNode(value, data) {
    115. if (!value) return true
    116. return data.postName.indexOf(value) !== -1
    117. },
    118. // 节点点击事件
    119. handleNodeClick(data, node, nodeData) {
    120. this.name = data.postName
    121. this.categoryId = data.id
    122. this.$refs.selectTree.blur()
    123. },
    124. // 数据集回显
    125. echoDataSet(val) {
    126. if (!val) return
    127. const categoryId = val.id || ''
    128. this.categoryId = categoryId
    129. this.$refs.tree.setCurrentKey(categoryId) // 设置节点高亮
    130. },
    131. focus(val) {
    132. this.$refs.tree.filter(val)
    133. },
    134. clearValue() {
    135. this.$refs.tree.setCurrentKey(null)
    136. }
    137. }
    138. }
    139. script>
    140. <style>
    141. .el-select-dropdown__item {
    142. padding: 0;
    143. }
    144. style>

    关于回显只需要在编辑的时候进行name字段的赋值,以及触发代码中数据回显的方法,传入需要选中的id就可以了。

  • 相关阅读:
    Roson的Qt之旅#104 QML Image控件
    【论文笔记】Encoding cloth manipulations using a graph of states and transitions
    数据结构——动态顺序表
    JAVA 中集合取交集
    【虚拟机】网卡不见了,失效了怎么办
    【重识云原生】第六章容器6.3.5节——Controller Manager概述
    this is biaoti
    微信公众号开发接入
    为什么Java能够称霸移动开发领域这么多年?
    Linux aarch64交叉编译之glm数学库
  • 原文地址:https://blog.csdn.net/ct5211314/article/details/134262700