ba-tree-picker.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <!-- 树形层级选择器-->
  2. <!-- 1、支持单选、多选 -->
  3. <template>
  4. <view>
  5. <view
  6. class="tree-cover"
  7. :class="{ show: showDialog }"
  8. @tap="_cancel"
  9. ></view>
  10. <view class="tree-dialog" :class="{ show: showDialog }">
  11. <view class="tree-bar">
  12. <view
  13. class="tree-bar-cancel"
  14. :style="{ color: cancelColor }"
  15. hover-class="hover-c"
  16. @tap="_cancel"
  17. >取消
  18. </view>
  19. <view class="tree-bar-title" :style="{ color: titleColor }">{{
  20. title
  21. }}</view>
  22. <view
  23. class="tree-bar-confirm"
  24. :style="{ color: confirmColor }"
  25. hover-class="hover-c"
  26. @tap="_confirm"
  27. >
  28. {{ multiple ? "确定" : "" }}
  29. </view>
  30. </view>
  31. <view class="tree-view">
  32. <scroll-view class="tree-list" :scroll-y="true">
  33. <block v-for="(item, index) in treeList" :key="index">
  34. <view
  35. class="tree-item"
  36. :style="[
  37. {
  38. paddingLeft: item.level * 30 + 'rpx',
  39. },
  40. ]"
  41. :class="{
  42. itemBorder: border === true,
  43. show: item.isShow,
  44. }"
  45. >
  46. <view class="item-label">
  47. <view
  48. class="item-icon uni-inline-item"
  49. @tap.stop="_onItemSwitch(item, index)"
  50. >
  51. <view
  52. v-if="!item.isLastLevel && item.isShowChild"
  53. class="switch-on"
  54. :style="{ 'border-left-color': switchColor }"
  55. >
  56. </view>
  57. <view
  58. v-else-if="!item.isLastLevel && !item.isShowChild"
  59. class="switch-off"
  60. :style="{ 'border-top-color': switchColor }"
  61. >
  62. </view>
  63. <view
  64. v-else
  65. class="item-last-dot"
  66. :style="{ 'border-top-color': switchColor }"
  67. >
  68. </view>
  69. </view>
  70. <view
  71. class="uni-flex-item uni-inline-item"
  72. @tap.stop="_onItemSelect(item, index)"
  73. >
  74. <view class="item-name">
  75. {{
  76. item.name +
  77. (item.childCount ? "(" + item.childCount + ")" : "")
  78. }}
  79. </view>
  80. <view
  81. class="item-check"
  82. v-if="selectParent ? true : item.isLastLevel"
  83. >
  84. <view
  85. class="item-check-yes"
  86. v-if="item.checkStatus == 1"
  87. :class="{ radio: !multiple }"
  88. :style="{ 'border-color': confirmColor }"
  89. >
  90. <view
  91. class="item-check-yes-part"
  92. :style="{ 'background-color': confirmColor }"
  93. >
  94. </view>
  95. </view>
  96. <view
  97. class="item-check-yes"
  98. v-else-if="item.checkStatus == 2"
  99. :class="{ radio: !multiple }"
  100. :style="{ 'border-color': confirmColor }"
  101. >
  102. <view
  103. class="item-check-yes-all"
  104. :style="{ 'background-color': confirmColor }"
  105. >
  106. </view>
  107. </view>
  108. <view
  109. class="item-check-no"
  110. v-else
  111. :class="{ radio: !multiple }"
  112. :style="{ 'border-color': confirmColor }"
  113. ></view>
  114. </view>
  115. </view>
  116. </view>
  117. </view>
  118. </block>
  119. </scroll-view>
  120. </view>
  121. </view>
  122. </view>
  123. </template>
  124. <script>
  125. export default {
  126. emits: ["select-change"],
  127. name: "ba-tree-picker",
  128. props: {
  129. valueKey: {
  130. type: String,
  131. default: "id",
  132. },
  133. textKey: {
  134. type: String,
  135. default: "name",
  136. },
  137. childrenKey: {
  138. type: String,
  139. default: "children",
  140. },
  141. localdata: {
  142. type: Array,
  143. default: function () {
  144. return [];
  145. },
  146. },
  147. localTreeList: {
  148. //在已经格式化好的数据
  149. type: Array,
  150. default: function () {
  151. return [];
  152. },
  153. },
  154. selectedData: {
  155. type: Array,
  156. default: function () {
  157. return [];
  158. },
  159. },
  160. title: {
  161. type: String,
  162. default: "",
  163. },
  164. multiple: {
  165. // 是否可以多选
  166. type: Boolean,
  167. default: true,
  168. },
  169. selectParent: {
  170. //是否可以选父级
  171. type: Boolean,
  172. default: true,
  173. },
  174. confirmColor: {
  175. // 确定按钮颜色
  176. type: String,
  177. default: "", // #0055ff
  178. },
  179. cancelColor: {
  180. // 取消按钮颜色
  181. type: String,
  182. default: "", // #757575
  183. },
  184. titleColor: {
  185. // 标题颜色
  186. type: String,
  187. default: "", //
  188. },
  189. switchColor: {
  190. // 节点切换图标颜色
  191. type: String,
  192. default: "", // #666
  193. },
  194. border: {
  195. // 是否有分割线
  196. type: Boolean,
  197. default: false,
  198. },
  199. },
  200. data() {
  201. return {
  202. showDialog: false,
  203. treeList: [],
  204. };
  205. },
  206. computed: {},
  207. methods: {
  208. _show() {
  209. this.showDialog = true;
  210. },
  211. _hide() {
  212. this.showDialog = false;
  213. },
  214. _cancel() {
  215. this._hide();
  216. this.$emit("cancel", "");
  217. },
  218. _confirm() {
  219. //多选
  220. let selectedList = []; //如果子集全部选中,只返回父级 id
  221. let selectedNames;
  222. let currentLevel = -1;
  223. this.treeList.forEach((item, index) => {
  224. if (currentLevel >= 0 && item.level > currentLevel) {
  225. } else {
  226. if (item.checkStatus === 2) {
  227. currentLevel = item.level;
  228. selectedList.push(item.id);
  229. selectedNames = selectedNames
  230. ? selectedNames + " / " + item.name
  231. : item.name;
  232. } else {
  233. currentLevel = -1;
  234. }
  235. }
  236. });
  237. //console.log('_confirm', selectedList);
  238. console.log(selectedList);
  239. this._hide();
  240. this.$emit("select-change", selectedList, selectedNames);
  241. },
  242. //格式化原数据(原数据为tree结构)
  243. _formatTreeData(list = [], level = 0, parentItem, isShowChild = true) {
  244. let nextIndex = 0;
  245. let parentId = -1;
  246. let initCheckStatus = 0;
  247. if (parentItem) {
  248. nextIndex =
  249. this.treeList.findIndex((item) => item.id === parentItem.id) + 1;
  250. parentId = parentItem.id;
  251. if (!this.multiple) {
  252. //单选
  253. initCheckStatus = 0;
  254. } else initCheckStatus = parentItem.checkStatus == 2 ? 2 : 0;
  255. }
  256. list.forEach((item) => {
  257. let isLastLevel = true;
  258. if (item && item[this.childrenKey]) {
  259. let children = item[this.childrenKey];
  260. if (Array.isArray(children) && children.length > 0) {
  261. isLastLevel = false;
  262. }
  263. }
  264. let itemT = {
  265. id: item[this.valueKey],
  266. name: item[this.textKey],
  267. level,
  268. isLastLevel,
  269. isShow: isShowChild,
  270. isShowChild: false,
  271. checkStatus: initCheckStatus,
  272. orCheckStatus: 0,
  273. parentId,
  274. children: item[this.childrenKey],
  275. childCount: item[this.childrenKey]
  276. ? item[this.childrenKey].length
  277. : 0,
  278. childCheckCount: 0,
  279. childCheckPCount: 0,
  280. };
  281. if (this.selectedData.indexOf(itemT.id) >= 0) {
  282. itemT.checkStatus = 2;
  283. itemT.orCheckStatus = 2;
  284. itemT.childCheckCount = itemT.children ? itemT.children.length : 0;
  285. this._onItemParentSelect(itemT, nextIndex);
  286. }
  287. this.treeList.splice(nextIndex, 0, itemT);
  288. nextIndex++;
  289. });
  290. //console.log(this.treeList);
  291. },
  292. // 节点打开、关闭切换
  293. _onItemSwitch(item, index) {
  294. // console.log(item)
  295. //console.log('_itemSwitch')
  296. if (item.isLastLevel === true) {
  297. return;
  298. }
  299. item.isShowChild = !item.isShowChild;
  300. if (item.children) {
  301. this._formatTreeData(item.children, item.level + 1, item);
  302. item.children = undefined;
  303. } else {
  304. this._onItemChildSwitch(item, index);
  305. }
  306. },
  307. _onItemChildSwitch(item, index) {
  308. //console.log('_onItemChildSwitch')
  309. const firstChildIndex = index + 1;
  310. if (firstChildIndex > 0)
  311. for (var i = firstChildIndex; i < this.treeList.length; i++) {
  312. let itemChild = this.treeList[i];
  313. if (itemChild.level > item.level) {
  314. if (item.isShowChild) {
  315. if (itemChild.parentId === item.id) {
  316. itemChild.isShow = item.isShowChild;
  317. if (!itemChild.isShow) {
  318. itemChild.isShowChild = false;
  319. }
  320. }
  321. } else {
  322. itemChild.isShow = item.isShowChild;
  323. itemChild.isShowChild = false;
  324. }
  325. } else {
  326. return;
  327. }
  328. }
  329. },
  330. // 节点选中、取消选中
  331. _onItemSelect(item, index) {
  332. //console.log('_onItemSelect')
  333. //console.log(item)
  334. if (!this.multiple) {
  335. //单选
  336. item.checkStatus = item.checkStatus == 0 ? 2 : 0;
  337. this.treeList.forEach((v, i) => {
  338. if (i != index) {
  339. this.treeList[i].checkStatus = 0;
  340. } else {
  341. this.treeList[i].checkStatus = 2;
  342. }
  343. });
  344. let selectedList = [];
  345. let selectedNames;
  346. selectedList.push(item.id);
  347. selectedNames = item.name;
  348. this._hide();
  349. this.$emit("select-change", selectedList, selectedNames);
  350. return;
  351. }
  352. let oldCheckStatus = item.checkStatus;
  353. switch (oldCheckStatus) {
  354. case 0:
  355. item.checkStatus = 2;
  356. item.childCheckCount = item.childCount;
  357. item.childCheckPCount = 0;
  358. break;
  359. case 1:
  360. case 2:
  361. item.checkStatus = 0;
  362. item.childCheckCount = 0;
  363. item.childCheckPCount = 0;
  364. break;
  365. default:
  366. break;
  367. }
  368. //子节点 全部选中
  369. this._onItemChildSelect(item, index);
  370. //父节点 选中状态变化
  371. this._onItemParentSelect(item, index, oldCheckStatus);
  372. },
  373. _onItemChildSelect(item, index) {
  374. //console.log('_onItemChildSelect')
  375. let allChildCount = 0;
  376. if (item.childCount && item.childCount > 0) {
  377. index++;
  378. while (
  379. index < this.treeList.length &&
  380. this.treeList[index].level > item.level
  381. ) {
  382. let itemChild = this.treeList[index];
  383. itemChild.checkStatus = item.checkStatus;
  384. if (itemChild.checkStatus == 2) {
  385. itemChild.childCheckCount = itemChild.childCount;
  386. itemChild.childCheckPCount = 0;
  387. } else if (itemChild.checkStatus == 0) {
  388. itemChild.childCheckCount = 0;
  389. itemChild.childCheckPCount = 0;
  390. }
  391. // console.log('>>>>index:', index, 'item:', itemChild.name, ' status:', itemChild
  392. // .checkStatus)
  393. index++;
  394. }
  395. }
  396. },
  397. _onItemParentSelect(item, index, oldCheckStatus) {
  398. //console.log('_onItemParentSelect')
  399. //console.log(item)
  400. const parentIndex = this.treeList.findIndex(
  401. (itemP) => itemP.id == item.parentId
  402. );
  403. //console.log('parentIndex:' + parentIndex)
  404. if (parentIndex >= 0) {
  405. let itemParent = this.treeList[parentIndex];
  406. let count = itemParent.childCheckCount;
  407. let oldCheckStatusParent = itemParent.checkStatus;
  408. if (oldCheckStatus == 1) {
  409. itemParent.childCheckPCount -= 1;
  410. } else if (oldCheckStatus == 2) {
  411. itemParent.childCheckCount -= 1;
  412. }
  413. if (item.checkStatus == 1) {
  414. itemParent.childCheckPCount += 1;
  415. } else if (item.checkStatus == 2) {
  416. itemParent.childCheckCount += 1;
  417. }
  418. if (
  419. itemParent.childCheckCount <= 0 &&
  420. itemParent.childCheckPCount <= 0
  421. ) {
  422. itemParent.childCheckCount = 0;
  423. itemParent.childCheckPCount = 0;
  424. itemParent.checkStatus = 0;
  425. } else if (itemParent.childCheckCount >= itemParent.childCount) {
  426. itemParent.childCheckCount = itemParent.childCount;
  427. itemParent.childCheckPCount = 0;
  428. itemParent.checkStatus = 2;
  429. } else {
  430. itemParent.checkStatus = 1;
  431. }
  432. //console.log('itemParent:', itemParent)
  433. this._onItemParentSelect(itemParent, parentIndex, oldCheckStatusParent);
  434. }
  435. },
  436. // 重置数据
  437. _reTreeList() {
  438. this.treeList.forEach((v, i) => {
  439. this.treeList[i].checkStatus = v.orCheckStatus;
  440. });
  441. },
  442. _initTree() {
  443. this.treeList = [];
  444. this._formatTreeData(this.localdata);
  445. },
  446. },
  447. watch: {
  448. localdata() {
  449. this._initTree();
  450. },
  451. localTreeList() {
  452. this.treeList = this.localTreeList;
  453. },
  454. },
  455. mounted() {
  456. this._initTree();
  457. },
  458. };
  459. </script>
  460. <style scoped>
  461. .tree-cover {
  462. position: fixed;
  463. top: 0rpx;
  464. right: 0rpx;
  465. bottom: 0rpx;
  466. left: 0rpx;
  467. z-index: 100;
  468. background-color: rgba(0, 0, 0, 0.4);
  469. opacity: 0;
  470. transition: all 0.3s ease;
  471. visibility: hidden;
  472. }
  473. .tree-cover.show {
  474. visibility: visible;
  475. opacity: 1;
  476. }
  477. .tree-dialog {
  478. position: fixed;
  479. top: 0rpx;
  480. right: 0rpx;
  481. bottom: 0rpx;
  482. left: 0rpx;
  483. background-color: #fff;
  484. border-top-left-radius: 10px;
  485. border-top-right-radius: 10px;
  486. /* #ifndef APP-NVUE */
  487. display: flex;
  488. /* #endif */
  489. flex-direction: column;
  490. z-index: 102;
  491. top: 20%;
  492. transition: all 0.3s ease;
  493. transform: translateY(100%);
  494. }
  495. .tree-dialog.show {
  496. transform: translateY(0);
  497. }
  498. .tree-bar {
  499. /* background-color: #fff; */
  500. height: 90rpx;
  501. padding-left: 25rpx;
  502. padding-right: 25rpx;
  503. display: flex;
  504. justify-content: space-between;
  505. align-items: center;
  506. box-sizing: border-box;
  507. border-bottom-width: 1rpx !important;
  508. border-bottom-style: solid;
  509. border-bottom-color: #f5f5f5;
  510. font-size: 32rpx;
  511. color: #757575;
  512. line-height: 1;
  513. }
  514. .tree-bar-confirm {
  515. color: #0055ff;
  516. padding: 15rpx;
  517. }
  518. .tree-bar-title {
  519. }
  520. .tree-bar-cancel {
  521. color: #757575;
  522. padding: 15rpx;
  523. }
  524. .tree-view {
  525. flex: 1;
  526. padding: 20rpx;
  527. /* #ifndef APP-NVUE */
  528. display: flex;
  529. /* #endif */
  530. flex-direction: column;
  531. overflow: hidden;
  532. height: 100%;
  533. }
  534. .tree-list {
  535. flex: 1;
  536. height: 100%;
  537. overflow: hidden;
  538. }
  539. .tree-item {
  540. display: flex;
  541. justify-content: space-between;
  542. align-items: center;
  543. line-height: 1;
  544. height: 0;
  545. opacity: 0;
  546. transition: 0.2s;
  547. overflow: hidden;
  548. }
  549. .tree-item.show {
  550. height: 90rpx;
  551. opacity: 1;
  552. }
  553. .tree-item.showchild:before {
  554. transform: rotate(90deg);
  555. }
  556. .tree-item.last:before {
  557. opacity: 0;
  558. }
  559. .switch-on {
  560. width: 0;
  561. height: 0;
  562. border-left: 10rpx solid transparent;
  563. border-right: 10rpx solid transparent;
  564. border-top: 15rpx solid #666;
  565. }
  566. .switch-off {
  567. width: 0;
  568. height: 0;
  569. border-bottom: 10rpx solid transparent;
  570. border-top: 10rpx solid transparent;
  571. border-left: 15rpx solid #666;
  572. }
  573. .item-last-dot {
  574. position: absolute;
  575. width: 10rpx;
  576. height: 10rpx;
  577. border-radius: 100%;
  578. background: #666;
  579. }
  580. .item-icon {
  581. width: 26rpx;
  582. height: 26rpx;
  583. margin-right: 8rpx;
  584. padding-right: 20rpx;
  585. padding-left: 20rpx;
  586. }
  587. .item-label {
  588. flex: 1;
  589. display: flex;
  590. align-items: center;
  591. height: 100%;
  592. line-height: 1.2;
  593. }
  594. .item-name {
  595. flex: 1;
  596. overflow: hidden;
  597. text-overflow: ellipsis;
  598. white-space: nowrap;
  599. width: 450rpx;
  600. }
  601. .item-check {
  602. width: 40px;
  603. height: 40px;
  604. display: flex;
  605. justify-content: center;
  606. align-items: center;
  607. }
  608. .item-check-yes,
  609. .item-check-no {
  610. width: 20px;
  611. height: 20px;
  612. border-top-left-radius: 20%;
  613. border-top-right-radius: 20%;
  614. border-bottom-right-radius: 20%;
  615. border-bottom-left-radius: 20%;
  616. border-top-width: 1rpx;
  617. border-left-width: 1rpx;
  618. border-bottom-width: 1rpx;
  619. border-right-width: 1rpx;
  620. border-style: solid;
  621. border-color: #0055ff;
  622. display: flex;
  623. justify-content: center;
  624. align-items: center;
  625. box-sizing: border-box;
  626. }
  627. .item-check-yes-part {
  628. width: 12px;
  629. height: 12px;
  630. border-top-left-radius: 20%;
  631. border-top-right-radius: 20%;
  632. border-bottom-right-radius: 20%;
  633. border-bottom-left-radius: 20%;
  634. background-color: #0055ff;
  635. }
  636. .item-check-yes-all {
  637. margin-bottom: 5px;
  638. border: 2px solid #007aff;
  639. border-left: 0;
  640. border-top: 0;
  641. height: 12px;
  642. width: 6px;
  643. transform-origin: center;
  644. /* #ifndef APP-NVUE */
  645. transition: all 0.3s;
  646. /* #endif */
  647. transform: rotate(45deg);
  648. }
  649. .item-check .radio {
  650. border-top-left-radius: 50%;
  651. border-top-right-radius: 50%;
  652. border-bottom-right-radius: 50%;
  653. border-bottom-left-radius: 50%;
  654. }
  655. .item-check .radio .item-check-yes-b {
  656. border-top-left-radius: 50%;
  657. border-top-right-radius: 50%;
  658. border-bottom-right-radius: 50%;
  659. border-bottom-left-radius: 50%;
  660. }
  661. .hover-c {
  662. opacity: 0.6;
  663. }
  664. .itemBorder {
  665. border-bottom: 1px solid #e5e5e5;
  666. }
  667. </style>