Picker.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <!-- 时间选择弹窗 -->
  2. <template>
  3. <portal to="notification-outlet">
  4. <div class="toast-wrap" v-if="show">
  5. <div class="content">
  6. <van-picker
  7. :columns="columns"
  8. :visible-item-count='3'
  9. @change="handlechange"
  10. />
  11. <div class="confBtn">
  12. <p class="tip" v-show="showTip">请选择正确的时间</p>
  13. <div @click="getVal">确认</div>
  14. </div>
  15. </div>
  16. <div class="mask" @click="onCancel"></div>
  17. </div>
  18. </portal>
  19. </template>
  20. <script type="text/javascript">
  21. import $ from 'jquery';
  22. const num = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9];
  23. const unit = ['分钟', '小时', '天', '周', '月','年','分钟', '小时', '天', '周', '月','年'];
  24. export default {
  25. name:'Picker',
  26. data() {
  27. return {
  28. columns: [
  29. {
  30. values:num,
  31. className:'column1',
  32. defaultIndex: 10
  33. },
  34. {
  35. values:num,
  36. className:'column2',
  37. defaultIndex: 11
  38. },
  39. {
  40. values:unit,
  41. className:'column3',
  42. defaultIndex: 6
  43. },
  44. ],
  45. showTip:false,
  46. columIndx:[]
  47. }
  48. },
  49. props:['show','picIndex'],
  50. created(){//记录位置
  51. this.columns[0].defaultIndex = this.columIndx[0] = this.picIndex[0] || 10;
  52. this.columns[1].defaultIndex = this.columIndx[1] = this.picIndex[1] || 11;
  53. this.columns[2].defaultIndex = this.columIndx[2] = this.picIndex[2] || 6;
  54. },
  55. methods: {
  56. getVal() {
  57. const val1 = $(".column1 ul .van-picker-column__item--selected")[0].innerText;
  58. const val2 = $(".column2 ul .van-picker-column__item--selected")[0].innerText;
  59. const val3 = $(".column3 ul .van-picker-column__item--selected")[0].innerText;
  60. // 如果选两个0,则提示
  61. if(val1 == 0 && val2 == 0){
  62. this.showTip = true;
  63. return
  64. }
  65. const value = val1 + val2 + val3;
  66. this.$emit("comfirn",value,this.columIndx)
  67. this.columns[0].defaultIndex = this.columIndx[0] || 10;
  68. this.columns[1].defaultIndex = this.columIndx[1] || 11;
  69. this.columns[2].defaultIndex = this.columIndx[2] || 6;
  70. },
  71. onCancel() {
  72. this.$emit("cancel")
  73. this.showTip = false;
  74. },
  75. handlechange(picker, value, colum){//关掉提示
  76. this.showTip = false;
  77. if(colum == 2){
  78. this.columIndx[colum] = unit.lastIndexOf(value[colum]);
  79. }else{
  80. this.columIndx[colum] = num.lastIndexOf(value[colum]);
  81. }
  82. },
  83. resetIndex(){//清除时复原默认index
  84. this.columns[0].defaultIndex = 10;
  85. this.columns[1].defaultIndex = 11;
  86. this.columns[2].defaultIndex = 6;
  87. }
  88. }
  89. };
  90. </script>
  91. <style lang="less" scoped>
  92. @import '../less/base.less';
  93. .toast-wrap{
  94. .content{
  95. padding-top: 25px;
  96. width: 6.3rem;
  97. // height: 4.8rem;
  98. height: 227px;
  99. background: #fff;
  100. z-index: 999;
  101. position: fixed;
  102. top:50%;
  103. left: 50%;
  104. transform: translate(-50%,-50%);
  105. box-sizing: border-box;
  106. border-radius: .2rem;
  107. z-index: 1000;
  108. font-size: .3rem;
  109. overflow-y: hidden;
  110. .confBtn{
  111. height: 44px;
  112. line-height: 44px;
  113. text-align: center;
  114. border-top: 1px solid #ebedf0;
  115. margin-top: 25px;
  116. position: relative;
  117. .tip{
  118. position: absolute;
  119. top:-22px;
  120. font-size: .24rem;
  121. color: red;
  122. line-height: 20px;
  123. width:100%;
  124. text-align: center;
  125. }
  126. div{
  127. color: #colors[theme];
  128. }
  129. }
  130. }
  131. .mask{
  132. .mask;
  133. z-index: 999;
  134. }
  135. }
  136. </style>