picker3.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="mpvue-picker">
  3. <view class="pickerMask" @click="maskClick" catchtouchmove="true"></view>
  4. <view class="mpvue-picker-content mpvue-picker-view-show">
  5. <view class="mpvue-picker__hd" catchtouchmove="true">
  6. <view class="mpvue-picker__action" @click="pickerCancel">取消</view>
  7. <view class="mpvue-picker__action" @click="pickerConfirm">确定</view>
  8. </view>
  9. <picker-view indicator-style="height: 40px;" class="mpvue-picker-view" :value="pickerValue" @change="pickerChangeMul" @pickend="pickend">
  10. <picker-view-column v-for="(item, index) in arrList" :key="index">
  11. <view class="picker-item" v-for="(i, idx) in item" :key="idx">{{i.label}}
  12. </view>
  13. </picker-view-column>
  14. </picker-view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. pickerValue: this.pickerValueDefault,
  23. arrList: [],
  24. pickerChange: []
  25. };
  26. },
  27. props: {
  28. /* picker 数值 */
  29. pickerValueArray: {
  30. type: Array,
  31. default () {
  32. return []
  33. }
  34. },
  35. /* 默认值 */
  36. pickerValueDefault: {
  37. type: Array,
  38. default () {
  39. return []
  40. }
  41. },
  42. },
  43. mounted() {
  44. this.disbandChildren(this.pickerValueArray, 0);
  45. // 根据几个选项设置集合默认值0
  46. this.arrList.forEach(e => {
  47. this.pickerValue.push(0)
  48. })
  49. },
  50. methods: {
  51. // 拆解数组
  52. disbandChildren(items, index) {
  53. this.$set(this.arrList, index, [])
  54. this.arrList[index].push(...items);
  55. for (let i in items) {
  56. if (items[i].children) {
  57. this.disbandChildren(items[i].children, index + 1);
  58. return;
  59. }
  60. return;
  61. }
  62. },
  63. // 取消
  64. pickerCancel() {
  65. this.$emit('close')
  66. },
  67. // 确认
  68. pickerConfirm() {
  69. const list = [];
  70. for (var i = 0; i < this.pickerValue.length; i++) {
  71. const dataIndex = this.pickerValue[i];
  72. list.push(this.arrList[i][dataIndex])
  73. }
  74. this.$emit('confirm', list);
  75. },
  76. // 滚动改变
  77. pickerChangeMul(e) {
  78. this.pickerChange = e.detail.value;
  79. },
  80. // 遮罩点击
  81. maskClick() {
  82. this.$emit('close')
  83. },
  84. // 滚动给结束
  85. pickend() {
  86. let index = null;
  87. let data = null;
  88. for (var i = 0; i < this.pickerChange.length; i++) {
  89. if (this.pickerChange[i] !== this.pickerValue[i]) {
  90. index = i;
  91. data = this.pickerChange[i]
  92. }
  93. }
  94. if (!index && !data) return;
  95. this.pickerValue = this.pickerChange;
  96. const item = this.arrList[index][data]
  97. if (this.arrList.length <= index + 1) return;
  98. for (var i = 0; i < this.pickerValue.length; i++) {
  99. if (i > index) this.pickerValue[i] = 0;
  100. }
  101. this.disbandChildren(item.children, index + 1);
  102. }
  103. }
  104. };
  105. </script>
  106. <style>
  107. .pickerMask {
  108. position: fixed;
  109. z-index: 1000;
  110. top: 0;
  111. right: 0;
  112. left: 0;
  113. bottom: 0;
  114. background: rgba(0, 0, 0, 0.6);
  115. }
  116. .mpvue-picker-content {
  117. position: fixed;
  118. bottom: 0;
  119. left: 0;
  120. width: 100%;
  121. transition: all 0.3s ease;
  122. transform: translateY(100%);
  123. z-index: 3000;
  124. }
  125. .mpvue-picker-view-show {
  126. transform: translateY(0);
  127. }
  128. .mpvue-picker__hd {
  129. display: flex;
  130. padding: 9px 15px;
  131. background-color: #fff;
  132. position: relative;
  133. text-align: center;
  134. font-size: 17px;
  135. }
  136. .mpvue-picker__hd:after {
  137. content: ' ';
  138. position: absolute;
  139. left: 0;
  140. bottom: 0;
  141. right: 0;
  142. height: 1px;
  143. border-bottom: 1px solid #e5e5e5;
  144. color: #e5e5e5;
  145. transform-origin: 0 100%;
  146. transform: scaleY(0.5);
  147. }
  148. .mpvue-picker__action {
  149. display: block;
  150. flex: 1;
  151. color: #1aad19;
  152. }
  153. .mpvue-picker__action:first-child {
  154. text-align: left;
  155. color: #888;
  156. }
  157. .mpvue-picker__action:last-child {
  158. text-align: right;
  159. }
  160. .picker-item {
  161. text-align: center;
  162. line-height: 40px;
  163. font-size: 16px;
  164. }
  165. .mpvue-picker-view {
  166. position: relative;
  167. bottom: 0;
  168. left: 0;
  169. width: 100%;
  170. height: 238px;
  171. background-color: rgba(255, 255, 255, 1);
  172. }
  173. </style>