mpvueCityPicker.vue 7.1 KB

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