转换layUI的数据表格中的⽇期格式⽅法转换layUI的数据表格中的⽇期格式
前端jsp代码
<table class="layui-table" lay-data="{url:'/why123/user/findUserAll?userType=0', page:true, id:'idTest'}" lay-filter="demo">
<thead>
<tr>
<th lay-data="{type:'checkbox', fixed: 'left'}"></th>
<th lay-data="{field:'userName',align:'center'}">⽤户名</th>
<th lay-data="{field:'gender',align:'center',width:80}">性别</th>
<th lay-data="{field:'mobile',align:'center'}">⼿机号码</th>
<th lay-data="{field:'email',align:'center'}">电⼦邮箱</th>
<th lay-data="{field:'userheat',align:'center',width:100,sort:'true'}">⼈⽓</th>
<th lay-data="{field:'regDate',align:'center',templet:'<div>{{ Date) }}</div>'}">注册时间</th>
<th lay-data="{field:'lastLogin',align:'center',templet:'<div>{{ DateString(d.lastLogin) }}</div>'}">最后⼀次登录时间</th>
<th lay-data="{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}">操作</th>
</tr>
</thead>
</table>
js代码:
layui.use('laytpl', function(){
var laytpl = layui.laytpl;
//时间戳的处理
DateString = function(d, format){
if(d==null||d==''){
return "未登录";
}else{
var date =new Date(d.time)
,ymd = [
this.FullYear(), 4)
,
this.Month() + 1)
,this.Date())
]
,hms = [
string转date的方法this.Hours())
,this.Minutes())
,this.Seconds())
];
format = format || 'yyyy-MM-dd HH:mm:ss';
place(/yyyy/g, ymd[0])
.replace(/MM/g, ymd[1])
.
replace(/dd/g, ymd[2])
.replace(/HH/g, hms[0])
.replace(/mm/g, hms[1])
.replace(/ss/g, hms[2]);
}
}
//数字前置补零
layui.laytpl.digit = function(num, length, end){
var str = '';
num = String(num);
length = length || 2;
for(var i = num.length; i < length; i++){
str += '0';
}
return num < Math.pow(10, length) ? str + (num|0) : num;
};
});
注意:
1、需引⼊laytpl模块
2、后台返回json的String类型,否则如果数据为空,会出错,错误如下:
net.sf.json.JSONException: Object is null
net.sf.json.JSONNull.isEmpty(JSONNull.java:69)
flect.Method.invoke(Method.java:606)
com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:536)
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:557)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:466)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:29)
com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:132)
com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:94)
com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:557)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:466)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:29)
com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:251)
com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:846)
org.verter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:286)
org.verter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:106)
org.springframework.web.hod.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231) org.springframework.web.hod.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174)
org.hod.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
org.springframework.web.hod.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.hod.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
以上这篇转换layUI的数据表格中的⽇期格式⽅法就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。