EasyUI datebox 設置爲按月選擇

效果:html

 

jsp:jsp

<input id="date" name="date" class="easyui-datebox" data-options="buttonText: '時間', 
  buttonAlign: 'left', width: 180, height: 34, panelAlign: 'right', editable: false">

js:ide

$(function () {
    DateUtil.datebox4YM('date');
});
DateUtil.datebox4YM = function (id) {
    var db = $('#' + id);
    db.datebox({
        onShowPanel: function () {//顯示日趨選擇對象後再觸發彈出月份層的事件,初始化時沒有生成月份層
            span.trigger('click'); //觸發click事件彈出月份層
            if (!tds) setTimeout(function () {//延時觸發獲取月份對象,由於上面的事件觸發和對象生成有時間間隔
                tds = p.find('div.calendar-menu-month-inner td');
                tds.click(function (e) {
                    e.stopPropagation(); //禁止冒泡執行easyui給月份綁定的事件
                    var year = /\d{4}/.exec(span.html())[0]//獲得年份
                        , month = parseInt($(this).attr('abbr'), 10); //月份,這裏不須要+1
                    db.datebox('hidePanel')//隱藏日期對象
                        .datebox('setValue', year + '-' + month); //設置日期的值
                });
            }, 0);
            yearIpt.unbind();//解綁年份輸入框中任何事件
        },
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        formatter: function (d) { return d.getFullYear() + '-' + (d.getMonth() + 1);/*getMonth返回的是0開始的,忘記了。。已修正*/ }
    });
    var p = db.datebox('panel'), //日期選擇對象
        tds = false, //日期選擇對象中月份
        aToday = p.find('a.datebox-current'),
        yearIpt = p.find('input.calendar-menu-year'),//年份輸入框
        //顯示月份層的觸發控件
        span = aToday.length ? p.find('div.calendar-title span') ://1.3.x版本
            p.find('span.calendar-text'); //1.4.x版本
        p.find('div.calendar-header').hide();
    if (aToday.length) {//1.3.x版本,取消Today按鈕的click事件,從新綁定新事件設置日期框爲今天,防止彈出日期選擇面板

        aToday.unbind('click').click(function () {
            var now=new Date();
            db.datebox('hidePanel').datebox('setValue', now.getFullYear() + '-' + (now.getMonth() + 1));
        });
    }
}

 

 

// 轉載自 http://blog.sina.com.cn/s/blog_13554ebc70102wh4r.html   作出些許修改ui

相關文章
相關標籤/搜索