简体   繁体   中英

Vue JS date of birth picker

I'm setting up a date of birth field. How can I set the "Year" as my first popup window? instead of calendar view popup.

The view package I'm using is "vue2-datepicker"

I would like to choose the "YEAR" first and then "Month" and then "Date"

This is what I want to show up first when I click the calendar icon.

在此处输入图片说明

At the moment, when I click the calendar icon it show the default calendar.

在此处输入图片说明

Here's the code I have at the moment. It's working fine with the default calendar but I just want to default to the year selection first and then Month and then date.

    <template>
    <div class="styled-form__field">
        <label class="styled-form__label">{{ label }}</label>
        <span
            :class="['styled-form__error', {'is-visible': error}]"
        >{{ error }}</span>

        <date-picker
            :value="value"
            lang="en"
            :format="'DD MMMM YYYY'"
            :placeholder="' '"
            :width="'auto'"
            :input-class="datePickerClass"
            :not-before="datePicker.start"
            :not-after="datePicker.finish"
            @input="changeHandler"
        ></date-picker>
    </div>
</template>

<script>
    import moment from 'moment';
    import DatePicker from '../common/DatePicker.vue';

    export default {
        components: {
            DatePicker
        },
        props: {
            value: {
                required: true
            },
            label: String,
            error: String
        },
        data() {
            return {
                datePicker: {
                    start: moment().subtract(115, 'years').toDate(),
                    finish: moment().subtract(1, 'days').toDate()
                }
            };
        },
        computed: {
            datePickerClass() {
                return this.error ? 'styled-form__date-picker is-invalid' : 'styled-form__date-picker';
            }
        },
        methods: {
            changeHandler(newValue) {
                let parsedValue = '';
                if (newValue !== '') {
                    parsedValue = moment(newValue).format('YYYY-MM-DD');
                }
                this.$emit('input', parsedValue);
            }
        }
    };
</script>

The standard browser datepicker doesn't let you alter its behaviour like that. You might find another datepicker component that suits you better, but I suspect you will have to write your own, combining separate inputs for year, month and date. The effort will only be worth it if you're super-picky about the result. For most situations, you will be better off with a ready-made component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM