tree.uvue 7.2 KB

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