ba-tree-picker.vue 15 KB

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