web
4 天以前 8f50c8203c8df64dd0a566b228bae7f69156e109
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<script setup>
import {onMounted, ref, watch} from 'vue'
 
const props = defineProps({
    valueArr: {
        default: () => {
            return ['01-03', '04-06', '07-09', '10-12'];
        },
        type: Array,
    },
    getValue: {
        default: () => { },
        type: Function,
    },
    // 传入显示的时间
    defaultValue: {
        default: "",
        type: String,
    },
    limitTime: {
        type: String,
        default: ''
    },
    clearable: {
        type: Boolean,
        default: false
    },
    placeholder: {
        type: String,
        default: ''
    }
})
const emit = defineEmits(['change'])
 
const showSeason = ref(false)
const showValue = ref('')
const year =  ref(new Date().getFullYear())
const season = ref()
 
watch(() => props.defaultValue, (value) => {
    initDefaultValue(value)
})
 
const prev = () => {
    year.value = year.value * 1 - 1;
}
const next = () => {
    // 如果有时间限制的话会进行判断
    if (props.limitTime == "") {
        year.value =  year.value * 1 + 1;
    } else if (props.limitTime != "" &&  year.value < props.limitTime) {
        year.value =  year.value * 1 + 1;
    }
}
 
const whitchQuarter = (month) => {
    let quarter = "";
    month = Number(month);
    switch (month) {
        case 1:
        case 2:
        case 3:
            quarter = "1";
            break;
        case 4:
        case 5:
        case 6:
            quarter = "2";
            break;
        case 7:
        case 8:
        case 9:
            quarter = "3";
            break;
        case 10:
        case 11:
        case 12:
            quarter = "4";
            break;
        default:
            console.error("The entered time is incorrect");
    }
    return quarter;
}
 
// 季度时间判定
const InitialTime = (val) => {
    let num = "";
    val = Number(val);
    switch (val) {
        case 1:
            num = "01";
            break;
        case 2:
            num = "04";
            break;
        case 3:
            num = "07";
            break;
        case 4:
            num = "10";
            break;
        default:
            console.error("时间格式有误!");
    }
    return num;
}
 
const selectSeason = (i) => {
    season.value = i + 1;
    let arr = props.valueArr[i].split("-");
    props.getValue(year.value + arr[0] + "-" + year.value + arr[1]);
    showSeason.value = false;
    showValue.value = `${year.value}年${season.value}季度`;
    var formatValue = `${year.value}-${InitialTime(season.value)}`;
    emit("change", formatValue);
}
 
const initDefaultValue = (value) => {
    let arr = value.split("-");
    year.value = arr[0].slice(0, 4);
    var myseason = arr[1];
    showValue.value = `${year.value}年${whitchQuarter(myseason)}季度`;
}
 
 
onMounted(() => {
    if (props.defaultValue) {
        console.log(props.defaultValue)
        initDefaultValue(props.defaultValue);
    }
})
</script>
 
<template>
    <div class="quarter">
        <mark style="position:fixed;top:0;bottom:0;left:0;right:0;background:rgba(0,0,0,0);z-index:999;" v-show="showSeason"
              @click.stop="showSeason = false"></mark>
        <el-popover
            :visible="showSeason"
            placement="bottom"
            trigger="click"
            class="popover"
            :width="300"
        >
            <template #reference>
                <el-input :placeholder="props.placeholder" v-model="showValue" style="width:100%;" class="elWidth" :clearable="props.clearable" @click="showSeason = true" >
                    <template #prefix>
                        <el-icon class="el-input__icon"><Calendar /></el-icon>
                    </template>
                </el-input>
            </template>
            <template #default>
                <div class="box-card">
                    <div class="yearBox">
                        <el-button type="info" icon="DArrowLeft" @click="prev" link></el-button>
                        <span role="button" class="el-date-picker__header-label">{{ year }}年</span>
                        <el-button type="info" @click="next" :class="{ notallow: year === props.limitTime }"
                                   icon="DArrowRight" link></el-button>
                    </div>
                    <div class="text">
                        <div @click="selectSeason(0)" class="item">第一季度</div>
                        <div @click="selectSeason(1)" class="item">第二季度</div>
                    </div>
                    <div class="text">
                        <div @click="selectSeason(2)" class="item">第三季度</div>
                        <div @click="selectSeason(3)" class="item">第四季度</div>
                    </div>
                </div>
            </template>
        </el-popover>
    </div>
</template>
 
<style scoped lang="scss">
.quarter{
    width: 100%;
    .elWidth{
        width: 100%;
    }
}
:deep(.el-popover .el-popper){
    width: 300px !important;
}
.notallow {
    cursor: not-allowed;
}
.box-card {
    width: 100%;
    .yearBox{
        text-align:center;
        padding:0;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .text{
        margin-top: 1rem;
        display: flex;
        .item{
            width: 50%;
            text-align: center;
            &:hover{
                color: #409eff !important;
                cursor: pointer;
            }
        }
    }
}
</style>