ba-tree-pickerfixed.vue 16 KB

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