index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <template>
  2. <view class="mpvue-picker">
  3. <view
  4. :class="{ pickerMask: showPicker }"
  5. @tap="maskClick"
  6. catchtouchmove="true"
  7. ></view>
  8. <view
  9. class="mpvue-picker-content "
  10. :class="{ 'mpvue-picker-view-show': showPicker }"
  11. >
  12. <view class="mpvue-picker__hd" catchtouchmove="true">
  13. <view class="mpvue-picker__action" @tap="pickerCancel">取消</view>
  14. <view
  15. class="mpvue-picker__action"
  16. :style="{ color: themeColor.color }"
  17. @tap="pickerConfirm"
  18. >确定</view
  19. >
  20. </view>
  21. <!-- 单列 -->
  22. <picker-view
  23. indicator-style="height: 40px;"
  24. class="mpvue-picker-view"
  25. :value="pickerValue"
  26. @change="pickerChange"
  27. v-if="mode === 'selector' && pickerValueSingleArray.length > 0"
  28. >
  29. <block>
  30. <picker-view-column>
  31. <view
  32. class="picker-item"
  33. v-for="(item, index) in pickerValueSingleArray"
  34. :key="index"
  35. >{{ item.label }}</view
  36. >
  37. </picker-view-column>
  38. </block>
  39. </picker-view>
  40. <!-- 时间选择器 -->
  41. <picker-view
  42. indicator-style="height: 40px;"
  43. class="mpvue-picker-view"
  44. :value="pickerValue"
  45. @change="pickerChange"
  46. v-if="mode === 'timeSelector'"
  47. >
  48. <block>
  49. <picker-view-column>
  50. <view
  51. class="picker-item"
  52. v-for="(item, index) in pickerValueHour"
  53. :key="index"
  54. >{{ item.label }}</view
  55. >
  56. </picker-view-column>
  57. <picker-view-column>
  58. <view
  59. class="picker-item"
  60. v-for="(item, index) in pickerValueMinute"
  61. :key="index"
  62. >{{ item.label }}</view
  63. >
  64. </picker-view-column>
  65. </block>
  66. </picker-view>
  67. <!-- 多列选择 -->
  68. <picker-view
  69. indicator-style="height: 40px;"
  70. class="mpvue-picker-view"
  71. :value="pickerValue"
  72. @change="pickerChange"
  73. v-if="mode === 'multiSelector'"
  74. >
  75. <block v-for="(n, index) in pickerValueMulArray.length" :key="index">
  76. <picker-view-column>
  77. <view
  78. class="picker-item"
  79. v-for="(item, index1) in pickerValueMulArray[n]"
  80. :key="index1"
  81. >{{ item.label }}</view
  82. >
  83. </picker-view-column>
  84. </block>
  85. </picker-view>
  86. <!-- 二级联动 -->
  87. <picker-view
  88. indicator-style="height: 40px;"
  89. class="mpvue-picker-view"
  90. :value="pickerValue"
  91. @change="pickerChangeMul"
  92. v-if="mode === 'multiLinkageSelector' && deepLength === 2"
  93. >
  94. <block>
  95. <picker-view-column>
  96. <view
  97. class="picker-item"
  98. v-for="(item, index) in pickerValueMulTwoOne"
  99. :key="index"
  100. >{{ item.label }}</view
  101. >
  102. </picker-view-column>
  103. <picker-view-column>
  104. <view
  105. class="picker-item"
  106. v-for="(item, index) in pickerValueMulTwoTwo"
  107. :key="index"
  108. >{{ item.label }}</view
  109. >
  110. </picker-view-column>
  111. </block>
  112. </picker-view>
  113. <!-- 三级联动 -->
  114. <picker-view
  115. indicator-style="height: 40px;"
  116. class="mpvue-picker-view"
  117. :value="pickerValue"
  118. @change="pickerChangeMul"
  119. v-if="mode === 'multiLinkageSelector' && deepLength === 3"
  120. >
  121. <block>
  122. <picker-view-column>
  123. <view
  124. class="picker-item"
  125. v-for="(item, index) in pickerValueMulThreeOne"
  126. :key="index"
  127. >{{ item.label }}</view
  128. >
  129. </picker-view-column>
  130. <picker-view-column>
  131. <view
  132. class="picker-item"
  133. v-for="(item, index) in pickerValueMulThreeTwo"
  134. :key="index"
  135. >{{ item.label }}</view
  136. >
  137. </picker-view-column>
  138. <picker-view-column>
  139. <view
  140. class="picker-item"
  141. v-for="(item, index) in pickerValueMulThreeThree"
  142. :key="index"
  143. >{{ item.label }}</view
  144. >
  145. </picker-view-column>
  146. </block>
  147. </picker-view>
  148. </view>
  149. </view>
  150. </template>
  151. <script>
  152. export default {
  153. data() {
  154. return {
  155. pickerChangeValue: [],
  156. pickerValue: [],
  157. pickerValueArrayChange: true,
  158. modeChange: false,
  159. pickerValueSingleArray: [],
  160. pickerValueHour: [],
  161. pickerValueMinute: [],
  162. pickerValueMulArray: [],
  163. pickerValueMulTwoOne: [],
  164. pickerValueMulTwoTwo: [],
  165. pickerValueMulThreeOne: [],
  166. pickerValueMulThreeTwo: [],
  167. pickerValueMulThreeThree: [],
  168. /* 是否显示控件 */
  169. showPicker: false
  170. };
  171. },
  172. props: {
  173. /* mode */
  174. mode: {
  175. type: String,
  176. default: 'selector'
  177. },
  178. /* picker 数值 */
  179. pickerValueArray: {
  180. type: Array,
  181. default() {
  182. return [];
  183. }
  184. },
  185. /* 默认值 */
  186. pickerValueDefault: {
  187. type: Array,
  188. default() {
  189. return [];
  190. }
  191. },
  192. /* 几级联动 */
  193. deepLength: {
  194. type: Number,
  195. default: 2
  196. }
  197. },
  198. watch: {
  199. mode(oldVal, newVal) {
  200. this.modeChange = true;
  201. },
  202. pickerValueArray(val) {
  203. this.initPicker(val);
  204. }
  205. },
  206. methods: {
  207. initPicker(valueArray) {
  208. let pickerValueArray = valueArray;
  209. this.pickerValue = this.pickerValueDefault;
  210. // 初始化多级联动
  211. if (this.mode === 'selector') {
  212. this.pickerValueSingleArray = valueArray;
  213. } else if (this.mode === 'timeSelector') {
  214. this.modeChange = false;
  215. let hourArray = [];
  216. let minuteArray = [];
  217. for (let i = 0; i < 24; i++) {
  218. hourArray.push({
  219. value: i,
  220. label: i > 9 ? `${i} 时` : `0${i} 时`
  221. });
  222. }
  223. for (let i = 0; i < 60; i++) {
  224. minuteArray.push({
  225. value: i,
  226. label: i > 9 ? `${i} 分` : `0${i} 分`
  227. });
  228. }
  229. this.pickerValueHour = hourArray;
  230. this.pickerValueMinute = minuteArray;
  231. } else if (this.mode === 'multiSelector') {
  232. this.pickerValueMulArray = valueArray;
  233. } else if (
  234. this.mode === 'multiLinkageSelector' &&
  235. this.deepLength === 2
  236. ) {
  237. // 两级联动
  238. let pickerValueMulTwoOne = [];
  239. let pickerValueMulTwoTwo = [];
  240. // 第一列
  241. for (let i = 0, length = pickerValueArray.length; i < length; i++) {
  242. pickerValueMulTwoOne.push(pickerValueArray[i]);
  243. }
  244. // 渲染第二列
  245. // 如果有设定的默认值
  246. if (this.pickerValueDefault.length === 2) {
  247. let num = this.pickerValueDefault[0];
  248. for (
  249. let i = 0, length = pickerValueArray[num].children.length;
  250. i < length;
  251. i++
  252. ) {
  253. pickerValueMulTwoTwo.push(pickerValueArray[num].children[i]);
  254. }
  255. } else {
  256. for (
  257. let i = 0, length = pickerValueArray[0].children.length;
  258. i < length;
  259. i++
  260. ) {
  261. pickerValueMulTwoTwo.push(pickerValueArray[0].children[i]);
  262. }
  263. }
  264. this.pickerValueMulTwoOne = pickerValueMulTwoOne;
  265. this.pickerValueMulTwoTwo = pickerValueMulTwoTwo;
  266. } else if (
  267. this.mode === 'multiLinkageSelector' &&
  268. this.deepLength === 3
  269. ) {
  270. let pickerValueMulThreeOne = [];
  271. let pickerValueMulThreeTwo = [];
  272. let pickerValueMulThreeThree = [];
  273. // 第一列
  274. for (let i = 0, length = pickerValueArray.length; i < length; i++) {
  275. pickerValueMulThreeOne.push(pickerValueArray[i]);
  276. }
  277. // 渲染第二列
  278. this.pickerValueDefault =
  279. this.pickerValueDefault.length === 3
  280. ? this.pickerValueDefault
  281. : [0, 0, 0];
  282. if (this.pickerValueDefault.length === 3) {
  283. let num = this.pickerValueDefault[0];
  284. for (
  285. let i = 0, length = pickerValueArray[num].children.length;
  286. i < length;
  287. i++
  288. ) {
  289. pickerValueMulThreeTwo.push(pickerValueArray[num].children[i]);
  290. }
  291. // 第三列
  292. let numSecond = this.pickerValueDefault[1];
  293. for (
  294. let i = 0,
  295. length =
  296. pickerValueArray[num].children[numSecond].children.length;
  297. i < length;
  298. i++
  299. ) {
  300. pickerValueMulThreeThree.push(
  301. pickerValueArray[num].children[numSecond].children[i]
  302. );
  303. }
  304. }
  305. this.pickerValueMulThreeOne = pickerValueMulThreeOne;
  306. this.pickerValueMulThreeTwo = pickerValueMulThreeTwo;
  307. this.pickerValueMulThreeThree = pickerValueMulThreeThree;
  308. }
  309. },
  310. show() {
  311. setTimeout(() => {
  312. if (this.pickerValueArrayChange || this.modeChange) {
  313. this.initPicker(this.pickerValueArray);
  314. this.showPicker = true;
  315. this.pickerValueArrayChange = false;
  316. this.modeChange = false;
  317. } else {
  318. this.showPicker = true;
  319. }
  320. }, 0);
  321. },
  322. maskClick() {
  323. this.pickerCancel();
  324. },
  325. pickerCancel() {
  326. this.showPicker = false;
  327. this._initPickerVale();
  328. let pickObj = {
  329. index: this.pickerValue,
  330. value: this._getPickerLabelAndValue(this.pickerValue, this.mode).value,
  331. label: this._getPickerLabelAndValue(this.pickerValue, this.mode).label
  332. };
  333. this.$emit('onCancel', pickObj);
  334. },
  335. pickerConfirm(e) {
  336. this.showPicker = false;
  337. this._initPickerVale();
  338. let pickObj = {
  339. index: this.pickerValue,
  340. value: this._getPickerLabelAndValue(this.pickerValue, this.mode).value,
  341. label: this._getPickerLabelAndValue(this.pickerValue, this.mode).label
  342. };
  343. this.$emit('onConfirm', pickObj);
  344. },
  345. showPickerView() {
  346. this.showPicker = true;
  347. },
  348. pickerChange(e) {
  349. this.pickerValue = e.mp.detail.value;
  350. let pickObj = {
  351. index: this.pickerValue,
  352. value: this._getPickerLabelAndValue(this.pickerValue, this.mode).value,
  353. label: this._getPickerLabelAndValue(this.pickerValue, this.mode).label
  354. };
  355. this.$emit('onChange', pickObj);
  356. },
  357. pickerChangeMul(e) {
  358. if (this.deepLength === 2) {
  359. let pickerValueArray = this.pickerValueArray;
  360. let changeValue = e.mp.detail.value;
  361. // 处理第一列滚动
  362. if (changeValue[0] !== this.pickerValue[0]) {
  363. let pickerValueMulTwoTwo = [];
  364. // 第一列滚动第二列数据更新
  365. for (
  366. let i = 0,
  367. length = pickerValueArray[changeValue[0]].children.length;
  368. i < length;
  369. i++
  370. ) {
  371. pickerValueMulTwoTwo.push(
  372. pickerValueArray[changeValue[0]].children[i]
  373. );
  374. }
  375. this.pickerValueMulTwoTwo = pickerValueMulTwoTwo;
  376. // 第二列初始化为 0
  377. changeValue[1] = 0;
  378. }
  379. this.pickerValue = changeValue;
  380. } else if (this.deepLength === 3) {
  381. let pickerValueArray = this.pickerValueArray;
  382. let changeValue = e.mp.detail.value;
  383. let pickerValueMulThreeTwo = [];
  384. let pickerValueMulThreeThree = [];
  385. // 重新渲染第二列
  386. // 如果是第一列滚动
  387. if (changeValue[0] !== this.pickerValue[0]) {
  388. this.pickerValueMulThreeTwo = [];
  389. for (
  390. let i = 0,
  391. length = pickerValueArray[changeValue[0]].children.length;
  392. i < length;
  393. i++
  394. ) {
  395. pickerValueMulThreeTwo.push(
  396. pickerValueArray[changeValue[0]].children[i]
  397. );
  398. }
  399. // 重新渲染第三列
  400. for (
  401. let i = 0,
  402. length =
  403. pickerValueArray[changeValue[0]].children[0].children.length;
  404. i < length;
  405. i++
  406. ) {
  407. pickerValueMulThreeThree.push(
  408. pickerValueArray[changeValue[0]].children[0].children[i]
  409. );
  410. }
  411. changeValue[1] = 0;
  412. changeValue[2] = 0;
  413. this.pickerValueMulThreeTwo = pickerValueMulThreeTwo;
  414. this.pickerValueMulThreeThree = pickerValueMulThreeThree;
  415. } else if (changeValue[1] !== this.pickerValue[1]) {
  416. // 第二列滚动
  417. // 重新渲染第三列
  418. this.pickerValueMulThreeThree = [];
  419. pickerValueMulThreeTwo = this.pickerValueMulThreeTwo;
  420. for (
  421. let i = 0,
  422. length =
  423. pickerValueArray[changeValue[0]].children[changeValue[1]]
  424. .children.length;
  425. i < length;
  426. i++
  427. ) {
  428. pickerValueMulThreeThree.push(
  429. pickerValueArray[changeValue[0]].children[changeValue[1]]
  430. .children[i]
  431. );
  432. }
  433. changeValue[2] = 0;
  434. this.pickerValueMulThreeThree = pickerValueMulThreeThree;
  435. }
  436. this.pickerValue = changeValue;
  437. }
  438. let pickObj = {
  439. index: this.pickerValue,
  440. value: this._getPickerLabelAndValue(this.pickerValue, this.mode).value,
  441. label: this._getPickerLabelAndValue(this.pickerValue, this.mode).label
  442. };
  443. this.$emit('onChange', pickObj);
  444. },
  445. // 获取 pxikerLabel
  446. _getPickerLabelAndValue(value, mode) {
  447. let pickerLable;
  448. let pickerGetValue = [];
  449. // selector
  450. if (mode === 'selector') {
  451. pickerLable = this.pickerValueSingleArray[value].label;
  452. pickerGetValue.push(this.pickerValueSingleArray[value].value);
  453. } else if (mode === 'timeSelector') {
  454. pickerLable = `${this.pickerValueHour[value[0]].label}-${
  455. this.pickerValueMinute[value[1]].label
  456. }`;
  457. pickerGetValue.push(this.pickerValueHour[value[0]].value);
  458. pickerGetValue.push(this.pickerValueHour[value[1]].value);
  459. } else if (mode === 'multiSelector') {
  460. for (let i = 0; i < value.length; i++) {
  461. if (i > 0) {
  462. pickerLable +=
  463. this.pickerValueMulArray[i][value[i]].label +
  464. (i === value.length - 1 ? '' : '-');
  465. } else {
  466. pickerLable = this.pickerValueMulArray[i][value[i]].label + '-';
  467. }
  468. pickerGetValue.push(this.pickerValueMulArray[i][value[i]].value);
  469. }
  470. } else if (mode === 'multiLinkageSelector') {
  471. /* eslint-disable indent */
  472. pickerLable =
  473. this.deepLength === 2
  474. ? `${this.pickerValueMulTwoOne[value[0]].label}-${this.pickerValueMulTwoTwo[value[1]].label}`
  475. : `${this.pickerValueMulThreeOne[value[0]].label}-${this.pickerValueMulThreeTwo[value[1]].label}-${this.pickerValueMulThreeThree[value[2]].label}`;
  476. if (this.deepLength === 2) {
  477. pickerGetValue.push(this.pickerValueMulTwoOne[value[0]].value);
  478. pickerGetValue.push(this.pickerValueMulTwoTwo[value[1]].value);
  479. } else {
  480. pickerGetValue.push(this.pickerValueMulThreeOne[value[0]].value);
  481. pickerGetValue.push(this.pickerValueMulThreeTwo[value[1]].value);
  482. pickerGetValue.push(this.pickerValueMulThreeThree[value[2]].value);
  483. }
  484. /* eslint-enable indent */
  485. }
  486. return {
  487. label: pickerLable,
  488. value: pickerGetValue
  489. };
  490. },
  491. // 初始化 pickerValue 默认值
  492. _initPickerVale() {
  493. if (this.pickerValue.length === 0) {
  494. if (this.mode === 'selector') {
  495. this.pickerValue = [0];
  496. } else if (this.mode === 'multiSelector') {
  497. this.pickerValue = new Int8Array(this.pickerValueArray.length);
  498. } else if (
  499. this.mode === 'multiLinkageSelector' &&
  500. this.deepLength === 2
  501. ) {
  502. this.pickerValue = [0, 0];
  503. } else if (
  504. this.mode === 'multiLinkageSelector' &&
  505. this.deepLength === 3
  506. ) {
  507. this.pickerValue = [0, 0, 0];
  508. }
  509. }
  510. }
  511. }
  512. };
  513. </script>
  514. <style>
  515. .pickerMask {
  516. position: fixed;
  517. z-index: 1000;
  518. top: 0;
  519. right: 0;
  520. left: 0;
  521. bottom: 0;
  522. background: rgba(0, 0, 0, 0.6);
  523. }
  524. .mpvue-picker-content {
  525. position: fixed;
  526. bottom: 0;
  527. left: 0;
  528. width: 100%;
  529. transition: all 0.3s ease;
  530. transform: translateY(100%);
  531. z-index: 3000;
  532. }
  533. .mpvue-picker-view-show {
  534. transform: translateY(0);
  535. }
  536. .mpvue-picker__hd {
  537. display: flex;
  538. padding: 9px 15px;
  539. background-color: #fff;
  540. position: relative;
  541. text-align: center;
  542. font-size: 17px;
  543. }
  544. .mpvue-picker__hd:after {
  545. content: ' ';
  546. position: absolute;
  547. left: 0;
  548. bottom: 0;
  549. right: 0;
  550. height: 1px;
  551. border-bottom: 1px solid #e5e5e5;
  552. color: #e5e5e5;
  553. transform-origin: 0 100%;
  554. transform: scaleY(0.5);
  555. }
  556. .mpvue-picker__action {
  557. display: block;
  558. flex: 1;
  559. color: #1aad19;
  560. }
  561. .mpvue-picker__action:first-child {
  562. text-align: left;
  563. color: #888;
  564. }
  565. .mpvue-picker__action:last-child {
  566. text-align: right;
  567. }
  568. .picker-item {
  569. text-align: center;
  570. line-height: 40px;
  571. font-size: 16px;
  572. }
  573. .mpvue-picker-view {
  574. position: relative;
  575. bottom: 0;
  576. left: 0;
  577. width: 100%;
  578. height: 238px;
  579. background-color: rgba(255, 255, 255, 1);
  580. }
  581. </style>