xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<template>
  <el-date-picker
    v-model="selectValue"
    type="daterange"
    range-separator="-"
    unlink-panels
    value-format="yyyy-MM-dd"
    :start-placeholder="startPlaceholder"
    :end-placeholder="endPlaceholder"
    :picker-options="pickerOptions"
    @change="onSelected">
  </el-date-picker>
</template>
<script>
 
  export default {
    name: 'ZtDateRangePicker',
    components: {},
    props: {
      value: {
        type: Array,
        default() {
          return []
        }
      },
      start: {// 初始化起始日期
        type: String,
        default: ''
      },
      end: {// 初始化截止日期
        type: String,
        default: ''
      },
      max: { // 可选最大日期
        type: [String, Date],
        default: '2100-01-01'
      },
      min: { // 可选最小日期
        type: [String, Date],
        default: '1979-01-01'
      },
      startPlaceholder: {
        type: String,
        default: '开始日期'
      },
      endPlaceholder: {
        type: String,
        default: '结束日期'
      }
    },
    data() {
      let that = this
      return {
        selectValue: [this.start, this.end],
        pickerOptions: {
          disabledDate(date) {
            let d = date.getTime()
            let start = that.min instanceof Date ? that.min.getTime() : new Date(that.min.replace('/-/g', '/')).getTime()
            let end = that.max instanceof Date ? that.max.getTime() : new Date(that.max.replace('/-/g', '/')).getTime()
            return !(d >= start && d <= end)
          }
        }
      }
    },
    watch: {
      value(val, oldval) {
        this.selectValue = val
      },
      start(val, oldVal) {
        this.selectValue = [val, this.end]
      },
      end(val, oldVal) {
        this.selectValue = [this.start, val]
      }
    },
    mounted() {
    },
    methods: {
      onSelected(data) {
        this.$emit('input', this.selectValue)
        if (this.selectValue && this.selectValue.length > 1) {
          this.$emit('startChange', this.selectValue[0])
          this.$emit('endChange', this.selectValue[1])
        } else {
          this.$emit('startChange', '')
          this.$emit('endChange', '')
        }
      }
    }
  }
</script>
<style lang="scss">
  .el-date-editor--daterange.el-input__inner {
    width: 100%;
  }
  .el-date-editor--daterange .el-input__icon {
    height: auto;
  }
  .el-date-editor .el-range-separator {
    line-height: 25px;
  }
</style>