ba-tree-pickerfixed.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. var multiple=this.multiple;//是否多选
  157. this.treeList.forEach((item, index) => {
  158. // console.log(item,1)
  159. if (currentLevel >= 0 && item.level > currentLevel) {
  160. } else {
  161. if (item.checkStatus === 2) {
  162. // 判断有无子元素
  163. currentLevel = item.level;
  164. if(item.children&&item.children.length&&multiple){
  165. var children=item.children;
  166. children.forEach(ite=>{
  167. var obj={
  168. deptId:ite.id,
  169. deptName:ite.label,
  170. deptType:that.deptType
  171. }
  172. selectedList.push(obj);
  173. })
  174. }else{
  175. var obj={
  176. deptId:item.id,
  177. deptName:item.name,
  178. deptType:that.deptType
  179. }
  180. selectedList.push(obj);
  181. }
  182. // selectedNames = selectedNames ? selectedNames + ' / ' + item.name : item.name;
  183. } else {
  184. currentLevel = -1;
  185. }
  186. }
  187. })
  188. this._hide()
  189. // this.$emit("select-change", selectedList, selectedNames);
  190. this.$emit("select-change", selectedList);
  191. },
  192. //格式化原数据(原数据为tree结构)
  193. _formatTreeData(list = [], level = 0, parentItem, isShowChild = true) {
  194. let nextIndex = 0;
  195. let parentId = -1;
  196. let initCheckStatus = 0;
  197. if (parentItem) {
  198. nextIndex = this.treeList.findIndex(item => item.id === parentItem.id) + 1;
  199. parentId = parentItem.id;
  200. if (!this.multiple) { //单选
  201. initCheckStatus = 0;
  202. } else
  203. initCheckStatus = parentItem.checkStatus == 2 ? 2 : 0;
  204. }
  205. list.forEach(item => {
  206. let isLastLevel = true;
  207. if (item && item[this.childrenKey]) {
  208. let children = item[this.childrenKey];
  209. if (Array.isArray(children) && children.length > 0) {
  210. isLastLevel = false;
  211. }
  212. }
  213. let itemT = {
  214. id: item[this.valueKey],
  215. name: item[this.textKey],
  216. level,
  217. isLastLevel,
  218. isShow: isShowChild,
  219. isShowChild: false,
  220. checkStatus: initCheckStatus,
  221. orCheckStatus: 0,
  222. parentId,
  223. children: item[this.childrenKey],
  224. childCount: item[this.childrenKey] ? item[this.childrenKey].length : 0,
  225. childCheckCount: 0,
  226. childCheckPCount: 0
  227. };
  228. // console.log(this.selectedData,itemT.id)
  229. if(this.selectedData&&this.selectedData.length){
  230. if (this.selectedData.indexOf(itemT.id) >= 0) {
  231. itemT.checkStatus = 2;
  232. itemT.orCheckStatus = 2;
  233. itemT.childCheckCount = itemT.children ? itemT.children.length : 0;
  234. this._onItemParentSelect(itemT, nextIndex);
  235. }
  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. selectedData() {
  396. this._initTree();
  397. },
  398. localTreeList() {
  399. this.treeList = this.localTreeList;
  400. }
  401. },
  402. mounted() {
  403. this._initTree();
  404. }
  405. }
  406. </script>
  407. <style scoped>
  408. .tree-cover {
  409. position: fixed;
  410. top: 0rpx;
  411. right: 0rpx;
  412. bottom: 0rpx;
  413. left: 0rpx;
  414. z-index: 100;
  415. background-color: rgba(0, 0, 0, .4);
  416. opacity: 0;
  417. transition: all 0.3s ease;
  418. visibility: hidden;
  419. }
  420. .tree-cover.show {
  421. visibility: visible;
  422. opacity: 1;
  423. }
  424. .tree-dialog {
  425. position: fixed;
  426. top: 0rpx;
  427. right: 0rpx;
  428. bottom: 0rpx;
  429. left: 0rpx;
  430. background-color: #fff;
  431. border-top-left-radius: 10px;
  432. border-top-right-radius: 10px;
  433. /* #ifndef APP-NVUE */
  434. display: flex;
  435. /* #endif */
  436. flex-direction: column;
  437. z-index: 100002;
  438. top: 20%;
  439. transition: all 0.3s ease;
  440. transform: translateY(100%);
  441. }
  442. .tree-dialog.show {
  443. transform: translateY(0);
  444. }
  445. .tree-bar {
  446. /* background-color: #fff; */
  447. height: 90rpx;
  448. padding-left: 25rpx;
  449. padding-right: 25rpx;
  450. display: flex;
  451. justify-content: space-between;
  452. align-items: center;
  453. box-sizing: border-box;
  454. border-bottom-width: 1rpx !important;
  455. border-bottom-style: solid;
  456. border-bottom-color: #f5f5f5;
  457. font-size: 32rpx;
  458. color: #757575;
  459. line-height: 1;
  460. }
  461. .tree-bar-confirm {
  462. color: #FE5706;
  463. padding: 15rpx;
  464. }
  465. .tree-bar-title {}
  466. .tree-bar-cancel {
  467. color: #757575;
  468. padding: 15rpx;
  469. }
  470. .tree-view {
  471. flex: 1;
  472. padding: 20rpx;
  473. /* #ifndef APP-NVUE */
  474. display: flex;
  475. /* #endif */
  476. flex-direction: column;
  477. overflow: hidden;
  478. height: 100%;
  479. }
  480. .tree-list {
  481. flex: 1;
  482. height: 100%;
  483. overflow: hidden;
  484. }
  485. .tree-item {
  486. display: flex;
  487. justify-content: space-between;
  488. align-items: center;
  489. line-height: 1;
  490. height: 0;
  491. opacity: 0;
  492. transition: 0.2s;
  493. overflow: hidden;
  494. }
  495. .tree-item.show {
  496. height: 90rpx;
  497. opacity: 1;
  498. }
  499. .tree-item.showchild:before {
  500. transform: rotate(90deg);
  501. }
  502. .tree-item.last:before {
  503. opacity: 0;
  504. }
  505. .switch-on {
  506. width: 0;
  507. height: 0;
  508. border-left: 16rpx solid transparent;
  509. border-right: 16rpx solid transparent;
  510. border-top: 20rpx solid #666;
  511. }
  512. .switch-off {
  513. width: 0;
  514. height: 0;
  515. border-bottom: 16rpx solid transparent;
  516. border-top: 16rpx solid transparent;
  517. border-left: 20rpx solid #666;
  518. }
  519. .item-last-dot {
  520. position: absolute;
  521. width: 12rpx;
  522. height: 12rpx;
  523. border-radius: 100%;
  524. background: #666;
  525. }
  526. .item-icon {
  527. width: 26rpx;
  528. height: 30rpx;
  529. margin-right: 8rpx;
  530. padding-right: 20rpx;
  531. padding-left: 20rpx;
  532. }
  533. .item-label {
  534. flex: 1;
  535. display: flex;
  536. align-items: center;
  537. height: 100%;
  538. line-height: 1.2;
  539. }
  540. .item-name {
  541. flex: 1;
  542. overflow: hidden;
  543. text-overflow: ellipsis;
  544. white-space: nowrap;
  545. /* width: 450rpx; */
  546. font-size: 34rpx;
  547. font-weight: bold;
  548. line-height: 1;
  549. }
  550. .item-check {
  551. width: 40px;
  552. height: 40px;
  553. display: flex;
  554. justify-content: center;
  555. align-items: center;
  556. }
  557. .item-check-yes,
  558. .item-check-no {
  559. width: 32rpx;
  560. height: 32rpx;
  561. border-top-left-radius: 20%;
  562. border-top-right-radius: 20%;
  563. border-bottom-right-radius: 20%;
  564. border-bottom-left-radius: 20%;
  565. border-top-width: 1rpx;
  566. border-left-width: 1rpx;
  567. border-bottom-width: 1rpx;
  568. border-right-width: 1rpx;
  569. border-style: solid;
  570. border-color: #FE5706;
  571. border-radius: 8rpx;
  572. display: flex;
  573. justify-content: center;
  574. align-items: center;
  575. box-sizing: border-box;
  576. /* background-color: #FE5706; */
  577. }
  578. .item-check-yes{
  579. border: none;
  580. background-color: #FE5706;
  581. }
  582. .item-check-yes-part {
  583. width: 24rpx;
  584. height: 6rpx;
  585. border-top-left-radius: 20%;
  586. border-top-right-radius: 20%;
  587. border-bottom-right-radius: 20%;
  588. border-bottom-left-radius: 20%;
  589. background-color: #ffffff;
  590. }
  591. .item-check-yes-all {
  592. margin-bottom: 5px;
  593. border: 2px solid #ffffff;
  594. border-left: 0;
  595. border-top: 0;
  596. height: 12px;
  597. width: 6px;
  598. transform-origin: center;
  599. /* #ifndef APP-NVUE */
  600. transition: all 0.3s;
  601. /* #endif */
  602. transform: rotate(45deg);
  603. /* border: none;
  604. background-color: #FE5706; */
  605. }
  606. .item-check .radio {
  607. border-top-left-radius: 50%;
  608. border-top-right-radius: 50%;
  609. border-bottom-right-radius: 50%;
  610. border-bottom-left-radius: 50%;
  611. }
  612. .item-check .radio .item-check-yes-b {
  613. border-top-left-radius: 50%;
  614. border-top-right-radius: 50%;
  615. border-bottom-right-radius: 50%;
  616. border-bottom-left-radius: 50%;
  617. }
  618. .hover-c {
  619. opacity: 0.6;
  620. }
  621. .itemBorder {
  622. border-bottom: 1px solid #e5e5e5;
  623. }
  624. </style>