layui table数据行变色、layui 表格数据行变色、js求两个时间差天数

关键函数:求两个时间相差天数

function getDiffDay(date1, date2){
                    // 计算两个日期之间的差值
                    let totalDays,diffDate
                    let myDate_1 = Date.parse(date_1)
                    let myDate_2 = Date.parse(date_2)
                    // 将两个日期都转换为毫秒格式,然后做差
                    //diffDate = Math.abs(myDate_1 - myDate_2) // 取相差毫秒数的绝对值
                    diffDate = myDate_1 - myDate_2
                   
                    totalDays = Math.floor(diffDate / (1000 * 3600 * 24)) // 向下取整
                   
                    return totalDays// 相差的天数
                  }

--------// 行变色

that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#FA8072");

业务要求,到期提醒(变黄),过期提醒(变红),下面是具体代码实现:

var tableIns = table.render({
        elem: '#tableTextile',
        url: url,
        where: {
            state:Request.state,
            companyCode:loginInfo.companyCode,
            orderCode:Request.orderCode
        },
        method: 'post',
        headers: {
            "Authorization": sessionStorage.getItem(g_const.sessionTokenHeader)
        },
        title: '耗材类别信息表',
        sink: 'line',
        toolbar: '#toolbar', //开启头部工具栏,并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        height: 'full-110',
        cols: [
            [ 
            {
                field: 'csmbCode',
                title: '耗材条码',
                width: 190,
                align: 'center'
            }, {
                field: 'classifyName',
                title: '耗材名称',
                width: 180,
                align: 'center'
            }, {
                field: 'deptName',
                title: '部门名称',
                width: 120,
                align: 'center'
            }, {
                field: 'inHouseCode',
                title: '院内码',
                width: 190,
                align: 'center'
            }, {
                field: 'stateValue',
                title: '耗材状态',
                align: 'center',
                width: 120
            }, {
                field: 'changeTime',
                title: '操作时间',
                align: 'center',
                width: 170
            }
            ,{
                field: 'orderCode',
                title: '订单编号',
                align: 'center',
                width: 190
            },
            {
                field: 'spec',
                title: '规格',
                align: 'center',
                width: 80
            }, {
                field: 'model',
                title: '型号',
                align: 'center',
                width: 80
            }, {
                field: 'batchNumber',
                title: '批号',
                width: 190,
                align: 'center'
            }, {
                field: 'prodDate',
                title: '生产日期',
                align: 'center',
                width: 110,
                unresize: true,
                templet: function(d) {
                    if (d.prodDate != undefined && d.prodDate != null) {
                        var date = new Date(Date.parse(d.prodDate.replace(/-/g, "/")));
                        return date.Format("yyyy-MM-dd");
                    }
                }
            }, {
                field: 'expireDate',
                title: '有效期',
                align: 'center',
                width: 110,
                unresize: true,
                templet: function(d) {
                    if (d.expireDate != undefined && d.expireDate != null) {
                        var date = new Date(Date.parse(d.expireDate.replace(/-/g, "/")));
                        return date.Format("yyyy-MM-dd");
                    }
                }
            },{
                field: 'terminalCode',
                title: '设备号',
                align: 'center',
                width: 120
               
            }, 
            {
                field: 'terminalName',
                title: '设备名称',
                align: 'center',
                width: 160
            }, {
                field: 'boxId',
                title: '箱号',
                width: 60,
                align: 'center'
            }]
        ],
        even: true, //开启隔行背景
        page: true, //开启分页
        parseData: function (res) {
            return {
                'code': res.code,
                'msg': res.msg,
                'count': res.code == 0 ? res.data.count : 0,
                'data': res.code == 0 ? res.data.records : null
            }
        },
        done: function (res, curr, count) { // 表格渲染完成之后的回调
            var that = this.elem.next();
            var preliminaryDayList = res.data;  // 拿到后台给的数据
            preliminaryDayList.forEach(function (item,index) { // 循环取后台给的条件的值
            
                let time = new Date();

                let time1 = new Date(item.expireDate);
                
                function timestampToTime(times) {
                    let time = times[1]
                    let mdy = times[0]
                    mdy = mdy.split('/')
                    let month = parseInt(mdy[0]);
                    let day = parseInt(mdy[1]);
                    let year = parseInt(mdy[2])
                    return year + '-' + month + '-' + day + ' ' + time
                }
                let nowTime = timestampToTime(time.toLocaleString('en-US',{hour12: false}).split(" "))
              
                function getDiffDay(date_1, date_2){
                    // 计算两个日期之间的差值
                    let totalDays,diffDate
                    let myDate_1 = Date.parse(date_1)
                    let myDate_2 = Date.parse(date_2)
                    // 将两个日期都转换为毫秒格式,然后做差
                    //diffDate = Math.abs(myDate_1 - myDate_2) // 取相差毫秒数的绝对值
                    diffDate = myDate_1 - myDate_2
                   
                    totalDays = Math.floor(diffDate / (1000 * 3600 * 24)) // 向下取整
                   
                    return totalDays// 相差的天数
                  }

                let day= getDiffDay(time1,nowTime);
               
                if (day<0) {
                    that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#FA8072"); // 行变色
                }
                if (day<7&& day>0) {
                    that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#FFFF00"); // 行变色
                }
            });

        }
    });
----------------------遇到var that = this.elem.next();报错用下面的代码代替

 var tableContainer = $(this.elem).closest('#tableDepositMaster');

 var that = tableContainer.next();

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值