vant 時間選擇器--開始時間和結束時間


<template>
    <div class="linesMigrate">
        <div class="conditionDiv">
            <div class="singleCondition" :style="conditonStyle2" @click="showPopupDateChooseStart">
                <div class="dateValueDiv" v-if="allDateStart">{{dateFormatterStart}}</div>
                <div class="dateValueDiv" v-if="!allDateStart" :style="{ fontSize:'0.72rem' }">{{dateFormatterStart}}</div>
            </div>
            <div class="singleCondition" :style="conditonStyle2" @click="showPopupDateChoose">
                <div class="dateValueDiv" v-if="allDate">{{dateFormatter}}</div>
                <div class="dateValueDiv" v-if="!allDate" :style="{ fontSize:'0.72rem' }">{{dateFormatter}}</div>
            </div>
        </div>
        <!-- 選開始時間 -->
        <van-popup
                v-model="showDateChooseStart"
                position="bottom"
                :close-on-click-overlay="false"
                :style="{ height: '22rem',position: 'absolute',bottom:'3rem' }"
                :overlay-style="{ position: 'absolute', bottom: '3rem', top: 'auto', background: 'rgba(0, 0, 0, 0.298039215686275)' }">
            <div class="popupDate" :style="bgDateImgStyle">
            <img alt="..." src="../../assets/icon/downArrow.png" @click="closeDatePopStart" :style="{ marginBottom: '16%',width:'10%',marginTop:'7.7%',opacity:0 }" />
            <van-datetime-picker
                    :show-toolbar="false"
                    v-model="dateChooseStart"
                    type="date"
                    :min-date="minDateStart"
                    :max-date="maxDateStart"
            />
                <div class="chooseTodayDiv" @click="chooseDateStart">選擇最近一周</div>
            </div>
        </van-popup>
        <!-- 選結束時間 最近一天 -->
        <van-popup
                v-model="showDateChoose"
                position="bottom"
                :close-on-click-overlay="false"
                :style="{ height: '22rem',position: 'absolute',bottom:'3rem' }"
                :overlay-style="{ position: 'absolute', bottom: '3rem', top: 'auto', background: 'rgba(0, 0, 0, 0.298039215686275)' }">
            <div class="popupDate" :style="bgDateImgStyle">
            <img alt="..." src="../../assets/icon/downArrow.png" @click="closeDatePop" :style="{ marginBottom: '16%',width:'10%',marginTop:'7.7%',opacity:0 }" />
            <van-datetime-picker
                    :show-toolbar="false"
                    v-model="dateChoose"
                    type="date"
                    :min-date="minDate"
                    :max-date="maxDate"
            />
                <div class="chooseTodayDiv" @click="chooseDateToday">選擇最新一天</div>
            </div>
        </van-popup>

 

        <Loading :isShow="loadingState" />
    </div>
</template>

 

