项目需求中有一个日期选择限制的功能点:今天之前不可选,周末不可选。
传统的input type='date无法做到,所以使用了这个插件来实现功能。

  1. 引入vue-datepicker loader:npm install vue-datepicker
  2. 引入moment loader:npm install moment --save

    因为vue-datepicker是依赖vue和moment的,所以也应提前 引入moment;

  3. 在用到该插件的地方引入:
    import myDatepicker from 'vue-datepicker/vue-datepicker-es6.vue';

/* vue 2.0 */
页面中实现如下:

<template>
                <date-picker :date="startTime" :option="option" :limit="limit" id="select_date"></date-picker>
</template>
export default {
        components: {
            'date-picker': myDatepicker
        },
        data() {
            return {
                startTime: {
                    time: ''
                },
                endTime: {
                    time: ''
                },
                option: {
                    type: 'day',
                    week: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
                    month: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                    format: 'YYYY-MM-DD',
                    placeholder: 'when?',
                    inputStyle: {
                        'display': 'inline-block',
                        'padding': '4px',
                        'line-height': '17px',
                        'font-size': '14px',
                        'width': '190px',
                        'border': '1px solid #ddd',
                        // 'box-shadow': '0 1px 3px 0 rgba(0, 0, 0, 0.2)',
                        'border-radius': '5px',
                        'color': '#5F5F5F'
                    },
                    color: {
                        header: '#ccc',
                        headerText: '#f00'
                    },
                    buttons: {
                        ok: 'Ok',
                        cancel: 'Cancel'
                    },
                    overlayOpacity: 0.5, // 0.5 as default
                    dismissible: true // as true as default
                },
                timeoption: {
                    type: 'min',
                    week: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
                    month: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                    format: 'YYYY-MM-DD HH:mm'
                  },
                  multiOption: {
                    type: 'multi-day',
                    week: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
                    month: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                    format:"YYYY-MM-DD HH:mm"
                  },
                limit: [{
                    type: 'weekday',
                    available: [1, 2, 3, 4, 5]
                }, {
                    type: 'fromto',
                    from: getLocalTime(date),
                    to: ''
                }]
            }
        }
    }
Logo

前往低代码交流专区

更多推荐