ba-tree-picker.vue 17 KB

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