ba-tree-pickerfixed.vue 16 KB

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