Setting values
Values can change in a couple of different ways: through defaults, interacting with the UI or programmatically. The date and time picker defaults to now, which can be easily overridden with the defaultSelection option. The values are set by interacting with the component and making a selection or it can be done programmatically by updating the bound value. Use the buttons option for showing/hiding set, cancel or add custom buttons.
Demo
The calendar defaults to today if not overridden. You can however manually set a default value through the
defaultSelection option.
<template>
<MbscDatepicker
:controls="['date']"
/>
</template>
<script setup>
const myDefaultSelection = new Date(2020, 11, 24)
</script>
<template>
<MbscDatepicker
:controls="['calendar']"
:defaultSelection="myDefaultSelection"
/>
</template>
While the selected value can be changed by interacting with the calendar, you can set the model value to change it programmatically.
<script setup>
const myCustom = ref()
function setValue() {
myCustom.value = new Date(2020, 0, 2)
}
function setToday() {
myCustom.value = new Date()
}
</script>
<template>
<MbscButton @click="setValue">02-01-2020</MbscButton>
<MbscButton @click="setToday">Today</MbscButton>
<MbscDatepicker
:controls="['date']"
v-model="myCustom"
/>
</template>
The button API provides shortcuts to value selection and confirmation. Add 'Now' or custom button to the component or entirely remove the 'Set' button to activate auto-set, which automatically closes the dialog if a date is selected.
<script setup>
import {
MbscButton,
MbscDatepicker,
MbscPage,
setOptions
} from '@mobiscroll/vue'
import { ref } from 'vue'
setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
})
const myDefaultSelection = new Date(2020, 11, 24)
const myCustom = ref()
const now = ref()
const nowRef = ref()
const custom = ref()
const customRef = ref()
const nowButtons = [
{
text: 'Now',
handler: () => {
now.value = new Date()
nowRef.value.instance.close()
}
}
]
const customButtons = [
{
text: '05 Jan 2020',
handler: () => {
custom.value = new Date(2020, 0, 5)
customRef.value.instance.close()
}
},
'set',
'cancel'
]
const autoButtons = ['close']
function setValue() {
myCustom.value = new Date(2020, 0, 2)
}
function setToday() {
myCustom.value = new Date()
}
</script>
<template>
<MbscPage>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<MbscDatepicker label="Default" placeholder="Please select..." :controls="['date']" />
<MbscDatepicker
label="Custom default"
placeholder="Please select..."
:controls="['date']"
:defaultSelection="myDefaultSelection"
/>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Setting a custom value</div>
<div class="mbsc-button-group-block">
<MbscButton @click="setValue">02-01-2020</MbscButton>
<MbscButton @click="setToday">Today</MbscButton>
</div>
<MbscDatepicker :controls="['date']" v-model="myCustom" display="inline" />
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Buttons API</div>
<MbscDatepicker
ref="nowRef"
label="Now"
placeholder="Please select..."
v-model="now"
:controls="['date']"
:buttons="nowButtons"
/>
<MbscDatepicker
ref="customRef"
label="Custom"
placeholder="Please select..."
v-model="custom"
:controls="['date']"
:buttons="customButtons"
/>
<MbscDatepicker
label="Auto set"
placeholder="Please select..."
:controls="['date']"
:buttons="autoButtons"
/>
</div>
</MbscPage>
</template>
<script setup lang="ts">
import {
MbscButton,
MbscDatepicker,
MbscPage,
setOptions
} from '@mobiscroll/vue'
import { ref } from 'vue'
setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
})
const myDefaultSelection = new Date(2020, 11, 24)
const myCustom = ref()
const now = ref()
const nowRef = ref()
const custom = ref()
const customRef = ref()
const nowButtons = [
{
text: 'Now',
handler: () => {
now.value = new Date()
nowRef.value.instance.close()
}
}
]
const customButtons = [
{
text: '05 Jan 2020',
handler: () => {
custom.value = new Date(2020, 0, 5)
customRef.value.instance.close()
}
},
'set',
'cancel'
]
const autoButtons = ['close']
function setValue() {
myCustom.value = new Date(2020, 0, 2)
}
function setToday() {
myCustom.value = new Date()
}
</script>
<template>
<MbscPage>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<MbscDatepicker label="Default" placeholder="Please select..." :controls="['date']" />
<MbscDatepicker
label="Custom default"
placeholder="Please select..."
:controls="['date']"
:defaultSelection="myDefaultSelection"
/>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Setting a custom value</div>
<div class="mbsc-button-group-block">
<MbscButton @click="setValue">02-01-2020</MbscButton>
<MbscButton @click="setToday">Today</MbscButton>
</div>
<MbscDatepicker :controls="['date']" v-model="myCustom" display="inline" />
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Buttons API</div>
<MbscDatepicker
ref="nowRef"
label="Now"
placeholder="Please select..."
v-model="now"
:controls="['date']"
:buttons="nowButtons"
/>
<MbscDatepicker
ref="customRef"
label="Custom"
placeholder="Please select..."
v-model="custom"
:controls="['date']"
:buttons="customButtons"
/>
<MbscDatepicker
label="Auto set"
placeholder="Please select..."
:controls="['date']"
:buttons="autoButtons"
/>
</div>
</MbscPage>
</template>