ba-tree-picker.vue 15 KB

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