ba-tree-picker.vue 16 KB

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