ba-tree-picker.vue 15 KB

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