layui 的时间选择器选中本日,本周,本月

/**
 * 时间选择器
 * **/
let data=new Date();
let monthCurr1=data.getMonth()+1;
let yearCurr=data.getFullYear();
let day;

//时间选择器选择本周----周一
function getMonday()
{
    let today=new Date();
    let weekday=today.getDay();
    let dd=new Date(1000*60*60*24*(1-weekday) + today.getTime());
    let y = dd.getFullYear();
    let m = dd.getMonth()+1;//获取当前月份的日期
    m=parseInt(m,10);
    if(m<10){
        m="0"+m;
    }
    var d = dd.getDate();
    d=parseInt(d,10);
    if(d<10){
        d="0"+d;
    }
    return y+"-"+m+"-"+d;
}
//时间选择器选择本周----周日
function getSunday()
{
    let today=new Date();
    let weekday=today.getDay();
    let dd=new Date(1000*60*60*24*(7-weekday) + today.getTime());
    let y = dd.getFullYear();
    let m = dd.getMonth()+1;//获取当前月份的日期
    m=parseInt(m,10);
    if(m<10){
        m="0"+m;
    }
    let d = dd.getDate();
    d=parseInt(d,10);
    if(d<10){
        d="0"+d;
    }
    return y+"-"+m+"-"+d;
}


//时间选择器选定本月,根据月份来选定月末日期
let  Array=[1,3,5,7,8,10,12];
let Array1=[4,6,9,11];
if (Array.includes(monthCurr1)){
    day = 31;
}else if (Array1.includes(monthCurr1)){
    day = 30;
}else if(monthCurr1===2){
    //判断是否闰年
    if (((yearCurr % 4)==0) && ((yearCurr % 100)!=0) || ((yearCurr % 400)==0)) {
        day = 29;
    } else {
        day = 28;
    }
}

//监听select
form.on('select(selectDate)', function(data){
    let date = data.value;
    if (date==0){
        laydate.render({
            elem: "[name='planBeginTime']",//指定元素
            theme: '#3278F0'
            ,trigger:'click'
            ,value:new Date
            ,isInitValue:true
        });
        laydate.render({
            elem: "[name='planEndTime']",//指定元素
            theme: '#3278F0'
            ,trigger:'click'
            ,value:new Date
            ,isInitValue:true
        });
    }
    else if (date==1) {

        laydate.render({
            elem: "[name='planBeginTime']",//指定元素
            theme: '#3278F0'
            ,value:getMonday()
            ,trigger:'click'
            ,isInitValue:true

        });
        laydate.render({
            elem: "[name='planEndTime']",
            theme: '#3278F0'
            ,trigger:'click'
            ,isInitValue:true
            ,value:getSunday()
        });
    }
    else if (date==2) {
        laydate.render({
            elem: "[name='planBeginTime']",//指定元素
            theme: '#3278F0'
            ,trigger:'click'
            ,isInitValue:true
            ,value:""+yearCurr+"-"+monthCurr1+"-"+"01"+""
        });
        laydate.render({
            elem: "[name='planEndTime']",//指定元素
            theme: '#3278F0'
            ,trigger:'click'
            ,isInitValue:true
            ,value:""+yearCurr+"-"+monthCurr1+"-"+day+""
        });
    }

});

但是这样书写还会有bug,就是点击搜索的时候渔鸥将今日的值赋给了时间选择器,所以需要在刷新页面的时候将目前的值赋值给时间选择器。

//查询 方法
const querySupplement = (query) => {
    supplementApi.getList(query)
        .then((data) => {
            if (data.code === 200) {
                supplement_data = data.data;
                if($('#selectonEd option:selected').val()==0){
                    //默认选中今日
                    laydate.render({
                        elem: "[name='planBeginTime']",//指定元素
                        theme: '#3278F0'
                        ,trigger:'click'
                        ,value:new Date
                        ,isInitValue:true
                    });
                    laydate.render({
                        elem: "[name='planEndTime']",//指定元素
                        theme: '#3278F0'
                        ,trigger:'click'
                        ,value:new Date
                        ,isInitValue:true
                    });
                }
                else if($('#selectonEd option:selected').val()==1){
                    //选中本周
                    laydate.render({
                        elem: "[name='planBeginTime']",//指定元素
                        theme: '#3278F0'
                        ,value:getMonday()
                        ,trigger:'click'
                        ,isInitValue:true

                    });
                    laydate.render({
                        elem: "[name='planEndTime']",
                        theme: '#3278F0'
                        ,trigger:'click'
                        ,isInitValue:true
                        ,value:getSunday()
                    });

                }
                else if($('#selectonEd option:selected').val()==2){
                    //选中本月
                    laydate.render({
                        elem: "[name='planBeginTime']",//指定元素
                        theme: '#3278F0'
                        ,trigger:'click'
                        ,isInitValue:true
                        ,value:""+yearCurr+"-"+monthCurr1+"-"+"01"+""
                    });
                    laydate.render({
                        elem: "[name='planEndTime']",//指定元素
                        theme: '#3278F0'
                        ,trigger:'click'
                        ,isInitValue:true
                        ,value:""+yearCurr+"-"+monthCurr1+"-"+day+""
                    });

                }
                supplementTable.reload({
                    data: supplement_data.list
                });
                supplementPage();
            }
            cfx.close();
        });
};

你可能感兴趣的:(框架,JavaScript)