博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
layer---table 处理 方法设定使用 时间戳转换
阅读量:4356 次
发布时间:2019-06-07

本文共 7916 字,大约阅读时间需要 26 分钟。

layui.use(['table','util','form'], function(){           var table = layui.table ,util=layui.util;   var form =layui.form;  //第一个实例  table.render({     elem: '#demo' ,//指定原始 table 容器的选择器或 DOM,方法渲染方式必填    //width    Number    设定容器宽度。table容器的默认宽度是跟随它的父元素铺满,你也可以设定一个固定值,当容器中的内容超出了该宽度时,会自动出现横向滚动条     height: 500,     url: 'xyhai.php?s=/Tzlist/user' , //数据接口     page: true , //开启分页     cols: [[ //表头      {field: 'userid', title: '用户编号',  sort: true, fixed: 'left'}      ,{field: 'loginname', title: '用户名', }      ,{field: 'usermobile', title: '手机号码',  sort: true}      ,{field: 'ifReal', title:'性别', width:85, templet: '#switchTpl', unresize: true}       ,{field: 'lastlogintime', title: '登陆', }      ,{field: 'addtimes', title: '注册',  sort: true, templet: function(d) {
return util.toDateString(d.ctime*1000); } }// 调用 layer内置 时间转换util方法 ,{field: 'pc', title: '设备', templet: '#pcTpl', } ,{field: 'ip', title: '地址', } ]] }); //监听锁定操作 form.on('checkbox(lockDemo)', function(obj){ layer.tips(this.value + ' ' + this.name + ':'+ obj.elem.checked, obj.othis); });}); php 返回数组的 定义格式
{"code":0,"msg":"","count":1000,"data":[{"id":10000,"username":"user-0","sex":"女","city":"城市-0","sign":"签名-0","experience":255,"logins":24,"wealth":82830700,"classify":"作家","score":57},{"id":10001,"username":"user-1","sex":"男","city":"城市-1","sign":"签名-1","experience":884,"logins":58,"wealth":64928690,"classify":"词人","score":27},{"id":10002,"username":"user-2","sex":"女","city":"城市-2","sign":"签名-2","experience":650,"logins":77,"wealth":6298078,"classify":"酱油","score":31},{"id":10003,"username":"user-3","sex":"女","city":"城市-3","sign":"签名-3","experience":362,"logins":157,"wealth":37117017,"classify":"诗人","score":68},{"id":10004,"username":"user-4","sex":"男","city":"城市-4","sign":"签名-4","experience":807,"logins":51,"wealth":76263262,"classify":"作家","score":6},{"id":10005,"username":"user-5","sex":"女","city":"城市-5","sign":"签名-5","experience":173,"logins":68,"wealth":60344147,"classify":"作家","score":87},{"id":10006,"username":"user-6","sex":"女","city":"城市-6","sign":"签名-6","experience":982,"logins":37,"wealth":57768166,"classify":"作家","score":34},{"id":10007,"username":"user-7","sex":"男","city":"城市-7","sign":"签名-7","experience":727,"logins":150,"wealth":82030578,"classify":"作家","score":28},{"id":10008,"username":"user-8","sex":"男","city":"城市-8","sign":"签名-8","experience":951,"logins":133,"wealth":16503371,"classify":"词人","score":14},{"id":10009,"username":"user-9","sex":"女","city":"城市-9","sign":"签名-9","experience":484,"logins":25,"wealth":86801934,"classify":"词人","score":75},{"id":10010,"username":"user-10","sex":"女","city":"城市-10","sign":"签名-10","experience":1016,"logins":182,"wealth":71294671,"classify":"诗人","score":34},{"id":10011,"username":"user-11","sex":"女","city":"城市-11","sign":"签名-11","experience":492,"logins":107,"wealth":8062783,"classify":"诗人","score":6},{"id":10012,"username":"user-12","sex":"女","city":"城市-12","sign":"签名-12","experience":106,"logins":176,"wealth":42622704,"classify":"词人","score":54},{"id":10013,"username":"user-13","sex":"男","city":"城市-13","sign":"签名-13","experience":1047,"logins":94,"wealth":59508583,"classify":"诗人","score":63},{"id":10014,"username":"user-14","sex":"男","city":"城市-14","sign":"签名-14","experience":873,"logins":116,"wealth":72549912,"classify":"词人","score":8},{"id":10015,"username":"user-15","sex":"女","city":"城市-15","sign":"签名-15","experience":1068,"logins":27,"wealth":52737025,"classify":"作家","score":28},{"id":10016,"username":"user-16","sex":"女","city":"城市-16","sign":"签名-16","experience":862,"logins":168,"wealth":37069775,"classify":"酱油","score":86},{"id":10017,"username":"user-17","sex":"女","city":"城市-17","sign":"签名-17","experience":1060,"logins":187,"wealth":66099525,"classify":"作家","score":69},{"id":10018,"username":"user-18","sex":"女","city":"城市-18","sign":"签名-18","experience":866,"logins":88,"wealth":81722326,"classify":"词人","score":74},{"id":10019,"username":"user-19","sex":"女","city":"城市-19","sign":"签名-19","experience":682,"logins":106,"wealth":68647362,"classify":"词人","score":51},{"id":10020,"username":"user-20","sex":"男","city":"城市-20","sign":"签名-20","experience":770,"logins":24,"wealth":92420248,"classify":"诗人","score":87},{"id":10021,"username":"user-21","sex":"男","city":"城市-21","sign":"签名-21","experience":184,"logins":131,"wealth":71566045,"classify":"词人","score":99},{"id":10022,"username":"user-22","sex":"男","city":"城市-22","sign":"签名-22","experience":739,"logins":152,"wealth":60907929,"classify":"作家","score":18},{"id":10023,"username":"user-23","sex":"女","city":"城市-23","sign":"签名-23","experience":127,"logins":82,"wealth":14765943,"classify":"作家","score":30},{"id":10024,"username":"user-24","sex":"女","city":"城市-24","sign":"签名-24","experience":212,"logins":133,"wealth":59011052,"classify":"词人","score":76},{"id":10025,"username":"user-25","sex":"女","city":"城市-25","sign":"签名-25","experience":938,"logins":182,"wealth":91183097,"classify":"作家","score":69},{"id":10026,"username":"user-26","sex":"男","city":"城市-26","sign":"签名-26","experience":978,"logins":7,"wealth":48008413,"classify":"作家","score":65},{"id":10027,"username":"user-27","sex":"女","city":"城市-27","sign":"签名-27","experience":371,"logins":44,"wealth":64419691,"classify":"诗人","score":60},{"id":10028,"username":"user-28","sex":"女","city":"城市-28","sign":"签名-28","experience":977,"logins":21,"wealth":75935022,"classify":"作家","score":37},{"id":10029,"username":"user-29","sex":"男","city":"城市-29","sign":"签名-29","experience":647,"logins":107,"wealth":97450636,"classify":"酱油","score":27}]}
 
