selectMorePicker.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <template name="aui-picker">
  2. <view class="aui-picker" v-if="SHOW" :class="{
  3. 'aui-picker-in': FADE==1,
  4. 'aui-picker-out': FADE==0}"
  5. >
  6. <view class="aui-picker-main">
  7. <view class="aui-mask" @click.stop="close"></view>
  8. <view class="aui-picker-header">
  9. <view @click.stop="close" class="aui-picker-close" >取消</view>
  10. <view class="aui-picker-title" v-if="title">{{title}}</view>
  11. <!-- <view class="aui-picker-close iconfont iconclose" ></view> -->
  12. <!-- <view class="aui-picker-sure" @click.stop="getSure" v-if="titflag">确定</view> -->
  13. <view class="aui-picker-sure" @click.stop="getSure" >确定</view>
  14. </view>
  15. <view class="aui-picker-nav">
  16. <view class="aui-picker-navitem"
  17. v-if="nav.length>0"
  18. v-for="(item, index) in nav"
  19. :key="index"
  20. :data-index="index"
  21. :class="[index==navCurrentIndex ? 'active' : '', 'aui-picker-navitem-'+index]"
  22. @click.stop="_changeNav($event)"
  23. >{{item.name}}</view>
  24. <!-- :style="{margin: nav.length>2 ? '0 10px 0 0' : '0 30px 0 0'}" -->
  25. <view class="aui-picker-navitem"
  26. :key="nav.length"
  27. :data-index="nav.length"
  28. :class="[nav.length==navCurrentIndex ? 'active' : '', 'aui-picker-navitem-'+nav.length]"
  29. @click.stop="_changeNav($event)"
  30. >请选择</view>
  31. <!-- :style="{margin: nav.length>2 ? '0 10px 0 0' : '0 30px 0 0'}" -->
  32. <view class="aui-picker-navborder" :style="{left: navBorderLeft+'px'}"></view>
  33. </view>
  34. <view class="aui-picker-content">
  35. <view class="aui-picker-lists">
  36. <view class="aui-picker-list"
  37. v-for="(list, index) in queryItems.length + 1"
  38. :key="index"
  39. :data-index="index"
  40. :class="[index==navCurrentIndex ? 'active' : '']"
  41. >
  42. <view class="aui-picker-list-warp" v-if="index == 0">
  43. <view class="aui-picker-item"
  44. v-for="(item, key) in items"
  45. v-if="item.pid=='0'"
  46. :key="key"
  47. :data-pindex="index"
  48. :data-index="key"
  49. :data-areaid="item.areaId"
  50. :data-pid="item.pid"
  51. :data-name="item.name"
  52. :class="{'active': result.length>index && result[index].areaid==item.areaId}"
  53. :style="{'background': touchConfig.index==key && touchConfig.pindex==index ? touchConfig.style.background : ''}"
  54. @click.stop="_chooseItem($event)"
  55. @touchstart="_btnTouchStart($event)"
  56. @touchmove="_btnTouchEnd($event)"
  57. @touchend="_btnTouchEnd($event)"
  58. >{{item.name}}</view>
  59. </view>
  60. <view class="aui-picker-list-warp" v-else>
  61. <view class="aui-picker-item"
  62. v-for="(item, key) in queryItems[index-1]"
  63. :key="key"
  64. :data-pindex="index"
  65. :data-index="key"
  66. :data-areaid="item.areaId"
  67. :data-pid="item.pid"
  68. :data-name="item.name"
  69. :class="{'active': result.length>index && result[index].areaid==item.areaId}"
  70. :style="{'background': touchConfig.index==key && touchConfig.pindex==index ? touchConfig.style.background : ''}"
  71. @click.stop="_chooseItem($event)"
  72. @touchstart="_btnTouchStart($event)"
  73. @touchmove="_btnTouchEnd($event)"
  74. @touchend="_btnTouchEnd($event)"
  75. >{{item.name}}</view>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import {getarealist} from "@/api/work/addsk.js"
  85. export default {
  86. name: 'aui-picker',
  87. props: {
  88. title: { //标题
  89. type: String,
  90. default: ''
  91. },
  92. titflag:{
  93. type: Boolean,
  94. default: false,
  95. },
  96. layer: { //控制几级联动,默认无限级(跟随数据有无下级)
  97. type: Number,
  98. default: null
  99. },
  100. data: { //数据 如:[{id: '', name: '', children: [{id: '', name: ''}]}]
  101. type: Array,
  102. default (){
  103. return [
  104. // [{id: '', name: '', children: [{id: '', name: ''}]}]
  105. ]
  106. }
  107. }
  108. },
  109. data(){
  110. return {
  111. SHOW: false,
  112. FADE: -1,
  113. nav: [],
  114. items: [],
  115. queryItems: [],
  116. navCurrentIndex: 0,
  117. navBorderLeft: 25,
  118. result: [],
  119. touchConfig: {
  120. index: -1,
  121. pindex: -1,
  122. style: {
  123. color: '#197DE0',
  124. background: '#EFEFEF'
  125. }
  126. },
  127. loadflag:false
  128. }
  129. },
  130. created(){
  131. const _this = this;
  132. },
  133. watch:{
  134. data(){
  135. const _this = this;
  136. const data = _this.data;
  137. _this.items = _this._flatten(data, '0')
  138.     }  
  139.   },
  140. mounted(){
  141. },
  142. methods:{
  143. // 打开
  144. open(e){
  145. const _this = this;
  146. _this.loadflag=false;
  147. if(e==1){
  148. _this.reset(); //打开时重置picker
  149. }
  150. return new Promise(function(resolve, reject){
  151. _this.SHOW = true;
  152. _this.FADE = 1;
  153. resolve();
  154. });
  155. },
  156. // 关闭
  157. close(){
  158. const _this = this;
  159. return new Promise(function(resolve, reject){
  160. _this.FADE = 0;
  161. const _hidetimer = setTimeout(()=>{
  162. _this.SHOW = false;
  163. _this.FADE = -1;
  164. clearTimeout(_hidetimer);
  165. resolve();
  166. },100)
  167. });
  168. },
  169. //重置
  170. reset(){
  171. const _this = this;
  172. _this.queryItems = [];
  173. _this.nav = [];
  174. _this.navBorderLeft = 25;
  175. _this.navCurrentIndex = 0;
  176. _this.result = [];
  177. },
  178. //导航栏切换
  179. _changeNav(e){
  180. const _this = this;
  181. const index = Number(e.currentTarget.dataset.index);
  182. _this.navCurrentIndex = index;
  183. const _el = uni.createSelectorQuery().in(this).select(".aui-picker-navitem-"+index);
  184. _el.boundingClientRect(data => {
  185. _this.navBorderLeft = data.left + 15;
  186. }).exec();
  187. },
  188. //数据选择
  189. _chooseItem(e){
  190. // 加载的时候禁止点击
  191. if(this.loadflag){
  192. return
  193. }
  194. this.loadflag=true;
  195. const _this = this;
  196. const areaid = e.currentTarget.dataset.areaid;
  197. const name = e.currentTarget.dataset.name;
  198. const pid = e.currentTarget.dataset.pid;
  199. const _arr = [];
  200. // 获取新数据
  201. _this.result[_this.navCurrentIndex] = {areaid: areaid, name: name, pid: pid};
  202. // 只选择两级
  203. if(_this.navCurrentIndex==1){
  204. this.loadflag=false;
  205. _this.close().then(()=>{
  206. _this.$emit("callback", {status: 0, data: _this.result});
  207. });
  208. return
  209. }
  210. getarealist(areaid).then(res=>{
  211. this.loadflag=false;
  212. if(res.data.length>0){
  213. if(_this.navCurrentIndex == _this.queryItems.length)
  214. { //选择数据
  215. _this.queryItems.push(res.data);
  216. _this.nav.push({name: name});
  217. }
  218. else
  219. { //重新选择数据
  220. _this.queryItems.splice(_this.navCurrentIndex+1);
  221. _this.nav.splice(_this.navCurrentIndex+1);
  222. _this.queryItems.splice(_this.navCurrentIndex, 1, res.data);
  223. _this.nav.splice(_this.navCurrentIndex, 1, {name: name});
  224. //清空后面的选择
  225. _this.result.splice(Number(_this.navCurrentIndex+1))
  226. }
  227. _this.navCurrentIndex = _this.navCurrentIndex + 1;
  228. const _el = uni.createSelectorQuery().in(this).select(".aui-picker-navitem-"+_this.navCurrentIndex);
  229. setTimeout(()=>{
  230. _el.boundingClientRect(data => {
  231. _this.navBorderLeft = data.left + 15;
  232. }).exec();
  233. },100)
  234. }else{
  235. //无下级数据
  236. _this.close().then(()=>{
  237. _this.$emit("callback", {status: 0, data: _this.result});
  238. });
  239. }
  240. })
  241. // _this.result[_this.navCurrentIndex] = {areaId: areaId, name: name, pid: pid};
  242. // if(
  243. // (!_this._isDefine(_this.layer) && _this._isDefine(_this._deepQuery(_this.data, areaId).children))
  244. // ||
  245. // (_this.navCurrentIndex < (Number(_this.layer) - 1) && _this._isDefine(_this._deepQuery(_this.data, areaId).children))
  246. // )
  247. // { //有下级数据
  248. // _this._deepQuery(_this.data, areaId).children.forEach(function(item, index){
  249. // _arr.push({areaId: item.areaId, name: item.name, pid: id});
  250. // });
  251. // if(_this.navCurrentIndex == _this.queryItems.length)
  252. // { //选择数据
  253. // _this.queryItems.push(_arr);
  254. // _this.nav.push({name: name});
  255. // }
  256. // else
  257. // { //重新选择数据
  258. // _this.queryItems.splice(_this.navCurrentIndex+1, 1);
  259. // _this.nav.splice(_this.navCurrentIndex+1, 1);
  260. // _this.queryItems.splice(_this.navCurrentIndex, 1, _arr);
  261. // _this.nav.splice(_this.navCurrentIndex, 1, {name: name});
  262. // }
  263. // _this.navCurrentIndex = _this.navCurrentIndex + 1;
  264. // const _el = uni.createSelectorQuery().in(this).select(".aui-picker-navitem-"+_this.navCurrentIndex);
  265. // setTimeout(()=>{
  266. // _el.boundingClientRect(data => {
  267. // _this.navBorderLeft = data.left + 20;
  268. // }).exec();
  269. // },100)
  270. // }
  271. // else
  272. // { //无下级数据
  273. // _this.close().then(()=>{
  274. // _this.$emit("callback", {status: 0, data: _this.result});
  275. // });
  276. // }
  277. },
  278. getSure(){
  279. this.close().then(()=>{
  280. this.$emit("callback", {status: 0, data: this.result});
  281. });
  282. },
  283. //递归遍历——将树形结构数据转化为数组格式
  284. _flatten(tree, pid) {
  285. return tree.reduce((arr, {areaId, name, children = []}) =>
  286. arr.concat([{areaId, name, pid}], this._flatten(children, areaId)), [])
  287. },
  288. //根据id查询对应的数据(如查询id=10100对应的对象)
  289. _deepQuery(tree, areaId) {
  290. let isGet = false;
  291. let retNode = null;
  292. function deepSearch(tree, areaId){
  293. for(let i = 0; i < tree.length; i++) {
  294. if(tree[i].children && tree[i].children.length > 0) {
  295. deepSearch(tree[i].children, areaId);
  296. }
  297. if(areaId === tree[i].areaId || isGet) {
  298. isGet||(retNode = tree[i]);
  299. isGet = true;
  300. break;
  301. }
  302. }
  303. }
  304. deepSearch(tree, areaId);
  305. return retNode;
  306. },
  307. /***判断字符串是否为空
  308. @param {string} str 变量
  309. @example: aui.isDefine("变量");
  310. */
  311. _isDefine(str){
  312. if (str==null || str=="" || str=="undefined" || str==undefined || str=="null" || str=="(null)" || str=='NULL' || typeof (str)=='undefined'){
  313. return false;
  314. }else{
  315. str = str + "";
  316. str = str.replace(/\s/g, "");
  317. if (str == ""){return false;}
  318. return true;
  319. }
  320. },
  321. _btnTouchStart(e){
  322. const _this = this,
  323. index = Number(e.currentTarget.dataset.index),
  324. pindex = Number(e.currentTarget.dataset.pindex);
  325. _this.touchConfig.index = index;
  326. _this.touchConfig.pindex = pindex;
  327. },
  328. _btnTouchEnd(e){
  329. const _this = this,
  330. index = Number(e.currentTarget.dataset.index),
  331. pindex = Number(e.currentTarget.dataset.pindex);
  332. _this.touchConfig.index = -1;
  333. _this.touchConfig.pindex = -1;
  334. },
  335. }
  336. }
  337. </script>
  338. <style scoped>
  339. /* ====================
  340. 多级联动弹窗
  341. =====================*/
  342. .aui-picker{
  343. width: 100vw;
  344. height: 100vh;
  345. /* opacity: 0; */
  346. position: fixed;
  347. top: 0;
  348. left: 0;
  349. z-index: 1200;
  350. background: rgba(0,0,0,0.5);
  351. /* display: none; */
  352. }
  353. .aui-picker.aui-picker-in{
  354. -moz-animation: aui-fade-in .1s ease-out forwards;
  355. -ms-animation: aui-fade-in .1s ease-out forwards;
  356. -webkit-animation: aui-fade-in .1s ease-out forwards;
  357. animation: aui-fade-in .1s ease-out forwards;
  358. }
  359. .aui-picker.aui-picker-out{
  360. -moz-animation: aui-fade-out .1s ease-out forwards;
  361. -ms-animation: aui-fade-out .1s ease-out forwards;
  362. -webkit-animation: aui-fade-out .1s ease-out forwards;
  363. animation: aui-fade-out .1s ease-out forwards;
  364. }
  365. .aui-picker-main{
  366. width: 100vw;
  367. height: 50vh;
  368. background: #FFF;
  369. border-radius: 15px 15px 0 0;
  370. position: absolute;
  371. left: 0px;
  372. /* bottom: -50vh; */
  373. bottom: 0vh;
  374. z-index: 999;
  375. }
  376. .aui-picker.aui-picker-in .aui-picker-main{
  377. -moz-animation: aui-slide-up-screen .2s ease-out forwards;
  378. -ms-animation: aui-slide-up-screen .2s ease-out forwards;
  379. -webkit-animation: aui-slide-up-screen .2s ease-out forwards;
  380. animation: aui-slide-up-screen .2s ease-out forwards;
  381. }
  382. .aui-picker.aui-picker-out .aui-picker-main{
  383. -moz-animation: aui-slide-down-screen .2s ease-out forwards;
  384. -ms-animation: aui-slide-down-screen .2s ease-out forwards;
  385. -webkit-animation: aui-slide-down-screen .2s ease-out forwards;
  386. animation: aui-slide-down-screen .2s ease-out forwards;
  387. }
  388. .aui-picker-header{
  389. width: 100%;
  390. height: 50px;
  391. position: relative;
  392. z-index: 999;
  393. background: #F2F2F2;
  394. border-radius: 15px 15px 0 0;
  395. display: flex;
  396. align-items: center;
  397. }
  398. .aui-picker-header::after{
  399. content: '';
  400. width: 100%;
  401. height: 1px;
  402. background: rgba(100,100,100,.3);
  403. -moz-transform: scaleY(.3);
  404. -ms-transform: scaleY(.3);
  405. -webkit-transform: scaleY(.3);
  406. transform: scaleY(.3);
  407. position: absolute;
  408. left: 0;
  409. bottom: 0;
  410. z-index: 999;
  411. }
  412. .aui-picker-title{
  413. line-height: 20px;
  414. text-align: center;
  415. font-size: 17px;
  416. color: #333;
  417. padding: 15px;
  418. box-sizing: border-box;
  419. flex: 1;
  420. text-align: center;
  421. /* position: absolute;
  422. left: 50px;
  423. right: 50px;
  424. top: 0; */
  425. }
  426. .aui-picker-close{
  427. font-size: 15px;
  428. color: #333333;
  429. flex: 0 0 auto;
  430. height: 50px;
  431. width: 60px;
  432. text-align: center;
  433. line-height: 50px;
  434. }
  435. .aui-picker-sure{
  436. flex: 0 0 auto;
  437. font-size: 15px;
  438. color: #197DE0;
  439. height: 50px;
  440. width: 60px;
  441. text-align: center;
  442. line-height: 50px;
  443. }
  444. /* .aui-picker-close.iconfont{
  445. width: 50px;
  446. height: 50px;
  447. line-height: 50px;
  448. text-align: center;
  449. font-size: 20px;
  450. color: #aaa;
  451. border-radius: 0 10px 0 0;
  452. position: absolute;
  453. right: 0;
  454. top: 0;
  455. } */
  456. .aui-picker-content{
  457. width: 100%;
  458. height: -webkit-calc(100% - 100px);
  459. height: calc(100% - 100px);
  460. }
  461. .aui-picker-nav{
  462. width: 100%;
  463. height: 50px;
  464. text-align: left;
  465. padding: 0 20rpx;
  466. margin: 0 0 1px 0;
  467. justify-content: flex-start;
  468. white-space: nowrap;
  469. box-sizing: border-box;
  470. position: relative;
  471. display: flex;
  472. align-items: center;
  473. }
  474. .aui-picker-nav::after{
  475. content: '';
  476. width: 100%;
  477. height: 1px;
  478. background: rgba(100,100,100,.3);
  479. -moz-transform: scaleY(.3);
  480. -ms-transform: scaleY(.3);
  481. -webkit-transform: scaleY(.3);
  482. transform: scaleY(.3);
  483. position: absolute;
  484. left: 0;
  485. bottom: 0;
  486. z-index: 999;
  487. }
  488. .aui-picker-navitem{
  489. flex: 1;
  490. max-width:70px ;
  491. /* width: 80px; */
  492. line-height: 50px;
  493. font-size: 16px;
  494. text-align: center;
  495. display: inline-block;
  496. overflow: hidden;
  497. white-space: nowrap;
  498. text-overflow: ellipsis;
  499. }
  500. .aui-picker-navitem.active{
  501. color: #197DE0;
  502. }
  503. .aui-picker-navborder{
  504. width: 40px;
  505. height: 3px;
  506. background: #197DE0;
  507. border-radius: 5px;
  508. transition: left .15s;
  509. position: absolute;
  510. left: 25px;
  511. bottom: 0;
  512. }
  513. .aui-picker-lists{
  514. width: 100%;
  515. height: 100%;
  516. justify-content: space-around;
  517. white-space: nowrap;
  518. }
  519. .aui-picker-list{
  520. width: 100%;
  521. height: 100%;
  522. overflow: hidden;
  523. overflow-y: scroll;
  524. display: none;
  525. vertical-align: top;
  526. }
  527. .aui-picker-list.active{
  528. display: inline-block;
  529. }
  530. .aui-picker-list-warp{
  531. width: 100%;
  532. height: auto;
  533. box-sizing: border-box;
  534. padding: 15px 0;
  535. display: inline-block;
  536. }
  537. .aui-picker-item{
  538. width: 100%;
  539. height: 50px;
  540. line-height: 50px;
  541. padding: 0 15px;
  542. box-sizing: border-box;
  543. font-size: 15px;
  544. color: #333;
  545. position: relative;
  546. }
  547. .aui-picker-item.active{
  548. color: #197DE0;
  549. }
  550. .aui-picker-item.active::after{
  551. content: '✔';
  552. font-size: 15px;
  553. color: #197DE0;
  554. position: absolute;
  555. top: 0px;
  556. right: 10px;
  557. }
  558. </style>