selectnoreMorePicker.vue 15 KB

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