ba-tree-picker.vue 15 KB

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