当前位置:   article > 正文

ant design可搜索的树,自定义树数据_antdesignvue可搜索的树

antdesignvue可搜索的树

先上效果图:

 基于Ant Design的树结构,vue3

  1. <template>
  2. <div>
  3. <a-input-search
  4. v-model:value="searchValue"
  5. style="margin-bottom: 8px"
  6. placeholder="Search"
  7. />
  8. <a-tree
  9. :expandedKeys="expandedKeys"
  10. :auto-expand-parent="autoExpandParent"
  11. :tree-data="gData"
  12. @expand="onExpand"
  13. >
  14. <template #title="{ title }">
  15. <span v-if="title.indexOf(searchValue) > -1">
  16. {{ title.substr(0, title.indexOf(searchValue)) }}
  17. <span style="color: #f50">{{ searchValue }}</span>
  18. {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
  19. </span>
  20. <span v-else>{{ title }}</span>
  21. </template>
  22. </a-tree>
  23. </div>
  24. </template>
  25. <script>
  26. import { defineComponent, ref, watch } from "vue";
  27. const genData = [
  28. {
  29. title: "河北省",
  30. key: "河北省",
  31. children: [
  32. {
  33. title: "石家庄市",
  34. key: "石家庄市",
  35. children: [
  36. {
  37. title: "桥西区",
  38. key: "桥西区",
  39. },
  40. {
  41. title: "新华区",
  42. key: "新华区",
  43. },
  44. ],
  45. },
  46. {
  47. title: "唐山市",
  48. key: "唐山市",
  49. children: [
  50. {
  51. title: "丰南区",
  52. key: "丰南区",
  53. },
  54. ],
  55. },
  56. ],
  57. },
  58. {
  59. title: "山西省",
  60. key: "山西省",
  61. children: [
  62. {
  63. title: "太原市",
  64. key: "太原市",
  65. },
  66. {
  67. title: "大同市",
  68. key: "大同市",
  69. children: [
  70. {
  71. title: "云冈区",
  72. key: "云冈区",
  73. },
  74. {
  75. title: "新荣区",
  76. key: "新荣区",
  77. },
  78. ],
  79. },
  80. ],
  81. },
  82. ];
  83. const dataList = [];
  84. const generateList = (data) => {
  85. for (let i = 0; i < data.length; i++) {
  86. const node = data[i];
  87. const key = node.key;
  88. dataList.push({
  89. key,
  90. title: key,
  91. });
  92. if (node.children) {
  93. generateList(node.children);
  94. }
  95. }
  96. };
  97. generateList(genData);
  98. const getParentKey = (key, tree) => {
  99. let parentKey;
  100. for (let i = 0; i < tree.length; i++) {
  101. const node = tree[i];
  102. if (node.children) {
  103. if (node.children.some((item) => item.key === key)) {
  104. parentKey = node.key;
  105. } else if (getParentKey(key, node.children)) {
  106. parentKey = getParentKey(key, node.children);
  107. }
  108. }
  109. }
  110. return parentKey;
  111. };
  112. export default defineComponent({
  113. setup() {
  114. const expandedKeys = ref([]);
  115. const searchValue = ref("");
  116. const autoExpandParent = ref(true);
  117. const gData = ref(genData);
  118. const onExpand = (keys) => {
  119. expandedKeys.value = keys;
  120. autoExpandParent.value = false;
  121. };
  122. watch(searchValue, (value) => {
  123. const expanded = dataList
  124. .map((item) => {
  125. if (item.title.indexOf(value) > -1) {
  126. return getParentKey(item.key, gData.value);
  127. }
  128. return null;
  129. })
  130. .filter((item, i, self) => item && self.indexOf(item) === i);
  131. expandedKeys.value = expanded;
  132. searchValue.value = value;
  133. autoExpandParent.value = true;
  134. });
  135. return {
  136. expandedKeys,
  137. searchValue,
  138. autoExpandParent,
  139. gData,
  140. onExpand,
  141. };
  142. },
  143. });
  144. </script>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号