index.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <div class="date-cell">
  3. <van-cell :title="title" is-link :label="selected" @click="clickItem"/>
  4. <van-popup v-model="showPicker" round position="bottom" :close-on-popstate="true" get-container="#app">
  5. <van-datetime-picker
  6. v-bind="$attrs"
  7. show-toolbar
  8. :type="dateType"
  9. :columns="columns"
  10. @cancel="cancelPicker"
  11. @confirm="pickerConfirm"
  12. />
  13. </van-popup>
  14. </div>
  15. </template>
  16. <script>
  17. import dayjs from "dayjs";
  18. export default {
  19. props:{
  20. //默认值
  21. value:{
  22. type: String,
  23. default: null,
  24. },
  25. //标题
  26. title:{
  27. type: String,
  28. default: null,
  29. },
  30. //时间类型,见van-datetime-picker
  31. dateType:{
  32. type: String,
  33. default: 'time',
  34. },
  35. //时间格式 个别模式下不启用
  36. format:{
  37. type: String,
  38. default: 'YYYY-MM-DD',
  39. }
  40. },
  41. data(){
  42. return{
  43. showPicker:false,
  44. selected:null,
  45. columns: [],
  46. }
  47. },
  48. watch:{
  49. value:{
  50. handler (val) {
  51. console.log(val,'val111')
  52. if(!val){
  53. this.selected = null
  54. }else{
  55. this.selected = val;
  56. }
  57. },
  58. immediate: true
  59. }
  60. },
  61. methods:{
  62. cancelPicker(){
  63. this.showPicker = false;
  64. },
  65. pickerConfirm(val){
  66. console.log(val,'val')
  67. if(this.dateType === 'time' || this.dateType === 'year-month' || this.dateType === 'month-day') {
  68. this.selected = val;
  69. }else {
  70. this.selected = dayjs(val).format(this.format) ;
  71. }
  72. this.showPicker = false;
  73. this.$emit('change',this.selected)
  74. },
  75. clickItem(){
  76. this.showPicker = true;
  77. },
  78. },
  79. model:{
  80. prop: 'value',
  81. event: 'change',
  82. }
  83. }
  84. </script>
  85. <style scoped>
  86. </style>