ba-tree-picker.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. children: item[this.childrenKey],
  274. childCount: item[this.childrenKey]
  275. ? item[this.childrenKey].length
  276. : 0,
  277. childCheckCount: 0,
  278. childCheckPCount: 0
  279. }
  280. if (this.selectedData.indexOf(itemT.id) >= 0) {
  281. itemT.checkStatus = 2
  282. itemT.orCheckStatus = 2
  283. itemT.childCheckCount = itemT.children ? itemT.children.length : 0
  284. this._onItemParentSelect(itemT, nextIndex)
  285. }
  286. this.treeList.splice(nextIndex, 0, itemT)
  287. nextIndex++
  288. })
  289. //console.log(this.treeList);
  290. },
  291. // 节点打开、关闭切换
  292. _onItemSwitch (item, index) {
  293. // console.log(item)
  294. //console.log('_itemSwitch')
  295. if (item.isLastLevel === true) {
  296. return
  297. }
  298. item.isShowChild = !item.isShowChild
  299. if (item.children) {
  300. this._formatTreeData(item.children, item.level + 1, item)
  301. item.children = undefined
  302. } else {
  303. this._onItemChildSwitch(item, index)
  304. }
  305. },
  306. _onItemChildSwitch (item, index) {
  307. //console.log('_onItemChildSwitch')
  308. const firstChildIndex = index + 1
  309. if (firstChildIndex > 0)
  310. for (var i = firstChildIndex; i < this.treeList.length; i++) {
  311. let itemChild = this.treeList[i]
  312. if (itemChild.level > item.level) {
  313. if (item.isShowChild) {
  314. if (itemChild.parentId === item.id) {
  315. itemChild.isShow = item.isShowChild
  316. if (!itemChild.isShow) {
  317. itemChild.isShowChild = false
  318. }
  319. }
  320. } else {
  321. itemChild.isShow = item.isShowChild
  322. itemChild.isShowChild = false
  323. }
  324. } else {
  325. return
  326. }
  327. }
  328. },
  329. // 节点选中、取消选中
  330. _onItemSelect (item, index) {
  331. //console.log('_onItemSelect')
  332. //console.log(item)
  333. if (!this.multiple) {
  334. //单选
  335. item.checkStatus = item.checkStatus == 0 ? 2 : 0
  336. this.treeList.forEach((v, i) => {
  337. if (i != index) {
  338. this.treeList[i].checkStatus = 0
  339. } else {
  340. this.treeList[i].checkStatus = 2
  341. }
  342. })
  343. let selectedList = []
  344. let selectedNames
  345. selectedList.push(item.id)
  346. selectedNames = item.name
  347. this._hide()
  348. this.$emit('select-change', selectedList, selectedNames)
  349. return
  350. }
  351. let oldCheckStatus = item.checkStatus
  352. switch (oldCheckStatus) {
  353. case 0:
  354. item.checkStatus = 2
  355. item.childCheckCount = item.childCount
  356. item.childCheckPCount = 0
  357. break
  358. case 1:
  359. case 2:
  360. item.checkStatus = 0
  361. item.childCheckCount = 0
  362. item.childCheckPCount = 0
  363. break
  364. default:
  365. break
  366. }
  367. //子节点 全部选中
  368. this._onItemChildSelect(item, index)
  369. //父节点 选中状态变化
  370. this._onItemParentSelect(item, index, oldCheckStatus)
  371. },
  372. _onItemChildSelect (item, index) {
  373. //console.log('_onItemChildSelect')
  374. let allChildCount = 0
  375. if (item.childCount && item.childCount > 0) {
  376. index++
  377. while (
  378. index < this.treeList.length &&
  379. this.treeList[index].level > item.level
  380. ) {
  381. let itemChild = this.treeList[index]
  382. itemChild.checkStatus = item.checkStatus
  383. if (itemChild.checkStatus == 2) {
  384. itemChild.childCheckCount = itemChild.childCount
  385. itemChild.childCheckPCount = 0
  386. } else if (itemChild.checkStatus == 0) {
  387. itemChild.childCheckCount = 0
  388. itemChild.childCheckPCount = 0
  389. }
  390. // console.log('>>>>index:', index, 'item:', itemChild.name, ' status:', itemChild
  391. // .checkStatus)
  392. index++
  393. }
  394. }
  395. },
  396. _onItemParentSelect (item, index, oldCheckStatus) {
  397. //console.log('_onItemParentSelect')
  398. //console.log(item)
  399. const parentIndex = this.treeList.findIndex(
  400. itemP => itemP.id == item.parentId
  401. )
  402. //console.log('parentIndex:' + parentIndex)
  403. if (parentIndex >= 0) {
  404. let itemParent = this.treeList[parentIndex]
  405. let count = itemParent.childCheckCount
  406. let oldCheckStatusParent = itemParent.checkStatus
  407. if (oldCheckStatus == 1) {
  408. itemParent.childCheckPCount -= 1
  409. } else if (oldCheckStatus == 2) {
  410. itemParent.childCheckCount -= 1
  411. }
  412. if (item.checkStatus == 1) {
  413. itemParent.childCheckPCount += 1
  414. } else if (item.checkStatus == 2) {
  415. itemParent.childCheckCount += 1
  416. }
  417. if (
  418. itemParent.childCheckCount <= 0 &&
  419. itemParent.childCheckPCount <= 0
  420. ) {
  421. itemParent.childCheckCount = 0
  422. itemParent.childCheckPCount = 0
  423. itemParent.checkStatus = 0
  424. } else if (itemParent.childCheckCount >= itemParent.childCount) {
  425. itemParent.childCheckCount = itemParent.childCount
  426. itemParent.childCheckPCount = 0
  427. itemParent.checkStatus = 2
  428. } else {
  429. itemParent.checkStatus = 1
  430. }
  431. //console.log('itemParent:', itemParent)
  432. this._onItemParentSelect(itemParent, parentIndex, oldCheckStatusParent)
  433. }
  434. },
  435. // 重置数据
  436. _reTreeList () {
  437. this.treeList.forEach((v, i) => {
  438. this.treeList[i].checkStatus = v.orCheckStatus
  439. })
  440. },
  441. _initTree () {
  442. this.treeList = []
  443. this._formatTreeData(this.localdata)
  444. }
  445. },
  446. watch: {
  447. localdata () {
  448. this._initTree()
  449. },
  450. localTreeList () {
  451. this.treeList = this.localTreeList
  452. }
  453. },
  454. mounted () {
  455. this._initTree()
  456. }
  457. }
  458. </script>
  459. <style scoped>
  460. .tree-cover {
  461. position: fixed;
  462. top: 0rpx;
  463. right: 0rpx;
  464. bottom: 0rpx;
  465. left: 0rpx;
  466. z-index: 100;
  467. background-color: rgba(0, 0, 0, 0.4);
  468. opacity: 0;
  469. transition: all 0.3s ease;
  470. visibility: hidden;
  471. }
  472. .tree-cover.show {
  473. visibility: visible;
  474. opacity: 1;
  475. }
  476. .tree-dialog {
  477. position: fixed;
  478. top: 0rpx;
  479. right: 0rpx;
  480. bottom: 0rpx;
  481. left: 0rpx;
  482. background-color: #fff;
  483. border-top-left-radius: 10px;
  484. border-top-right-radius: 10px;
  485. /* #ifndef APP-NVUE */
  486. display: flex;
  487. /* #endif */
  488. flex-direction: column;
  489. z-index: 102;
  490. top: 20%;
  491. transition: all 0.3s ease;
  492. transform: translateY(100%);
  493. }
  494. .tree-dialog.show {
  495. transform: translateY(0);
  496. }
  497. .tree-bar {
  498. /* background-color: #fff; */
  499. height: 90rpx;
  500. padding-left: 25rpx;
  501. padding-right: 25rpx;
  502. display: flex;
  503. justify-content: space-between;
  504. align-items: center;
  505. box-sizing: border-box;
  506. border-bottom-width: 1rpx !important;
  507. border-bottom-style: solid;
  508. border-bottom-color: #f5f5f5;
  509. font-size: 32rpx;
  510. color: #757575;
  511. line-height: 1;
  512. }
  513. .tree-bar-confirm {
  514. color: #0055ff;
  515. padding: 15rpx;
  516. }
  517. .tree-bar-title {
  518. }
  519. .tree-bar-cancel {
  520. color: #757575;
  521. padding: 15rpx;
  522. }
  523. .tree-view {
  524. flex: 1;
  525. padding: 20rpx;
  526. /* #ifndef APP-NVUE */
  527. display: flex;
  528. /* #endif */
  529. flex-direction: column;
  530. overflow: hidden;
  531. height: 100%;
  532. }
  533. .tree-list {
  534. flex: 1;
  535. height: 100%;
  536. overflow: hidden;
  537. }
  538. .tree-item {
  539. display: flex;
  540. justify-content: space-between;
  541. align-items: center;
  542. line-height: 1;
  543. height: 0;
  544. opacity: 0;
  545. transition: 0.2s;
  546. overflow: hidden;
  547. }
  548. .tree-item.show {
  549. height: 90rpx;
  550. opacity: 1;
  551. }
  552. .tree-item.showchild:before {
  553. transform: rotate(90deg);
  554. }
  555. .tree-item.last:before {
  556. opacity: 0;
  557. }
  558. .switch-on {
  559. width: 0;
  560. height: 0;
  561. border-left: 10rpx solid transparent;
  562. border-right: 10rpx solid transparent;
  563. border-top: 15rpx solid #666;
  564. }
  565. .switch-off {
  566. width: 0;
  567. height: 0;
  568. border-bottom: 10rpx solid transparent;
  569. border-top: 10rpx solid transparent;
  570. border-left: 15rpx solid #666;
  571. }
  572. .item-last-dot {
  573. position: absolute;
  574. width: 10rpx;
  575. height: 10rpx;
  576. border-radius: 100%;
  577. background: #666;
  578. }
  579. .item-icon {
  580. width: 26rpx;
  581. height: 26rpx;
  582. margin-right: 8rpx;
  583. padding-right: 20rpx;
  584. padding-left: 20rpx;
  585. }
  586. .item-label {
  587. flex: 1;
  588. display: flex;
  589. align-items: center;
  590. height: 100%;
  591. line-height: 1.2;
  592. }
  593. .item-name {
  594. flex: 1;
  595. overflow: hidden;
  596. text-overflow: ellipsis;
  597. white-space: nowrap;
  598. width: 450rpx;
  599. }
  600. .item-check {
  601. width: 40px;
  602. height: 40px;
  603. display: flex;
  604. justify-content: center;
  605. align-items: center;
  606. }
  607. .item-check-yes,
  608. .item-check-no {
  609. width: 20px;
  610. height: 20px;
  611. border-top-left-radius: 20%;
  612. border-top-right-radius: 20%;
  613. border-bottom-right-radius: 20%;
  614. border-bottom-left-radius: 20%;
  615. border-top-width: 1rpx;
  616. border-left-width: 1rpx;
  617. border-bottom-width: 1rpx;
  618. border-right-width: 1rpx;
  619. border-style: solid;
  620. border-color: #0055ff;
  621. display: flex;
  622. justify-content: center;
  623. align-items: center;
  624. box-sizing: border-box;
  625. }
  626. .item-check-yes-part {
  627. width: 12px;
  628. height: 12px;
  629. border-top-left-radius: 20%;
  630. border-top-right-radius: 20%;
  631. border-bottom-right-radius: 20%;
  632. border-bottom-left-radius: 20%;
  633. background-color: #0055ff;
  634. }
  635. .item-check-yes-all {
  636. margin-bottom: 5px;
  637. border: 2px solid #007aff;
  638. border-left: 0;
  639. border-top: 0;
  640. height: 12px;
  641. width: 6px;
  642. transform-origin: center;
  643. /* #ifndef APP-NVUE */
  644. transition: all 0.3s;
  645. /* #endif */
  646. transform: rotate(45deg);
  647. }
  648. .item-check .radio {
  649. border-top-left-radius: 50%;
  650. border-top-right-radius: 50%;
  651. border-bottom-right-radius: 50%;
  652. border-bottom-left-radius: 50%;
  653. }
  654. .item-check .radio .item-check-yes-b {
  655. border-top-left-radius: 50%;
  656. border-top-right-radius: 50%;
  657. border-bottom-right-radius: 50%;
  658. border-bottom-left-radius: 50%;
  659. }
  660. .hover-c {
  661. opacity: 0.6;
  662. }
  663. .itemBorder {
  664. border-bottom: 1px solid #e5e5e5;
  665. }
  666. </style>