mpvueCityPicker.vue 6.0 KB

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