ba-tree-picker.vue 18 KB

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