资讯专栏INFORMATION COLUMN

datepicker/calendar(价格日历|日历|日期选择器)

simpleapples / 1920人阅读

摘要:基于事件操作的价格日历,适用于端和移动端

基于事件操作的价格日历,适用于PC端和移动端
https://github.com/Fi2zz/date...

FEATURES

Base on event

Easy to display data on html element by using data event

Support double views,flat view and single view

Disabled date can be selected as end date while using setData function

Support Multi selection

RUN DEMO
yarn dev or npm run dev 
OPTIONS
OPTION RERUIRED TYPE DESC DEFAULT VALUE
el YES string,HTMLElement Element or selector to mount DatePcker
startDate NO Date Start date of DatePicker new Date
endDate NO Date End date of DatePicker new Date() + 6 months
doubleSelect NO boolean Enable pick two dates false
limit NO number Limitation between two dates while doubleSelect is on
views NO number,string Display views of DatePicker auto,1,2
selection NO number Size of dates can be picked ,value must be not less than 2 1
NOTE
when `selection` is greater than 2, `bindData` and `doubleSelect` will be set to `false`,
and `option.limit` will be set the same as `selection`
API
   setDates([dates]:tuple);
   //Set  dates to DatePicker
   //dates accept  and 

   setLanguage(language:any);
   //set DatePicker"s language

   setData(callback)
   //set data to DatePicker
   //more detail see [USAGE]

   setDisabled({
        days?:Array[5],
        dates?:Array,
        from?:,
        to?:
    })
   //Set disabled dates to DataPicker
   //dates =>dates accept  and ,  all dates in [dates] will be disabled
   //days => days accept number of [0~6],   all days in [days] will be disabled
   //from => from accept  or , all dates after [from] date will be disabled,
   //        eg: from = 2018-3-31 => disabled from 2018-4-1  
   //to   => to accept  or  all dates before [to] date will be disabled,
   //        eg: to =2018-3-4 => all dates before 2018-3-5 will be disabled

   parse(formattedDate:string,dateFormat:string)
   //Transform date string into date object,return Date object
   //eg: formattedDate ="2018-3-4",format="YYYY-M-D" =>  new Date(2018,2,4)

   format(date:Date,format:string)
   //Transform date object into string,return string  
   //eg: date =new Date(),format="YYYY-MM-DD" => 2018-03-04

   on(event:string,fn:Function)
   //Event listener
   //eg: datePicker.on("event",(result)=>{
                //your logic
   //    })
