ba-tree-picker.vue 15 KB

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