当前位置:   article > 正文

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

回显数据

一、前言

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

二、源码

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/577720
推荐阅读
相关标签
  

闽ICP备14008679号