gui-area-picker.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <gui-popup ref="guipopupforareapicker" position="bottom">
  3. <view class="gap-body gui-bg-white" @tap.stop.prevent="stopfun">
  4. <view class="gap-header gui-flex gui-rows gui-space-between">
  5. <text class="gap-header-btn gui-block-text" :class="[cancelClass]"
  6. @tap="close">{{cancelText}}</text>
  7. <text class="gap-header-btn gui-block-text" :class="[confirmClass]"
  8. style="text-align:right;" @tap="confirm">{{confirmText}}</text>
  9. </view>
  10. <picker-view :indicator-style="indicatorStyle"
  11. class="gap-main" :value="defaultVal" @change="change">
  12. <picker-view-column v-if="level >= 1">
  13. <text class="gap-item gui-block-text" v-for="(item, index) in province"
  14. :key="index">{{item.label}}</text>
  15. </picker-view-column>
  16. <picker-view-column v-if="level >= 2">
  17. <text class="gap-item gui-block-text" v-for="(item, index) in city[defaultVal[0]]"
  18. :key="index">{{item.label}}</text>
  19. </picker-view-column>
  20. <picker-view-column v-if="level >= 3">
  21. <text class="gap-item gui-block-text" v-for="(item, index) in area[defaultVal[0]][defaultVal[1]]"
  22. :key="index">{{item.label}}</text>
  23. </picker-view-column>
  24. </picker-view>
  25. </view>
  26. </gui-popup>
  27. </template>
  28. <script>
  29. import provinceData from '../data/city-data/province.js';
  30. import cityData from '../data/city-data/city.js';
  31. import areaData from '../data/city-data/area.js';
  32. export default{
  33. name : "gui-area-picker",
  34. props : {
  35. cancelText : { type : String, default : '取消' },
  36. cancelClass : { type : String, default : 'gui-color-gray' },
  37. confirmText : { type : String, default : '确定' },
  38. confirmClass : { type : String, default : 'gui-primary-color' },
  39. value : { type : Array , default () { return ['', '', ''] }},
  40. level : { type : Number, default : 3}
  41. },
  42. data() {
  43. return {
  44. indicatorStyle : 'height:35px',
  45. province : provinceData,
  46. city : cityData,
  47. area : areaData,
  48. defaultVal : [0,0,0],
  49. realshow : false
  50. }
  51. },
  52. watch:{
  53. value : function(nv, ov){this.setDefault();},
  54. defaultVal : function(nv, ov){
  55. if(ov[0] != nv[0]){
  56. this.defaultVal.splice(1,1,0);
  57. this.defaultVal.splice(2,1,0);
  58. }else if(ov[1] != nv[1]){
  59. this.defaultVal.splice(2,1,0);
  60. }
  61. }
  62. },
  63. created() {
  64. this.setDefault();
  65. },
  66. methods:{
  67. setDefault:function(){
  68. if(this.value[0] == ''){return ;}
  69. this.$nextTick(() => {
  70. this.defaultVal.splice(0, 1, this.arrayIndexOf(this.province, this.value[0]));
  71. if(this.value[1] == ''){return ;}
  72. this.$nextTick(() => {
  73. this.defaultVal.splice(1,1, this.arrayIndexOf(this.city[this.defaultVal[0]], this.value[1]));
  74. if(this.value[2] == ''){return ;}
  75. this.$nextTick(() => {
  76. this.defaultVal.splice(2,1, this.arrayIndexOf(this.area[this.defaultVal[0]][this.defaultVal[1]], this.value[2]));
  77. })
  78. })
  79. });
  80. },
  81. arrayIndexOf : function(arr, needFind){
  82. var index = 0;
  83. for(let i = 0; i < arr.length; i++){if(arr[i].label == needFind){index = i; return i;}}
  84. return index;
  85. },
  86. change : function (e) {
  87. var res = e.detail.value;
  88. if(!res[0]){res[0] = 0;}
  89. if(!res[1]){res[1] = 0;}
  90. if(!res[2]){res[2] = 0;}
  91. this.defaultVal = res;
  92. },
  93. confirm:function () {
  94. var codes = [
  95. provinceData[this.defaultVal[0]].value,
  96. cityData[this.defaultVal[0]][this.defaultVal[1]].value,
  97. areaData[this.defaultVal[0]][this.defaultVal[1]][this.defaultVal[2]].value ? areaData[this.defaultVal[0]][this.defaultVal[1]][this.defaultVal[2]].value : 0
  98. ];
  99. var names = [
  100. provinceData[this.defaultVal[0]].label,
  101. cityData[this.defaultVal[0]][this.defaultVal[1]].label,
  102. areaData[this.defaultVal[0]][this.defaultVal[1]][this.defaultVal[2]].label ? areaData[this.defaultVal[0]][this.defaultVal[1]][this.defaultVal[2]].label : ''
  103. ];
  104. codes = codes.splice(0, this.level);
  105. names = names.splice(0, this.level);
  106. var res = {codes : codes, names : names, indexs : this.defaultVal};
  107. this.$emit('confirm', res);
  108. this.$refs.guipopupforareapicker.close();
  109. },
  110. open : function () {
  111. this.$refs.guipopupforareapicker.open();
  112. },
  113. close : function () {
  114. this.$refs.guipopupforareapicker.close();
  115. },
  116. stopfun : function(e){e.stopPropagation(); return null;}
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. .gap-body{height:500rpx;}
  122. .gap-header{padding:25rpx;}
  123. .gap-header-btn{width:200rpx; line-height:38rpx; height:38rpx; font-size:28rpx;}
  124. .gap-main{width:750rpx; height:500rpx;}
  125. .gap-item{height:35px; font-size:12px; line-height:35px; overflow:hidden; text-align:center;}
  126. </style>