ba-tree-picker.vue 15 KB

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