ba-tree-picker.vue 15 KB

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