ba-tree-picker.vue 15 KB

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