yearSelector.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <u-popup v-model="show"
  3. mode="bottom"
  4. :mask-close-able="false">
  5. <view style="">
  6. <text style="margin: 30upx;color: #1890FF;float: left;" @click="cancel">{{$i18n.locale=='zh'?'取消':'cancel'}}</text>
  7. <text style="margin: 30upx;color: #1890FF;float: right;" @click="btn">{{$i18n.locale=='zh'?'确定':'sure'}}</text>
  8. <view style="clear: both;"></view>
  9. </view>
  10. <view style="height: 600upx">
  11. <picker-view :indicator-style="'height: 50upx'" :value="initTimeValue" @change="bindChange" class="picker-view">
  12. <picker-view-column>
  13. <view class="item" v-for="(item,index) in startDateList" :key="index">{{item}}{{$i18n.locale=='zh'?'年':'year'}}</view>
  14. </picker-view-column>
  15. <picker-view-column v-if="range">
  16. <view class="item" v-for="(item,index) in endDateList" :key="index">{{item}}{{$i18n.locale=='zh'?'年':'year'}}</view>
  17. </picker-view-column>
  18. </picker-view>
  19. </view>
  20. </u-popup>
  21. </template>
  22. <script>
  23. const date = new Date()
  24. const year = date.getFullYear()
  25. export default {
  26. name: "yearSelector",
  27. props: {
  28. range: {
  29. type: Boolean,
  30. default: true
  31. },
  32. show: {
  33. type: Boolean,
  34. default: false
  35. },
  36. startYear:{
  37. type: String | Number,
  38. default: 1950
  39. },
  40. endYear:{
  41. type: String | Number,
  42. default: 2050
  43. },
  44. defaultTimeValue:{
  45. type: Array,
  46. default: ()=>[2015,2025]
  47. },
  48. },
  49. computed:{
  50. nowYear(){
  51. return new Date().getFullYear()
  52. },
  53. initTimeValue(){
  54. let val0 = this.startDateList.indexOf(this.nowYear)
  55. let val1 = this.endDateList.indexOf(this.nowYear)
  56. if(this.defaultTimeValue){
  57. val0 = this.startDateList.indexOf(this.defaultTimeValue[0])
  58. val1 = this.endDateList.indexOf(this.defaultTimeValue[1])
  59. }
  60. return [val0,val1]
  61. },
  62. startDateList(){
  63. let arr = []
  64. for (let i = this.startYear; i <= this.endYear; i++) {
  65. arr.push(i)
  66. }
  67. return arr
  68. },
  69. endDateList(){
  70. let arr = []
  71. for (let i = this.flag; i <= this.endYear; i++) {
  72. arr.push(i)
  73. }
  74. return arr
  75. }
  76. },
  77. data(){
  78. return{
  79. flag:this.startYear,
  80. timeValue:this.defaultTimeValue,
  81. }
  82. },
  83. methods:{
  84. bindChange(e){
  85. const val = e.detail.value
  86. if(this.startDateList[val[1]] < this.endDateList[val[0]]){
  87. this.timeValue = [this.startDateList[val[1]],this.endDateList[val[0]]]
  88. }else{
  89. this.timeValue = [this.startDateList[val[0]],this.endDateList[val[1]]]
  90. }
  91. // 限制第二列不小于第一列选中值
  92. // this.flag = this.timeValue[0]
  93. },
  94. btn(){
  95. if(this.range){
  96. if(this.timeValue[1] - this.timeValue[0] > 7){
  97. this.$emit('cancel',true)
  98. }else{
  99. this.$emit('getTimeValue',this.timeValue)
  100. }
  101. }else{
  102. this.$emit('getTimeValue',this.timeValue)
  103. }
  104. },
  105. cancel(){
  106. this.$emit('cancel')
  107. }
  108. }
  109. }
  110. </script>
  111. <style scoped>
  112. .picker-view {
  113. width: 750rpx;
  114. height: 600rpx;
  115. margin-top: 20rpx;
  116. }
  117. .item {
  118. height: 50px;
  119. align-items: center;
  120. justify-content: center;
  121. text-align: center;
  122. }
  123. </style>