mpvueCityPicker.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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: [],
  34. cityDataList: [],
  35. areaDataList: [],
  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. this.provinceDataList = provinceData;
  63. this.cityDataList = cityData[this.pickerValueDefault[0]];
  64. this.areaDataList = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]];
  65. this.pickerValue = this.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. if (this.pickerValueDefault !== [0, 0, 0]) {
  88. if (this.pickerValueDefault[0] > provinceData.length - 1) {
  89. this.pickerValueDefault[0] = provinceData.length - 1;
  90. }
  91. if (this.pickerValueDefault[1] > cityData[this.pickerValueDefault[0]].length - 1) {
  92. this.pickerValueDefault[1] = cityData[this.pickerValueDefault[0]].length - 1;
  93. }
  94. if (this.pickerValueDefault[2] > areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1) {
  95. this.pickerValueDefault[2] = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1;
  96. }
  97. }
  98. },
  99. pickerChange(e) {
  100. let changePickerValue = e.mp.detail.value;
  101. if (this.pickerValue[0] !== changePickerValue[0]) {
  102. // 第一级发生滚动
  103. this.cityDataList = cityData[changePickerValue[0]];
  104. this.areaDataList = areaData[changePickerValue[0]][0];
  105. changePickerValue[1] = 0;
  106. changePickerValue[2] = 0;
  107. } else if (this.pickerValue[1] !== changePickerValue[1]) {
  108. // 第二级滚动
  109. this.areaDataList =
  110. areaData[changePickerValue[0]][changePickerValue[1]];
  111. changePickerValue[2] = 0;
  112. }
  113. this.pickerValue = changePickerValue;
  114. this._$emit('onChange');
  115. },
  116. _$emit(emitName) {
  117. let pickObj = {
  118. name:this._getLabel(),
  119. province: this.provinceDataList[this.pickerValue[0]].label,
  120. city:this.cityDataList[this.pickerValue[1]].label,
  121. region:this.areaDataList[this.pickerValue[2]].label,
  122. value: this.pickerValue,
  123. cityCode: this._getCityCode()
  124. };
  125. this.$emit(emitName, pickObj);
  126. },
  127. _getLabel() {
  128. let pcikerLabel =
  129. this.provinceDataList[this.pickerValue[0]].label +
  130. '-' +
  131. this.cityDataList[this.pickerValue[1]].label +
  132. '-' +
  133. this.areaDataList[this.pickerValue[2]].label;
  134. return pcikerLabel;
  135. },
  136. _getCityCode() {
  137. return this.areaDataList[this.pickerValue[2]].value;
  138. }
  139. }
  140. };
  141. </script>
  142. <style>
  143. .pickerMask {
  144. position: fixed;
  145. z-index: 1000;
  146. top: 0;
  147. right: 0;
  148. left: 0;
  149. bottom: 0;
  150. background: rgba(0, 0, 0, 0.6);
  151. }
  152. .mpvue-picker-content {
  153. position: fixed;
  154. bottom: 0;
  155. left: 0;
  156. width: 100%;
  157. transition: all 0.3s ease;
  158. transform: translateY(100%);
  159. z-index: 3000;
  160. }
  161. .mpvue-picker-view-show {
  162. transform: translateY(0);
  163. }
  164. .mpvue-picker__hd {
  165. display: flex;
  166. padding: 9px 15px;
  167. background-color: #fff;
  168. position: relative;
  169. text-align: center;
  170. font-size: 17px;
  171. }
  172. .mpvue-picker__hd:after {
  173. content: ' ';
  174. position: absolute;
  175. left: 0;
  176. bottom: 0;
  177. right: 0;
  178. height: 1px;
  179. border-bottom: 1px solid #e5e5e5;
  180. color: #e5e5e5;
  181. transform-origin: 0 100%;
  182. transform: scaleY(0.5);
  183. }
  184. .mpvue-picker__action {
  185. display: block;
  186. flex: 1;
  187. color: #1aad19;
  188. }
  189. .mpvue-picker__action:first-child {
  190. text-align: left;
  191. color: #888;
  192. }
  193. .mpvue-picker__action:last-child {
  194. text-align: right;
  195. }
  196. .picker-item {
  197. text-align: center;
  198. line-height: 40px;
  199. text-overflow: ellipsis;
  200. white-space: nowrap;
  201. font-size: 16px;
  202. }
  203. .mpvue-picker-view {
  204. position: relative;
  205. bottom: 0;
  206. left: 0;
  207. width: 100%;
  208. height: 238px;
  209. background-color: rgba(255, 255, 255, 1);
  210. }
  211. </style>