mpvueAgePicker.vue 6.8 KB

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