记一次重写easyui的datetimebox控件的小片段

因为项目中服务器使用UTC时间,而easyui的datetimebox点击今天获取的是本地时间,存在时差。所以扩展了easyui的datetimebox控件,具体如下:

easyui官方的datetimebox控件如下:

扩展代码如下:

 1 //重写datebox今天按钮事件,点击设置事件从服务器取
 2 var formatterNowDateBox = "loginBegin,loginEnd,userCreatedBegin,userCreatedEnd".split(",");
 3  $(function(){
 4     var buttons = $.extend([], $.fn.datetimebox.defaults.buttons);
 5     buttons.splice(0, 1, {
 6         text: '<spring:message code="今天" />',
 7         handler: function(target){
 8             for(var i = 0; i <formatterNowDateBox.length;i++){
 9             if(target.id == formatterNowDateBox[i]){
10                 $.ajax({
11                     type:"GET",
12                     dataType:"json",
13                     url:"127.0.0.1:80/server/getServerTime.htm?dateTimeBoxId="+formatterNowDateBox[i],
14                     success:function(res){
15                         $("#" + res.dateTimeBoxId).datetimebox("setValue",res.date);
16                         //hidePanpel继承自combo控件
17                         $("#" + res.dateTimeBoxId).datetimebox("hidePanel");
18                     }
19                 });
20                 }
21             }
22         }
23     });
24     for(var i = 0; i <formatterNowDateBox.length;i++){
25         $('#'+formatterNowDateBox[i]).datetimebox({
26             buttons: buttons
27         });
28     }
29 }) 

因为项目中采用了国际化,所以使用了spring message标签,

formatterNowDateBox 变量定义了需要重写的控件id,实际项目中一个页面不会只有一个日期选择框,如果每个日期框都去重写一次代码看起来很冗余,所以定义了数组,存放id,同时ajax请求的时候把id传过去,返回的时候带上id就可以了。

因为是本地测试所以ajax请求的url写死了。

原文地址:https://www.cnblogs.com/FlyHeLanMan/p/6738430.html