mpvueCityPicker.vue 6.1 KB

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