mpvueCityPicker.vue 6.8 KB

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