//  将格式化日期转为时间戳1 function timeToStamp(datetime) {
if(datetime == '') {
return ''; }else{
var day = new Date(datetime) var stamp = Date.parse(day)/1000; return stamp; } } // 将时间戳转为格式化时间 function stampToTime(timestamp) {
if(timestamp == 0) {
return ""; }else{
var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear(); var M = date.getMonth()+ 1; var D = date.getDate(); var h = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); var datestring = Y + "-"; if (M < 10) datestring += "0"; datestring += M + "-"; if (D < 10) datestring += "0"; datestring += D + " "; if (h < 10) datestring += "0"; datestring += h + ":"; if (m < 10) datestring += "0"; datestring += m + ":"; if (s < 10) datestring += "0"; datestring += s; return datestring; } } 后台传值为时间戳,前台显示日期格式
{field: 'createtime', title: '申请时间', sort: true, templet: function(d){return stampToTime(d.createtime)}},
前台往后台传值为时间戳
where.createstime = timeToStamp(data.field.createstime);
定义 layui.laytpl.toDateString 自己的方法 并使用 2  在定义
//时间戳的处理2 layui.laytpl.toDateString = function(d, format){
var date = new Date(d || new Date()) ,ymd = [ this.digit(date.getFullYear(), 4) ,this.digit(date.getMonth() + 1) ,this.digit(date.getDate()) ] ,hms = [ this.digit(date.getHours()) ,this.digit(date.getMinutes()) ,this.digit(date.getSeconds()) ]; format = format || 'yyyy-MM-dd HH:mm:ss'; return format.replace(/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; }; 在列模板中调用时间戳的处理方法
table.render({ //其它参数在此省略   elem: '#demo'   ,cols: [[     {field: 'createTime', title: '创建时间', templet: '
{
{ layui.laytpl.toDateString(d.time) }}
'} ]] });

转载于:https://www.cnblogs.com/benpaodegegen/p/9159487.html

你可能感兴趣的文章
【补hackbar的坑】关于hackbar需要钱的补救措施
查看>>
纤程与Quasar
查看>>
MySQL的一个麻烦事
查看>>
Uri、URL和URN三者的区别
查看>>
数据字典的转换
查看>>
二维数组按照指定的字段排序的函数
查看>>
在IAR下通过Jlink将程序直接下载到Flash指定地址
查看>>
POJ2560-雀斑(Freckles)【图论,并查集,最小生成树,KURUSKAL】
查看>>
[Angular] Tree shakable provider
查看>>
[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript
查看>>
[Angular 2] Select From Multiple Nested Angular 2 Elements
查看>>
C# 中的委托和事件[转帖]
查看>>
图的遍历(bfs+dfs)模板
查看>>
angular service 进行组件通信
查看>>
linux安装Mac的默认Monaco字体
查看>>
java语言的特点
查看>>
关于动态添加iview admin路由以及刷新侧边栏
查看>>
ApplicationInsights的探测器尝鲜
查看>>
java 解析Json格式数据
查看>>
unix中的线程池技术详解
查看>>