ba-tree-picker.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. this.$emit('select-change', selectedList, selectedNames, rootCategoryLevelId)
  294. return
  295. }
  296. let oldCheckStatus = item.checkStatus
  297. switch (oldCheckStatus) {
  298. case 0:
  299. item.checkStatus = 2
  300. item.childCheckCount = item.childCount
  301. item.childCheckPCount = 0
  302. break
  303. case 1:
  304. case 2:
  305. item.checkStatus = 0
  306. item.childCheckCount = 0
  307. item.childCheckPCount = 0
  308. break
  309. default:
  310. break
  311. }
  312. //子节点 全部选中
  313. this._onItemChildSelect(item, index)
  314. //父节点 选中状态变化
  315. this._onItemParentSelect(item, index, oldCheckStatus)
  316. },
  317. _onItemChildSelect(item, index) {
  318. //console.log('_onItemChildSelect')
  319. let allChildCount = 0
  320. if (item.childCount && item.childCount > 0) {
  321. index++
  322. while (index < this.treeList.length && this.treeList[index].level > item.level) {
  323. let itemChild = this.treeList[index]
  324. itemChild.checkStatus = item.checkStatus
  325. if (itemChild.checkStatus == 2) {
  326. itemChild.childCheckCount = itemChild.childCount
  327. itemChild.childCheckPCount = 0
  328. } else if (itemChild.checkStatus == 0) {
  329. itemChild.childCheckCount = 0
  330. itemChild.childCheckPCount = 0
  331. }
  332. // console.log('>>>>index:', index, 'item:', itemChild.name, ' status:', itemChild
  333. // .checkStatus)
  334. index++
  335. }
  336. }
  337. },
  338. _onItemParentSelect(item, index, oldCheckStatus) {
  339. //console.log('_onItemParentSelect')
  340. //console.log(item)
  341. const parentIndex = this.treeList.findIndex(itemP => itemP.id == item.parentId)
  342. //console.log('parentIndex:' + parentIndex)
  343. if (parentIndex >= 0) {
  344. let itemParent = this.treeList[parentIndex]
  345. let oldCheckStatusParent = itemParent.checkStatus
  346. if (oldCheckStatus == 1) {
  347. itemParent.childCheckPCount -= 1
  348. } else if (oldCheckStatus == 2) {
  349. itemParent.childCheckCount -= 1
  350. }
  351. if (item.checkStatus == 1) {
  352. itemParent.childCheckPCount += 1
  353. } else if (item.checkStatus == 2) {
  354. itemParent.childCheckCount += 1
  355. }
  356. if (itemParent.childCheckCount <= 0 && itemParent.childCheckPCount <= 0) {
  357. itemParent.childCheckCount = 0
  358. itemParent.childCheckPCount = 0
  359. itemParent.checkStatus = 0
  360. } else if (itemParent.childCheckCount >= itemParent.childCount) {
  361. itemParent.childCheckCount = itemParent.childCount
  362. itemParent.childCheckPCount = 0
  363. itemParent.checkStatus = 2
  364. } else {
  365. itemParent.checkStatus = 1
  366. }
  367. //console.log('itemParent:', itemParent)
  368. this._onItemParentSelect(itemParent, parentIndex, oldCheckStatusParent)
  369. }
  370. },
  371. // 重置数据
  372. _reTreeList() {
  373. this.treeList.forEach((v, i) => {
  374. this.treeList[i].checkStatus = v.orCheckStatus
  375. })
  376. },
  377. _initTree() {
  378. this.treeList = []
  379. this._formatTreeData(this.localdata)
  380. }
  381. },
  382. watch: {
  383. localdata() {
  384. this._initTree()
  385. },
  386. localTreeList() {
  387. this.treeList = this.localTreeList
  388. }
  389. },
  390. mounted() {
  391. this._initTree()
  392. }
  393. }
  394. </script>
  395. <style scoped>
  396. .tree-cover {
  397. position: fixed;
  398. top: 0rpx;
  399. right: 0rpx;
  400. bottom: 0rpx;
  401. left: 0rpx;
  402. z-index: 100;
  403. background-color: rgba(0, 0, 0, 0.4);
  404. opacity: 0;
  405. transition: all 0.3s ease;
  406. visibility: hidden;
  407. }
  408. .tree-cover.show {
  409. visibility: visible;
  410. opacity: 1;
  411. }
  412. .tree-dialog {
  413. position: fixed;
  414. top: 0rpx;
  415. right: 0rpx;
  416. bottom: 0rpx;
  417. left: 0rpx;
  418. background-color: #fff;
  419. border-top-left-radius: 10px;
  420. border-top-right-radius: 10px;
  421. /* #ifndef APP-NVUE */
  422. display: flex;
  423. /* #endif */
  424. flex-direction: column;
  425. z-index: 102;
  426. top: 20%;
  427. transition: all 0.3s ease;
  428. transform: translateY(100%);
  429. }
  430. .tree-dialog.show {
  431. transform: translateY(0);
  432. }
  433. .tree-bar {
  434. /* background-color: #fff; */
  435. height: 90rpx;
  436. padding-left: 25rpx;
  437. padding-right: 25rpx;
  438. display: flex;
  439. justify-content: space-between;
  440. align-items: center;
  441. box-sizing: border-box;
  442. border-bottom-width: 1rpx !important;
  443. border-bottom-style: solid;
  444. border-bottom-color: #f5f5f5;
  445. font-size: 32rpx;
  446. color: #757575;
  447. line-height: 1;
  448. }
  449. .tree-bar-confirm {
  450. color: #0055ff;
  451. padding: 15rpx;
  452. }
  453. .tree-bar-title {
  454. }
  455. .tree-bar-cancel {
  456. color: #757575;
  457. padding: 15rpx;
  458. }
  459. .tree-view {
  460. flex: 1;
  461. padding: 20rpx;
  462. /* #ifndef APP-NVUE */
  463. display: flex;
  464. /* #endif */
  465. flex-direction: column;
  466. overflow: hidden;
  467. height: 100%;
  468. }
  469. .tree-list {
  470. flex: 1;
  471. height: 100%;
  472. overflow: hidden;
  473. }
  474. .tree-item {
  475. display: flex;
  476. justify-content: space-between;
  477. align-items: center;
  478. line-height: 1;
  479. height: 0;
  480. opacity: 0;
  481. transition: 0.2s;
  482. overflow: hidden;
  483. }
  484. .tree-item.show {
  485. height: 90rpx;
  486. opacity: 1;
  487. }
  488. .tree-item.showchild:before {
  489. transform: rotate(90deg);
  490. }
  491. .tree-item.last:before {
  492. opacity: 0;
  493. }
  494. .switch-on {
  495. width: 0;
  496. height: 0;
  497. border-left: 10rpx solid transparent;
  498. border-right: 10rpx solid transparent;
  499. border-top: 15rpx solid #666;
  500. }
  501. .switch-off {
  502. width: 0;
  503. height: 0;
  504. border-bottom: 10rpx solid transparent;
  505. border-top: 10rpx solid transparent;
  506. border-left: 15rpx solid #666;
  507. }
  508. .item-last-dot {
  509. position: absolute;
  510. width: 10rpx;
  511. height: 10rpx;
  512. border-radius: 100%;
  513. background: #666;
  514. }
  515. .item-icon {
  516. width: 26rpx;
  517. height: 26rpx;
  518. margin-right: 8rpx;
  519. padding-right: 20rpx;
  520. padding-left: 20rpx;
  521. }
  522. .item-label {
  523. flex: 1;
  524. display: flex;
  525. align-items: center;
  526. height: 100%;
  527. line-height: 1.2;
  528. }
  529. .item-name {
  530. flex: 1;
  531. overflow: hidden;
  532. text-overflow: ellipsis;
  533. white-space: nowrap;
  534. width: 450rpx;
  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: 20px;
  546. height: 20px;
  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: #0055ff;
  557. display: flex;
  558. justify-content: center;
  559. align-items: center;
  560. box-sizing: border-box;
  561. }
  562. .item-check-yes-part {
  563. width: 12px;
  564. height: 12px;
  565. border-top-left-radius: 20%;
  566. border-top-right-radius: 20%;
  567. border-bottom-right-radius: 20%;
  568. border-bottom-left-radius: 20%;
  569. background-color: #0055ff;
  570. }
  571. .item-check-yes-all {
  572. margin-bottom: 5px;
  573. border: 2px solid #007aff;
  574. border-left: 0;
  575. border-top: 0;
  576. height: 12px;
  577. width: 6px;
  578. transform-origin: center;
  579. /* #ifndef APP-NVUE */
  580. transition: all 0.3s;
  581. /* #endif */
  582. transform: rotate(45deg);
  583. }
  584. .item-check .radio {
  585. border-top-left-radius: 50%;
  586. border-top-right-radius: 50%;
  587. border-bottom-right-radius: 50%;
  588. border-bottom-left-radius: 50%;
  589. }
  590. .item-check .radio .item-check-yes-b {
  591. border-top-left-radius: 50%;
  592. border-top-right-radius: 50%;
  593. border-bottom-right-radius: 50%;
  594. border-bottom-left-radius: 50%;
  595. }
  596. .hover-c {
  597. opacity: 0.6;
  598. }
  599. .itemBorder {
  600. border-bottom: 1px solid #e5e5e5;
  601. }
  602. </style>