详解easyui基于 layui.laydate日期扩展组件

2020-05-24 21:25:39易采站长站整理

本人后端开发码农一个,公司前端忙的一逼,项目使用的是easyui组件,其自带的datebox组件使用起来非常不爽,主要表现在

1、自定义显示格式很麻烦

2、选择年份和月份用户体验也不好

网上有关于和 My97DatePicker 结合的例子,但感觉也用的不是很爽。

发现国内的layDate体验非常满意,所以萌生出想把这两个组件组合起来的想法,具体代码如下,本人非前端,所以只是实现了基本功能,大神勿喷哦。

easyUI版本:V1.5.2

layDate版本:V5.0


/*
基于laydate日期扩展组件
*/
(function ($) {
function createBox(target) {
var state = $.data(target, 'laydate');
var opts = state.options;
$(target).addClass('datebox-f').textbox($.extend({}, opts, {
editable: false,
icons: [{ iconCls: 'combo-arrow' }] }));
$(target).next("span.textbox").addClass('datebox');
laydate.render({
elem: '#' + $(target).next("span.textbox").children(":text").attr("id"), //指定元素,
format: opts.format,
type: opts.datetype,
min: opts.min,
max: opts.max,
zIndex :opts.zIndex ,
range :opts.range ,
theme: opts.theme,
calendar: opts.calendar,
mark: opts.mark,
done: function (value, date, endDate) {
$(target).textbox('setValue', value);
opts.onSelect.call(target, value, date, endDate);
},
change: function(value, date, endDate) {
opts.onChange.call(target, value, date, endDate);
}
});
}
$.parser.plugins.push("laydate");//注册扩展组件
$.fn.laydate = function (options, param) {
if (typeof options == 'string') {
var method = $.fn.laydate.methods[options];
if (method) {
return method(this, param);
} else {
return this.textbox(options, param);
}
}
options = options || {};
return this.each(function () {
var state = $.data(this, 'laydate');
if (state) {
$.extend(state.options, options);
} else {
$.data(this, 'laydate', {
options: $.extend({}, $.fn.laydate.defaults, $.fn.laydate.parseOptions(this), options)
});
}
createBox(this);
});
};
$.fn.laydate.methods = {
options: function (jq) {
var copts = jq.textbox('options');
return $.extend($.data(jq[0], 'laydate').options, {
width: copts.width,
height: copts.height,
originalValue: copts.originalValue,
disabled: copts.disabled,
readonly: copts.readonly
});
}
};
$.fn.laydate.parseOptions = function (target) {
return $.extend({}, $.fn.textbox.parseOptions(target));