Update timerangeselect dynamically

This commit is contained in:
Matthias 2021-07-04 16:25:41 +02:00
parent 8c8c430513
commit 466f809425

View File

@ -37,7 +37,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { Component, Vue, Emit, Prop } from 'vue-property-decorator'; import { Component, Vue, Emit, Prop, Watch } from 'vue-property-decorator';
import { dateFromString, dateStringToTimeRange, timestampToDateString } from '@/shared/formatters'; import { dateFromString, dateStringToTimeRange, timestampToDateString } from '@/shared/formatters';
const now = new Date(); const now = new Date();
@ -54,10 +54,12 @@ export default class TimeRangeSelect extends Vue {
return this.timeRange; return this.timeRange;
} }
created() { @Watch('value')
if (!this.value) { valueChanged() {
this.dateFrom = timestampToDateString(new Date(now.getFullYear(), now.getMonth() - 1, 1)); this.updateInput();
} else { }
updateInput() {
const tr = this.value.split('-'); const tr = this.value.split('-');
if (tr[0]) { if (tr[0]) {
this.dateFrom = timestampToDateString(dateFromString(tr[0], 'yyyyMMdd')); this.dateFrom = timestampToDateString(dateFromString(tr[0], 'yyyyMMdd'));
@ -66,6 +68,13 @@ export default class TimeRangeSelect extends Vue {
this.dateTo = timestampToDateString(dateFromString(tr[1], 'yyyyMMdd')); this.dateTo = timestampToDateString(dateFromString(tr[1], 'yyyyMMdd'));
} }
} }
created() {
if (!this.value) {
this.dateFrom = timestampToDateString(new Date(now.getFullYear(), now.getMonth() - 1, 1));
} else {
this.updateInput();
}
this.emitTimeRange(); this.emitTimeRange();
} }