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