ba-tree-pickerfixed.vue 16 KB

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