ba-tree-picker.vue 17 KB

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