tree.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view>
  3. <view class="cu-form-group solid-bottom ">
  4. <input placeholder="输入关键字进行过滤" v-model="filterText" ></input>
  5. </view>
  6. <ly-tree :tree-data="treeData"
  7. ref="tree"
  8. node-key="id"
  9. :ready="true"
  10. :accordion="true"
  11. :filter-node-method="filterNode"
  12. :highlightCurrent="true"
  13. :checkOnlyLeaf="true"
  14. @node-click="handleNodeClick">
  15. </ly-tree>
  16. <view class="cu-bar bg-white margin-top">
  17. <view class="action">
  18. <text class="cuIcon-title text-orange "></text> 侧边抽屉
  19. </view>
  20. <view class="action">
  21. <button class="cu-btn bg-green shadow" @tap="showModal" data-target="DrawerModalL">Left</button>
  22. <button class="cu-btn bg-blue shadow margin-left" @tap="showModal" data-target="DrawerModalR">Right</button>
  23. </view>
  24. </view>
  25. <view class="cu-modal drawer-modal justify-start" :class="modalName=='DrawerModalL'?'show':''" @tap="hideModal">
  26. <view class="bg-white cu-dialog basis-lg" @tap.stop="" :style="[{top:CustomBar+'px',height:'calc(100vh - ' + CustomBar + 'px)'}]">
  27. <scroll-view scroll-y="true" style="height: 100%;">
  28. <view class="cu-list menu text-left">
  29. <view class="cu-form-group solid-bottom ">
  30. <input placeholder="输入关键字进行过滤" v-model="filterTextLeft" ></input>
  31. </view>
  32. <ly-tree :tree-data="treeData"
  33. ref="treeLeft"
  34. node-key="id"
  35. :ready="true"
  36. :accordion="true"
  37. :filter-node-method="filterNodeLeft"
  38. :highlightCurrent="true"
  39. :checkOnlyLeaf="true"
  40. @node-click="handleNodeClick">
  41. </ly-tree>
  42. </view>
  43. </scroll-view>
  44. </view>
  45. </view>
  46. <view class="cu-modal drawer-modal justify-end" :class="modalName=='DrawerModalR'?'show':''" @tap="hideModal">
  47. <view class="cu-dialog basis-lg" @tap.stop="" :style="[{top:CustomBar+'px',height:'calc(100vh - ' + CustomBar + 'px)'}]">
  48. <scroll-view scroll-y="true" style="height: 100%;">
  49. <view class="bg-white cu-list tabbar menu text-left" style="margin-bottom: 100px;">
  50. <view class="cu-form-group solid-bottom ">
  51. <input placeholder="输入关键字进行过滤" v-model="filterTextRight" ></input>
  52. </view>
  53. <ly-tree :tree-data="treeData"
  54. ref="treeRight"
  55. node-key="id"
  56. :defaultExpandAll="true"
  57. :ready="true"
  58. :accordion="true"
  59. :filter-node-method="filterNodeRight"
  60. :highlightCurrent="true"
  61. :checkOnlyLeaf="true"
  62. @node-click="handleNodeClick">
  63. </ly-tree>
  64. </view>
  65. </scroll-view>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. export default {
  72. data() {
  73. return {
  74. CustomBar: this.CustomBar,
  75. modalName: null,
  76. filterText:"",
  77. filterTextLeft:"",
  78. filterTextRight:"",
  79. ready: false, // 这里用于自主控制loading加载状态,避免异步正在加载数据的空档显示“暂无数据”
  80. treeData: [{
  81. id: 1,
  82. label: '一级 1',
  83. children: [{
  84. id: 11,
  85. label: '二级 1-1',
  86. children: [{
  87. id: 111,
  88. label: '三级 1-1-1'
  89. },{
  90. id: 112,
  91. label: '三级 1-1-2'
  92. }]
  93. }]
  94. }, {
  95. id: 2,
  96. label: '一级 2',
  97. children: [{
  98. id: 21,
  99. label: '二级 2-1',
  100. children: [{
  101. id: 211,
  102. label: '三级 2-1-1'
  103. }]
  104. }, {
  105. id: 22,
  106. label: '二级 2-2',
  107. children: [{
  108. id: 221,
  109. label: '三级 2-2-1'
  110. }]
  111. }]
  112. }, {
  113. id: 3,
  114. label: '一级 3',
  115. children: [{
  116. id: 31,
  117. label: '二级 3-1',
  118. children: [{
  119. id: 311,
  120. label: '三级 3-1-1'
  121. }]
  122. }, {
  123. id: 32,
  124. label: '二级 3-2',
  125. children: [{
  126. id: 321,
  127. label: '三级 3-2-1'
  128. }]
  129. }]
  130. }],
  131. };
  132. },
  133. methods: {
  134. handleNodeClick(obj) {
  135. console.log("子节点信息:",obj);
  136. console.log("子节点路径:",this.$refs.tree.getNodePath(obj))
  137. },
  138. filterNode(value, data) {
  139. if (!value) return true;
  140. return data.label.indexOf(value) !== -1;
  141. },
  142. filterNodeLeft(value, data) {
  143. if (!value) return true;
  144. return data.label.indexOf(value) !== -1;
  145. },
  146. filterNodeRight(value, data) {
  147. if (!value) return true;
  148. return data.label.indexOf(value) !== -1;
  149. },
  150. showModal(e) {
  151. this.modalName = e.currentTarget.dataset.target
  152. },
  153. hideModal(e) {
  154. this.modalName = null
  155. },
  156. },
  157. watch:{
  158. filterText(val) {
  159. this.$refs.tree.filter(val);
  160. },
  161. filterTextLeft(val) {
  162. this.$refs.treeLeft.filter(val);
  163. },
  164. filterTextRight(val) {
  165. this.$refs.treeRight.filter(val);
  166. },
  167. }
  168. };
  169. </script>
  170. <style></style>