ba-tree-picker.vue 18 KB

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