city-picker.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. label: this._getLabel(),
  119. value: this.pickerValue,
  120. cityCode: this._getCityCode()
  121. };
  122. this.$emit(emitName, pickObj);
  123. },
  124. _getLabel() {
  125. let pcikerLabel =
  126. this.provinceDataList[this.pickerValue[0]].label +
  127. '-' +
  128. this.cityDataList[this.pickerValue[1]].label +
  129. '-' +
  130. this.areaDataList[this.pickerValue[2]].label;
  131. return pcikerLabel;
  132. },
  133. _getCityCode() {
  134. return this.areaDataList[this.pickerValue[2]].value;
  135. }
  136. }
  137. };
  138. </script>
  139. <style>
  140. .pickerMask {
  141. position: fixed;
  142. z-index: 1000;
  143. top: 0;
  144. right: 0;
  145. left: 0;
  146. bottom: 0;
  147. background: rgba(0, 0, 0, 0.6);
  148. }
  149. .mpvue-picker-content {
  150. position: fixed;
  151. bottom: 0;
  152. left: 0;
  153. width: 100%;
  154. transition: all 0.3s ease;
  155. transform: translateY(100%);
  156. z-index: 3000;
  157. }
  158. .mpvue-picker-view-show {
  159. transform: translateY(0);
  160. }
  161. .mpvue-picker__hd {
  162. display: flex;
  163. padding: 9px 15px;
  164. background-color: #fff;
  165. position: relative;
  166. text-align: center;
  167. font-size: 17px;
  168. }
  169. .mpvue-picker__hd:after {
  170. content: ' ';
  171. position: absolute;
  172. left: 0;
  173. bottom: 0;
  174. right: 0;
  175. height: 1px;
  176. border-bottom: 1px solid #e5e5e5;
  177. color: #e5e5e5;
  178. transform-origin: 0 100%;
  179. transform: scaleY(0.5);
  180. }
  181. .mpvue-picker__action {
  182. display: block;
  183. flex: 1;
  184. color: #1aad19;
  185. }
  186. .mpvue-picker__action:first-child {
  187. text-align: left;
  188. color: #888;
  189. }
  190. .mpvue-picker__action:last-child {
  191. text-align: right;
  192. }
  193. .picker-item {
  194. text-align: center;
  195. line-height: 40px;
  196. text-overflow: ellipsis;
  197. white-space: nowrap;
  198. font-size: 16px;
  199. }
  200. .mpvue-picker-view {
  201. position: relative;
  202. bottom: 0;
  203. left: 0;
  204. width: 100%;
  205. height: 238px;
  206. background-color: rgba(255, 255, 255, 1);
  207. }
  208. </style>