tree.uvue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <cl-page>
  3. <view class="p-3">
  4. <demo-item :label="t('树形结构')">
  5. <cl-tree
  6. ref="treeRef"
  7. :list="list"
  8. :icon="isCustomIcon ? 'add-circle-line' : 'arrow-right-s-fill'"
  9. :expand-icon="isCustomIcon ? 'indeterminate-circle-line' : 'arrow-down-s-fill'"
  10. :show-checkbox="true"
  11. :check-strictly="checkStrictly"
  12. ></cl-tree>
  13. <cl-list border :pt="{ className: 'mt-5' }">
  14. <cl-list-item :label="t('父子关联')">
  15. <cl-switch v-model="checkStrictly"></cl-switch>
  16. </cl-list-item>
  17. <cl-list-item :label="t('换个图标')">
  18. <cl-switch v-model="isCustomIcon"></cl-switch>
  19. </cl-list-item>
  20. </cl-list>
  21. </demo-item>
  22. <demo-item :label="t('选中操作')">
  23. <view class="mb-2">
  24. <cl-button @tap="setChecked">{{ t("选中部分节点") }}</cl-button>
  25. </view>
  26. <view class="mb-2">
  27. <cl-button @tap="getChecked">{{ t("获取选中节点") }}</cl-button>
  28. <cl-text
  29. v-if="checkedKeys.length > 0"
  30. :pt="{
  31. className: '!text-sm p-2'
  32. }"
  33. >{{ checkedKeys.join(", ") }}</cl-text
  34. >
  35. </view>
  36. <view class="mb-2">
  37. <cl-button @tap="getHalfChecked">{{ t("获取半选节点") }}</cl-button>
  38. <cl-text
  39. v-if="halfCheckedKeys.length > 0"
  40. :pt="{
  41. className: '!text-sm p-2'
  42. }"
  43. >{{ halfCheckedKeys.join(", ") }}</cl-text
  44. >
  45. </view>
  46. <view class="mb-2">
  47. <cl-button @tap="clearChecked">{{ t("清空选中") }}</cl-button>
  48. </view>
  49. </demo-item>
  50. <demo-item :label="t('展开操作')">
  51. <view class="mb-2">
  52. <cl-button @tap="expand">{{ t("展开部分节点") }}</cl-button>
  53. </view>
  54. <view class="mb-2">
  55. <cl-button @tap="getExpanded">{{ t("获取展开节点") }}</cl-button>
  56. <cl-text
  57. v-if="expandedKeys.length > 0"
  58. :pt="{
  59. className: '!text-sm p-2'
  60. }"
  61. >{{ expandedKeys.join(", ") }}</cl-text
  62. >
  63. </view>
  64. <view class="mb-2">
  65. <cl-button @tap="expandAll">{{ t("展开所有") }}</cl-button>
  66. </view>
  67. <view class="mb-2">
  68. <cl-button @tap="collapseAll">{{ t("收起所有") }}</cl-button>
  69. </view>
  70. </demo-item>
  71. </view>
  72. </cl-page>
  73. </template>
  74. <script lang="ts" setup>
  75. import DemoItem from "../components/item.uvue";
  76. import { t } from "@/locale";
  77. import { ref } from "vue";
  78. import { useTree } from "@/uni_modules/cool-ui";
  79. const list = useTree([
  80. {
  81. id: "1",
  82. label: "华为",
  83. children: [
  84. {
  85. id: "1-1",
  86. label: "手机",
  87. children: [
  88. {
  89. id: "1-1-1",
  90. label: "Mate系列",
  91. children: [
  92. {
  93. id: "1-1-1-1",
  94. label: "Mate 50"
  95. },
  96. {
  97. id: "1-1-1-2",
  98. label: "Mate 40"
  99. },
  100. {
  101. id: "1-1-1-3",
  102. label: "Mate 30"
  103. }
  104. ]
  105. },
  106. {
  107. id: "1-1-2",
  108. label: "P系列",
  109. children: [
  110. {
  111. id: "1-1-2-1",
  112. label: "P60"
  113. },
  114. {
  115. id: "1-1-2-2",
  116. label: "P50"
  117. },
  118. {
  119. id: "1-1-2-3",
  120. label: "P40"
  121. }
  122. ]
  123. }
  124. ]
  125. },
  126. {
  127. id: "1-2",
  128. label: "笔记本",
  129. children: [
  130. {
  131. id: "1-2-1",
  132. label: "MateBook X",
  133. children: [
  134. {
  135. id: "1-2-1-1",
  136. label: "MateBook X Pro"
  137. },
  138. {
  139. id: "1-2-1-2",
  140. label: "MateBook X 2022"
  141. }
  142. ]
  143. },
  144. {
  145. id: "1-2-2",
  146. label: "MateBook D",
  147. children: [
  148. {
  149. id: "1-2-2-1",
  150. label: "MateBook D 14"
  151. },
  152. {
  153. id: "1-2-2-2",
  154. label: "MateBook D 15"
  155. }
  156. ]
  157. },
  158. {
  159. id: "1-2-3",
  160. label: "MateBook 13"
  161. }
  162. ]
  163. }
  164. ]
  165. },
  166. {
  167. id: "2",
  168. label: "小米",
  169. children: [
  170. {
  171. id: "2-1",
  172. label: "手机",
  173. children: [
  174. {
  175. id: "2-1-1",
  176. label: "小米数字系列"
  177. },
  178. {
  179. id: "2-1-2",
  180. label: "Redmi系列"
  181. }
  182. ]
  183. },
  184. {
  185. id: "2-2",
  186. label: "家电",
  187. children: [
  188. {
  189. id: "2-2-1",
  190. label: "电视"
  191. },
  192. {
  193. id: "2-2-2",
  194. label: "空调"
  195. }
  196. ]
  197. }
  198. ]
  199. },
  200. {
  201. id: "3",
  202. label: "苹果",
  203. children: [
  204. {
  205. id: "3-1",
  206. label: "手机",
  207. children: [
  208. {
  209. id: "3-1-1",
  210. label: "iPhone 14"
  211. },
  212. {
  213. id: "3-1-2",
  214. label: "iPhone 13"
  215. }
  216. ]
  217. },
  218. {
  219. id: "3-2",
  220. label: "平板",
  221. children: [
  222. {
  223. id: "3-2-1",
  224. label: "iPad Pro"
  225. },
  226. {
  227. id: "3-2-2",
  228. label: "iPad Air"
  229. }
  230. ]
  231. }
  232. ]
  233. },
  234. {
  235. id: "4",
  236. label: "OPPO",
  237. children: [
  238. {
  239. id: "4-1",
  240. label: "手机",
  241. children: [
  242. {
  243. id: "4-1-1",
  244. label: "Find系列"
  245. },
  246. {
  247. id: "4-1-2",
  248. label: "Reno系列"
  249. }
  250. ]
  251. },
  252. {
  253. id: "4-2",
  254. label: "配件",
  255. children: [
  256. {
  257. id: "4-2-1",
  258. label: "耳机"
  259. },
  260. {
  261. id: "4-2-2",
  262. label: "手环"
  263. }
  264. ]
  265. }
  266. ]
  267. },
  268. {
  269. id: "5",
  270. label: "vivo",
  271. children: [
  272. {
  273. id: "5-1",
  274. label: "手机",
  275. children: [
  276. {
  277. id: "5-1-1",
  278. label: "X系列"
  279. },
  280. {
  281. id: "5-1-2",
  282. label: "S系列"
  283. }
  284. ]
  285. },
  286. {
  287. id: "5-2",
  288. label: "智能设备",
  289. children: [
  290. {
  291. id: "5-2-1",
  292. label: "手表"
  293. },
  294. {
  295. id: "5-2-2",
  296. label: "耳机"
  297. }
  298. ]
  299. }
  300. ]
  301. }
  302. ]);
  303. // 是否严格的遵循父子不互相关联
  304. const checkStrictly = ref(false);
  305. // 是否自定义图标
  306. const isCustomIcon = ref(false);
  307. // 树形组件引用
  308. const treeRef = ref<ClTreeComponentPublicInstance | null>(null);
  309. // 选中节点的keys
  310. const checkedKeys = ref<(string | number)[]>([]);
  311. // 半选节点的keys
  312. const halfCheckedKeys = ref<(string | number)[]>([]);
  313. // 展开节点的keys
  314. const expandedKeys = ref<(string | number)[]>([]);
  315. // 演示方法
  316. function setChecked() {
  317. treeRef.value!.setCheckedKeys(["1-1", "2"]);
  318. }
  319. function getChecked() {
  320. checkedKeys.value = treeRef.value!.getCheckedKeys();
  321. }
  322. function clearChecked() {
  323. treeRef.value!.clearChecked();
  324. checkedKeys.value = [];
  325. halfCheckedKeys.value = [];
  326. }
  327. function getHalfChecked() {
  328. halfCheckedKeys.value = treeRef.value!.getHalfCheckedKeys();
  329. }
  330. function expand() {
  331. treeRef.value!.setExpandedKeys(["1", "1-1", "2"]);
  332. }
  333. function getExpanded() {
  334. expandedKeys.value = treeRef.value!.getExpandedKeys();
  335. }
  336. function expandAll() {
  337. treeRef.value!.expandAll();
  338. expandedKeys.value = treeRef.value!.getExpandedKeys();
  339. }
  340. function collapseAll() {
  341. treeRef.value!.collapseAll();
  342. expandedKeys.value = [];
  343. }
  344. </script>