zoukankan      html  css  js  c++  java
  • 时间选择器datetimerpicker封装使用(非脚手架)

    datePicker.vue 封装

    <template>
        <el-date-picker
                v-model="currentTime"
                type="datetimerange"
                :picker-options="pickerOptions"
                range-separator="~"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right"
                @change="getChorceTime">
        </el-date-picker>
    </template>
    
    <script>
    
        module.exports = {
            name: "datePicker",
            props: ['time'],
            data() {
                return {
                    currentTime: "",
                    pickerOptions: {
                        shortcuts: [
                            {
                                text: '最近一周',
                                onClick(picker) {
                                    const end = new Date();
                                    const start = new Date();
                                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                                    picker.$emit('pick', [start, end]);
                                }
                            }, {
                                text: '最近一个月',
                                onClick(picker) {
                                    const end = new Date();
                                    const start = new Date();
                                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                                    picker.$emit('pick', [start, end]);
                                }
                            }, {
                                text: '最近三个月',
                                onClick(picker) {
                                    const end = new Date();
                                    const start = new Date();
                                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                                    picker.$emit('pick', [start, end]);
                                }
                            }]
                    },
                }
            },
            methods:{
                getChorceTime(val){
                    if(val == null){
                        bus.$emit('changeTimeValue',"");
                    }else{
                        bus.$emit('changeTimeValue',val)
                    }
                }
            }
        }
    </script>
    
    <style scoped>
    
    </style>

    注意:

     value-format :一定要,否则选择的时间是带有英文月份的时间,不是想要的年月日时分秒

     
    getChorceTime(val){
                    if(val == null){
                        bus.$emit('changeTimeValue',""); // 必要:当交互中清除已选时间时查询条件应该清空
                    }else{
                        bus.$emit('changeTimeValue',val) // 传递当前选择的时间
    } }
     

    使用

    js.
    module.exports = { name:'FileUpload', components: { 'date-picker': httpVueLoader('/template/default/pc/home/index/components/datePicker.vue'), },
    }

    <template>

    <date-picker :time="searchForm.time"></date-picker>
    </template>
    
    
    核心:
    mounted(){ bus.$on(
    'changeTimeValue',(val)=>{ if(val){ this.searchForm.time = val[0] + '~' + val[1]; }else{ this.searchForm.time = ""; } }) }
     

    本文来自博客园,作者:💞Travelerᘗ,转载请注明原文链接:https://www.cnblogs.com/LindaBlog/p/15791230.html

  • 相关阅读:
    内置函数(十)
    常用命令
    函数式编程(九)——map,filter,reduce
    函数(八)-函数和匿名函数
    设计模式(十三)——观察者模式
    Confluence 6 重要缓存和监控
    Confluence 6 数据中心的缓存
    Confluence 6 配置文件和key
    Confluence 6 缓存性能示例
    Confluence 6 缓存性能优化
  • 原文地址:https://www.cnblogs.com/LindaBlog/p/15791230.html
Copyright © 2011-2022 走看看