USAGE
    //es module
    import DatePicker from "/dist/datepicker.esm.js"
    import "/dist/style.css"

    //cjs
    const DatePicker =require("/dist/datepicker.js");
    require("/dist/style.css");


    //umd
    
    



    const date = new Date();
    const dist = {
           year: date.getFullYear(),
           month: date.getMonth(),
           date: date.getDate()
    };

    const from = new Date(dist.year, dist.month, dist.date)
    const to = new Date(dist.year, dist.month + 9, 0);
    const currDate = new Date(dist.year, dist.month, dist.date);

    //setup DatePicker instance
    const app = new DatePicker({
            el: document.getElementById("datepicker"),
            endDate:to,
            startDate:from,
            limit: 7,
            format: "YYYY-M-D",
            doubleSelect: true,
            views: 1,
            selection:4 // if selection not less than 2, doubleSelect will be disabled,
                        //and `data` event and `setData` will not work
    });




    //`update` event fired by click on date cell and DatePicker init
    app.on("update", (result) => {
        // result contains two keys, `value` and `type`
        // value =>  selected dates
        // type  =>  two types => `init` and `selected`
        // place your logic  here
        //eg:
        // document.getElementById("dates").innerText = value
    });

    // `disabled`event fired by `setDisabled`
    app.on("disabled", (result) => {
        // result contains two keys, `dateList` and `nodeList`
        const {dateList, nodeList} = result;
        for (let n = 0; n < nodeList.length; n++) {
            let node = nodeList[n];
            if (dateList[node.getAttribute("data-date")]) {
                node.classList.add("disabled")
            }
        }
    });
    // "data" event fired by `setData`
    app.on("data", (result) => {
        //set HTML nodes states
        // result contains two keys, `data:any` and `nodeList:Array`
            const data = result.data;
            const nodeList = result.nodeList;
            for (let i = 0; i < nodeList.length; i++) {
                let node = nodeList[i];
                let date = node.getAttribute("data-date");
                if (date in data) {
                    if (!node.classList.contains("disabled")) {
                        let itemData = source[date];
                        if (itemData.highlight) {
                            addClass(node, "highlight")
                        }
                        let placeholder = node.querySelector(".placeholder");
                        placeholder.innerHTML = itemData.value
                    }

                } else {
                    addClass(node, "disabled")
                }
            }
        });

       //tuple type,accept  and 
       const selected=["2018-2-21",new Date()];
       //use `setDates` to set init dates to DatePicker instance
       app.setDates(selected);
       // use `setDisabled` to set specified date or day to disabled,
       // `setDisabled` accept an object => {dates,days},
       // dates,accept  and  
       // >days accept 0,1,2,3,4,5,6
       app.setDisabled({
                dates: [
                    "2018-2-18",
                    "2018-2-19",
                    "2018-2-22",
                    new Date
                ],
                days: [1, 5, 2, 6],
                from:new Date(2018,2,10)
                to:"2018-7-15"
            });

        // To display your data,like price info on date cell,
        // use `setData` to pass data to `DatePicker` instance
        // `setData` function will dispatch `data` event
        // setup a listener to handle it
       app.setData(() => {
                    const data ={};
                    /*data accept Object like
                     {
                       "2018-1-31":{/*your codes * },
                       "2018-2-21":123,
                        }
                    */
                    //your logic here


                    //return data when your logic done
                    return data
        });

       //set DatePicker"s language
       //language options
       const language={
                 days: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
                 months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
                 year: ""
       };
       app.setLanguage(language)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/89726.html

相关文章

  • 移动端H5日历组件,已废弃不在维护

    摘要:默认可选回调函数,绑定业务数据。例如某天有日程,则会在对应日期上标识出一个小红点或者其他标识,默认传入数据格式必选回调函数,当你点击或轻触某日期后执行。 文档维护者:孙尊路 喜欢的话,记得star 一下噢! 适用场景 该组件目前仅适用于移动端H5页面展示,后期高级用法中会讲述到如何基于日历基类实现自定义模板传入。(即:开发者只需要传入自己的模板即可实现出自己的优美的日历出来。) 本篇...

    Moxmi 评论0 收藏0
  • 怎样实现一个datePicker日期选择)组件

    摘要:百度前端技术学院上有一个任务,要实现一个日期选择组件,本文由此而来看看需求组件默认一直呈显示状态通过某种方式选择年月,选择了年月后,日期列表做相应切换通过单击某个具体的日期进行日期选择组件初始化时,可配置可选日期的上下限。 百度前端技术学院上有一个任务,要实现一个日期选择组件,本文由此而来~ 看看需求 组件默认一直呈显示状态 通过某种方式选择年、月,选择了年月后,日期列表做相应切换 ...

    layman 评论0 收藏0
  • 怎样实现一个datePicker日期选择)组件

    摘要:百度前端技术学院上有一个任务,要实现一个日期选择组件,本文由此而来看看需求组件默认一直呈显示状态通过某种方式选择年月,选择了年月后,日期列表做相应切换通过单击某个具体的日期进行日期选择组件初始化时,可配置可选日期的上下限。 百度前端技术学院上有一个任务,要实现一个日期选择组件,本文由此而来~ 看看需求 组件默认一直呈显示状态 通过某种方式选择年、月,选择了年月后,日期列表做相应切换 ...

    beanlam 评论0 收藏0
  • vue 手写一个时间选择

    摘要:实现代码于文章末尾处构思页面结构组件由输入框和日历面板组成,写好页面主体结构。输入框点击显示或隐藏日历面板方法改变布尔值控制日历面板的显示隐藏。同时,当组件销毁时,也要及时清除该监听器。 最近研究了 DatePicker 的实现原理后做了一个 vue 的 DatePicker 组件,今天带大家一步一步实现 DatePicker 的 vue 组件。 原理 DatePicker 的原理是—...

    sf_wangchong 评论0 收藏0

发表评论

0条评论

simpleapples

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<