<script>
    import Loading from '../../components/common/loading.vue';// 引入公共的loading組件
    import Vue from 'vue';
    import { Popup } from 'vant';
    import { DatetimePicker } from 'vant';
    
    Vue.use(DatetimePicker);
    Vue.use(Popup);

 

    export default {
        name: 'lines',
        mounted() {
            // 獲取數據
            this.loadingState=false;
            this.initRequest();
        },
        data() {
            return {
                // 時間選擇背景樣式數據
                conditonStyle2: {
                    background: "url(" + require("../../assets/img/migrate2.png") + ") no-repeat center",
                    backgroundSize: "contain"
                },
                // 日期組件彈出層展示與否的標志 --開始時間
                showDateChooseStart:false,
                 // 日期組件彈出層展示與否的標志
                showDateChoose:false,
                
                // 彈出層背景圖片
                bgDateImgStyle: {
                    background: "url(" + require("../../assets/img/migratePopBackDate.png") + ") no-repeat",
                    backgroundSize: "contain"
                },
                // 標准化未經處理的時間格式
                dateChoose: new Date(),
                dateChooseStart: new Date(),
                // 處理過格式的日期數據
                dateFormatter:"日期",
                allDate:true,
                dateFormatterStart:"日期",
                allDateStart:true,

 

                minDateStart: new Date(2019,8,1),  // 開始時間最小2019/9/01
                maxDateStart: new Date(), // 開始時間最大 當前時間
                minDate: new Date(), //最小時間必須 》開始的最大時間maxDateStart 
                maxDate: new Date(), // 開始時間最大 當前時間

 

                // 控制加載中狀態的標志
                loadingState:true
            };
        },
        methods: {
            // 初始的請求方法
            async initRequest(){
                this.chooseDateToday();
                this.chooseDateStart();
            },
            // 展示日期彈出層--開始時間
            showPopupDateChooseStart() {
                this.showDateChooseStart = true;
            },
            // 展示日期彈出層 --結束時間
            showPopupDateChoose() {
                // 設置結束時間的最小值
                this.minDate = this.dateChooseStart;
                this.showDateChoose = true;
            },
            
            // 關閉日期彈出層 ---開始時間
            closeDatePopStart(){
                this.showDateChooseStart = false;
                this.dateFormatterStart=this.dateTransfor(this.dateChooseStart,"yyyy-MM-dd");

 

                // 設置結束時間的最小值
                this.minDate = this.dateChooseStart;

 

                this.allDateStart=false;
                // this.loadingState=true;
            },
            // 關閉日期彈出層 --結束時間
            closeDatePop(){
                this.showDateChoose = false;
                this.dateFormatter=this.dateTransfor(this.dateChoose,"yyyy-MM-dd");

 

                // 設置開始時間的最大值
                this.maxDateStart = this.dateChoose;

 

                this.allDate=false;
                // this.loadingState=true;
            },
            
            // 日期格式轉換函數
            dateTransfor(date,format){
                var o = {
                    "M+" : date.getMonth()+1,                 //月份
                    "d+" : date.getDate(),                    //日
                    "h+" : date.getHours(),                   //小時
                    "m+" : date.getMinutes(),                 //分
                    "s+" : date.getSeconds(),                 //秒
                    "q+" : Math.floor((date.getMonth()+3)/3), //季度
                    "S"  : date.getMilliseconds()             //毫秒
                };
                if(/(y+)/.test(format)) {
                    format=format.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
                }
                for(let k in o) {
                    if(new RegExp("("+ k +")").test(format)){
                        format = format.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
                    }
                }
                return format;
            },
            // 選擇時間函數 -- 七天前
            chooseDateStart(){
                let yesToday=new Date();
                yesToday.setDate(yesToday.getDate()-7);
                this.dateChooseStart=yesToday;
                this.dateFormatterStart=this.dateTransfor(this.dateChooseStart,"yyyy-MM-dd");
            },
            // 選擇時間函數 -- 今日
            chooseDateToday(){
                this.dateChoose= new Date();
                this.dateFormatter=this.dateTransfor(this.dateChoose,"yyyy-MM-dd");
            },
            // 選擇時間函數  i=0今日
            chooseDate(i){
                let yesToday=new Date();
                yesToday.setDate(yesToday.getDate()-i);
                return this.dateTransfor(yesToday,"yyyy-MM-dd");
            },

 

        },
        components: {
            Loading
        }
    };
</script>
<style scoped>
    /*覆蓋原有彈出層樣式*/
    .van-popup {
        position: absolute;
        background: transparent;
        /*bottom: 3.01rem;*/
    }
    /*覆蓋日期組件原有樣式*/
    .van-picker{
        position: relative;
        background-color: #fff;
        user-select: none;
        width: 100%;
    }
  
    /*篩選條件們的div樣式*/
    .conditionDiv{
        display: flex;
        justify-content: center;
        width: 100%;
        height: 3.7%;
        margin-top: 4%;
    }
    /*單個篩選條件的樣式*/
    .singleCondition{
        width: 27%;
        margin-left: 2%;
        margin-right: 2%;
        /*border: 1px solid #451da4;*/
        height: 100%;
        /*padding-right: 5%;*/
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        /*flex-direction: column;*/
        font-size: 0.72rem;
    }
    /*日期組件的div,由於其背景用icon而非整個背景圖,因此樣式上不同*/
    .singleDateDiv{
        width: 23%;
        margin-left: 2%;
        margin-right: 2%;
        /*border: 1px solid #451da4;*/
        height: 100%;
        color: #fff;
        padding-right: 5%;
        display: flex;
        justify-content: flex-start;
    }
    /*日期的值位置樣式*/
    .dateValueDiv{
        display: flex;
        justify-content: center;
        flex-direction: column;
        font-size: 0.72rem;
        width: 82%;
    }
    /*日期彈出層中選擇今天div樣式*/
    .chooseTodayDiv{
        box-sizing: border-box;
        z-index: 2;
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        bottom: 0;
        color: red;
        height: 3rem;
        width: 90%;
        background: #fff;
        border-top: 1px solid lightgrey;
        font-size: 0.875rem;
    }
    
    /*日期彈窗*/
    .popupDate {
        border-radius: 2px;
        background: #fff;
        height: 100%;
        width: 90%;
        margin: 0 auto;
    }

 

</style>
<style>
    /*覆蓋原有的選擇器樣式*/
    .van-picker-column{
        font-size: 0.9rem;
    }
</style>


免責聲明!

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



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