mpvueCityPicker.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div class="mpvue-picker">
  3. <div :class="{'pickerMask':showPicker}" @click="maskClick" catchtouchmove="true"></div>
  4. <div class="mpvue-picker-content " :class="{'mpvue-picker-view-show':showPicker}">
  5. <div class="mpvue-picker__hd" catchtouchmove="true">
  6. <div class="mpvue-picker__action" @click="pickerCancel">取消</div>
  7. <div class="mpvue-picker__action" :style="{color:themeColor}" @click="pickerConfirm">确定</div>
  8. </div>
  9. <picker-view indicator-style="height: 40px;" class="mpvue-picker-view" :value="pickerValue" @change="pickerChange">
  10. <block>
  11. <picker-view-column>
  12. <div class="picker-item" v-for="(item,index) in provinceDataList" :key="index">{{item.label}}</div>
  13. </picker-view-column>
  14. <picker-view-column>
  15. <div class="picker-item" v-for="(item,index) in cityDataList" :key="index">{{item.label}}</div>
  16. </picker-view-column>
  17. <picker-view-column>
  18. <div class="picker-item" v-for="(item,index) in areaDataList" :key="index">{{item.label}}</div>
  19. </picker-view-column>
  20. </block>
  21. </picker-view>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. import provinceData from './city-data/province.js';
  27. import cityData from './city-data/city.js';
  28. import areaData from './city-data/area.js';
  29. export default {
  30. data() {
  31. return {
  32. pickerValue: [0, 0, 0],
  33. provinceDataList: provinceData,
  34. cityDataList: cityData[0],
  35. areaDataList: areaData[0][0],
  36. /* 是否显示控件 */
  37. showPicker: false,
  38. };
  39. },
  40. created() {
  41. this.init()
  42. },
  43. props: {
  44. /* 默认值 */
  45. pickerValueDefault: {
  46. type: Array,
  47. default () {
  48. return [0, 0, 0]
  49. }
  50. },
  51. /* 主题色 */
  52. themeColor: String
  53. },
  54. watch: {
  55. pickerValueDefault() {
  56. this.init();
  57. }
  58. },
  59. methods: {
  60. init() {
  61. this.handPickValueDefault(); // 对 pickerValueDefault 做兼容处理
  62. const pickerValueDefault = this.pickerValueDefault
  63. this.cityDataList = cityData[pickerValueDefault[0]];
  64. this.areaDataList = areaData[pickerValueDefault[0]][pickerValueDefault[1]];
  65. this.pickerValue = pickerValueDefault;
  66. },
  67. show() {
  68. setTimeout(() => {
  69. this.showPicker = true;
  70. }, 0);
  71. },
  72. maskClick() {
  73. this.pickerCancel();
  74. },
  75. pickerCancel() {
  76. this.showPicker = false;
  77. this._$emit('onCancel');
  78. },
  79. pickerConfirm(e) {
  80. this.showPicker = false;
  81. this._$emit('onConfirm');
  82. },
  83. showPickerView() {
  84. this.showPicker = true;
  85. },
  86. handPickValueDefault() {
  87. const pickerValueDefault = this.pickerValueDefault
  88. let provinceIndex = pickerValueDefault[0]
  89. let cityIndex = pickerValueDefault[1]
  90. const areaIndex = pickerValueDefault[2]
  91. if (
  92. provinceIndex !== 0 ||
  93. cityIndex !== 0 ||
  94. areaIndex !== 0
  95. ) {
  96. if (provinceIndex > provinceData.length - 1) {
  97. this.pickerValueDefault[0] = provinceIndex = provinceData.length - 1;
  98. }
  99. if (cityIndex > cityData[provinceIndex].length - 1) {
  100. this.pickerValueDefault[1] = cityIndex = cityData[provinceIndex].length - 1;
  101. }
  102. if (areaIndex > areaData[provinceIndex][cityIndex].length - 1) {
  103. this.pickerValueDefault[2] = areaData[provinceIndex][cityIndex].length - 1;
  104. }
  105. }
  106. },
  107. pickerChange(e) {
  108. let changePickerValue = e.mp.detail.value;
  109. if (this.pickerValue[0] !== changePickerValue[0]) {
  110. // 第一级发生滚动
  111. this.cityDataList = cityData[changePickerValue[0]];
  112. this.areaDataList = areaData[changePickerValue[0]][0];
  113. changePickerValue[1] = 0;
  114. changePickerValue[2] = 0;
  115. } else if (this.pickerValue[1] !== changePickerValue[1]) {
  116. // 第二级滚动
  117. this.areaDataList =
  118. areaData[changePickerValue[0]][changePickerValue[1]];
  119. changePickerValue[2] = 0;
  120. }
  121. this.pickerValue = changePickerValue;
  122. this._$emit('onChange');
  123. },
  124. _$emit(emitName) {
  125. let pickObj = {
  126. label: this._getLabel(),
  127. value: this.pickerValue,
  128. cityCode: this._getCityCode()
  129. };
  130. this.$emit(emitName, pickObj);
  131. },
  132. _getLabel() {
  133. let pcikerLabel =
  134. this.provinceDataList[this.pickerValue[0]].label +
  135. '-' +
  136. this.cityDataList[this.pickerValue[1]].label +
  137. '-' +
  138. this.areaDataList[this.pickerValue[2]].label;
  139. return pcikerLabel;
  140. },
  141. _getCityCode() {
  142. return this.areaDataList[this.pickerValue[2]].value;
  143. }
  144. }
  145. };
  146. </script>
  147. <style>
  148. .pickerMask {
  149. position: fixed;
  150. z-index: 1000;
  151. top: 0;
  152. right: 0;
  153. left: 0;
  154. bottom: 0;
  155. background: rgba(0, 0, 0, 0.6);
  156. }
  157. .mpvue-picker-content {
  158. position: fixed;
  159. bottom: 0;
  160. left: 0;
  161. width: 100%;
  162. transition: all 0.3s ease;
  163. transform: translateY(100%);
  164. z-index: 3000;
  165. }
  166. .mpvue-picker-view-show {
  167. transform: translateY(0);
  168. }
  169. .mpvue-picker__hd {
  170. display: flex;
  171. padding: 9px 15px;
  172. background-color: #fff;
  173. position: relative;
  174. text-align: center;
  175. font-size: 17px;
  176. }
  177. .mpvue-picker__hd:after {
  178. content: ' ';
  179. position: absolute;
  180. left: 0;
  181. bottom: 0;
  182. right: 0;
  183. height: 1px;
  184. border-bottom: 1px solid #e5e5e5;
  185. color: #e5e5e5;
  186. transform-origin: 0 100%;
  187. transform: scaleY(0.5);
  188. }
  189. .mpvue-picker__action {
  190. display: block;
  191. flex: 1;
  192. color: #1aad19;
  193. }
  194. .mpvue-picker__action:first-child {
  195. text-align: left;
  196. color: #888;
  197. }
  198. .mpvue-picker__action:last-child {
  199. text-align: right;
  200. }
  201. .picker-item {
  202. text-align: center;
  203. line-height: 40px;
  204. text-overflow: ellipsis;
  205. white-space: nowrap;
  206. font-size: 16px;
  207. }
  208. .mpvue-picker-view {
  209. position: relative;
  210. bottom: 0;
  211. left: 0;
  212. width: 100%;
  213. height: 238px;
  214. background-color: rgba(255, 255, 255, 1);
  215. }
  216. </style>