From db3a486b572519f931171ac3de9e83af3f8ac67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E4=BA=91?= <42031039+supermajinbo@users.noreply.github.com> Date: Thu, 2 Aug 2018 18:26:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=90=8E=EF=BC=8C=E9=80=9A=E8=BF=87=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E4=BF=AE=E6=94=B9format=E5=90=8E=E4=B8=8D=E7=94=9F?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit var startDate = laydate.render({ elem: '#startDate', //指定元素 #startDate是一个input type: 'date', format: 'yyyy年MM月dd日', isInitValue: true, //是否允许填充初始值,默认为 true done: function(value, date, endDate) { /*回调里修改了type 和format 发现type的修改生效了,但是formar的修改没有生效,看源码发现是没有更新this.format、that.EXP_IF和that.EXP_SPLIT。(PS:这里只是举例,实际项目中是通过别的按钮触发改变type和format 事件的。) */ startDate .config.type = 'month'; startDate .config.format = 'yyyy年MM月'; } }); --- src/laydate.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/laydate.js b/src/laydate.js index 0a75401..ae0aa3c 100644 --- a/src/laydate.js +++ b/src/laydate.js @@ -1362,6 +1362,35 @@ : (options.range ? lay.extend({}, that.startDate, that.startTime) : options.dateTime)) ,format = that.format.concat(); + //#region 因为that.config可能被外部改变了,所以这里需要根据options.format重新生成正则表达式和返回结果的格式 by 凌云 2018-08-02 + that.EXP_IF = ''; + that.EXP_SPLIT = ''; + var dateType = 'yyyy|y|MM|M|dd|d|HH|H|mm|m|ss|s'; + format = options.format.match(new RegExp(dateType + '|.', 'g')) || []; + that.format = format; + lay.each(that.format, function(i, item) { + var EXP = new RegExp(dateType).test(item) ? + '\\d{' + function() { + if (new RegExp(dateType).test(that.format[i === 0 ? i + 1 : i - 1] || '')) { + if (/^yyyy|y$/.test(item)) return 4; + return item.length; + } + if (/^yyyy$/.test(item)) return '1,4'; + if (/^y$/.test(item)) return '1,308'; + return '1,2'; + }() + '}' : + '\\' + item; + that.EXP_IF = that.EXP_IF + EXP; + that.EXP_SPLIT = that.EXP_SPLIT + '(' + EXP + ')'; + }); + that.EXP_IF = new RegExp('^' + ( + options.range ? + that.EXP_IF + '\\s\\' + options.range + '\\s' + that.EXP_IF : + that.EXP_IF + ) + '$'); + that.EXP_SPLIT = new RegExp('^' + that.EXP_SPLIT + '$', ''); + //#endregion + //转义为规定格式 lay.each(format, function(i, item){ if(/yyyy|y/.test(item)){ //年 @@ -1863,4 +1892,4 @@ }() ); -}(); \ No newline at end of file +}();