ba-tree-picker.vue 16 KB

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