vant-日期使用


1、DatePicker.vue

<template>
    <div>
        <van-field
                v-model="result"
                v-bind="$attrs"
                readonly
                is-link
                @click="show = !show"
        />
        <van-popup v-model="show" position="bottom">
            <van-datetime-picker
                    show-postal
                    type="date"
                    :formatter="formatter"
                    :title="$attrs.label"
                    @cancel="show = !show"
                    @confirm="onConfirm"
                    @change="changeFn"
            />
        </van-popup>
    </div>
</template>

<script>
    export default {
        model: {
            prop: "selectValue"
        },
        props: {
            columns: {
                type: Array
            },
            selectValue: {
                type: String
            }
        },
        data() {
            return {
                show: false,
                result: this.selectValue
            };
        },
        methods: {
            onConfirm(value) {
                this.result = this.timeFormat(value);
                this.show = !this.show;
            },
            timeFormat(time) { // 時間格式化 2019-09-08
                let year = time.getFullYear();
                let month = time.getMonth() + 1;
                let day = time.getDate();
                return year + '-' + month + '-' + day;
            },
            // 選項格式化函數
            formatter (type, value) {
                if (type === 'year') {
                    return `${value}年`
                } else if (type === 'month') {
                    return `${value}月`
                } else if (type === 'day') {
                    return `${value}日`
                } else if (type === 'hour') {
                    return `${value}時`
                } else if (type === 'minute') {
                    return `${value}分`
                } else if (type === 'second') {
                    return `${value}秒`
                }
                return value
            },
            changeFn() { // 值變化是觸發
                // this.result = this.result
            },
        },
        watch: {
            selectValue: function(newVal) {
                this.result = newVal;
            },
            result(newVal) {
                this.$emit("input", newVal);
            }
        }
    };
</script>

<style></style>

2、父組件引用

 <date-picker
      :label="item.label"
      placeholder="請選擇"
      v-model="dataList[item.name]"
      :required="item.mandatory"
      :rules="[{ required: item.mandatory, message: '請選擇'+item.label}]"
/>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM