ba-tree-picker.vue 17 KB

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