ba-tree-picker.vue 16 KB

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