当前位置:   article > 正文

VueTreeselect树控件搜索+下拉框功能_vue-treeselect中文文档

vue-treeselect中文文档

Vue-Treeselect | Vue-Treeselect 中文网 

以上为网址

使用方式文档也有写的很清楚

 

 

上代码

  1. <treeselect ref="cbTreeselect" v-model="kkgsd" :multiple="false" :normalizer="normalizer" :options="optionsList" :backspace-removes="false" class="width272" :load-options="iscbAsync ? loadTagsOptions : null" :clear-on-select="true" :cache-options="iscbAsync" :default-options="iscbAsync" :search-prompt-text="'请搜索'" :async="iscbAsync" placeholder="请输入区划、道路名称和代码" @open="treeOpen()">
  2. <label slot="option-label" slot-scope="{ node }">
  3. <b v-if="node.isNew">{{ node.label }}{{ ' (新手动标签)' }}</b>
  4. <span v-else>{{ node.label }}</span>
  5. </label>
  6. </treeselect>

 js

  1. data() {
  2. return {
  3. iscbAsync: false,
  4. optionsList: [{
  5. key: 'a',
  6. name: 'a',
  7. children: [{
  8. key: 'ab',
  9. name: 'ab'
  10. }]
  11. },
  12. {
  13. key: 'c',
  14. name: 'c',
  15. children: [{
  16. key: 'c-c',
  17. name: 'c-d'
  18. }]
  19. },
  20. {
  21. key: 'd',
  22. name: 'c',
  23. children: [{
  24. key: 'd-c',
  25. name: 'd-d'
  26. }]
  27. }
  28. ],
  29. normalizer(node) {
  30. if (node.children == null || node.children == 'null' || node.children && !node.children.length) {
  31. delete node.children
  32. }
  33. return {
  34. id: node.key,
  35. label: node.name,
  36. children: node.children
  37. }
  38. },
  39. }
  40. },
  41. methods: {
  42. loadTagsOptions({ action, searchQuery, callback }) {
  43. // console.log('调用接口', action, searchQuery)
  44. // 输入时异步加载
  45. if (action === 'ASYNC_SEARCH') {
  46. let options = []
  47. const recursion = function (arr, searchQuery) {
  48. arr.forEach(item => {
  49. if (item.children && item.children.length) {
  50. // 满足模糊搜素则返回整个父节点联带的子节点
  51. console.log('满足模糊搜素则返回整个父节点联带的子节点', item.children)
  52. if (item.name.indexOf(searchQuery) > -1) {
  53. options.push({
  54. key: item.key,
  55. name: item.name,
  56. children: item.children
  57. })
  58. } else {
  59. // 不满足则继续往下找
  60. console.log('不满足则继续往下找', item.children)
  61. recursion(item.children, searchQuery)
  62. }
  63. } else {
  64. // 找到就push到数组中
  65. console.log('找到就push到数组中', item.name)
  66. if (item.name.indexOf(searchQuery) > -1) {
  67. options.push(item)
  68. }
  69. }
  70. })
  71. }
  72. // 1. 搜索空字符串,则展示全部已有标签
  73. if (searchQuery.trim() === '') {
  74. options = [...this.optionsList]
  75. } else {
  76. // 2.不为空,则生成新标签+过滤已有标签
  77. // 2.1 过滤已有标签
  78. console.log('不为空+过滤已有标签')
  79. recursion(this.optionsList, searchQuery)
  80. // 2.2 新增标签(如果没有完全匹配到,则新增)
  81. if (!options.length) {
  82. options.push({
  83. key: searchQuery,
  84. name: searchQuery,
  85. isNew: true
  86. })
  87. }
  88. }
  89. // 回传
  90. callback(null, options)
  91. }
  92. },
  93. treeOpen() {
  94. this.iscbAsync = true
  95. },}

如果想在没有搜索到的时候只是提示就注释手动推入方法

treeselect 标签加上:noResultsText="'未搜索到匹配项...'"

如果想调用方法中用可以把递归改成axios方法即可 

 

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

闽ICP备14008679号