layui之table根据内容渲染行颜色

起因

最近有个webpacs项目需要在检查列表中快速判断那些人已经报告结果,未保告结果行显示为红色,已报告结果显示为蓝色,需要根据每个人的报告状态对行的数据进行判断来给行上色.
在这里插入图片描述

思考

由于layui的table是动态渲染,很多新手都无法对其进行更多的操作,为了能操作行的颜色改变我们需要获取页面生成的DOM,如何获取这个DOM?我们可以根据lay-id来确定是否是动态生成的table,找到table后我们就可以在table载入完成的时候用css去对行的颜色进行改变.

实现

给table行上色方法

   function Layui_SetDataTableRowColor(TabDivId, RowIndex, ColorString) {
            try {
                var div = $("[lay-id='demo'] tr:eq(" + RowIndex + ")");
                if (div != null) //找到对象了
                {
                    // console.log(div);
                    div.css("color", ColorString);

                } else {
                    console.log('没有找到对象');
                }
            } catch (e) {
                console.log(e.message);
            }
        }

table渲染方法

table.render({
            elem: '#demo'
            , url: '{:url("index/getapinfobyday")}' //数据接口
            , where: {date: '{$tdate}'}
            , height: 800
            , cols: [[ //表头

                 {field: 'xm', title: '姓名', width: 100,style:'color:#f68;'}
                , {field: 'hm', title: '号码', width: 180,style:'color:#f68;'}
                , {field: 'xb', title: '性别', width: 80}
                , {field: 'rq', title: '日期', width: 120}
                , {field: 'other', title: '联', width: 120}
                , {field: 'xxx', title: '套', width: 200 ,style:'color:#f68;font-weight:bold'}
                , {field: 'hid', title: '机', width: 100 ,style:'color:#f68;'}
                , {field: 'apd', title: '预约日期', width: 120}
                , {field: 'hxrq', title: '时间', width: 200}
                , {field: 'orderId', title: '订单号', width: 180}
                , {field: 'hOrderId', title: '订单号', width:180}
                , {fixed: 'right', field: 'captcha', title: '验证码', width: 80, minWidth: 80}
                , {fixed: 'right', field: 'tm', title: '系统条码', width: 132, minWidth: 132}
                , {fixed: 'right', field: 'flag', title: '报告状态', width: 100, minWidth: 100}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 300}
            ]]
            , done: function (res, curr, count) {
				//循环表数据根据flag状态给行上色
                $.each(res['data'], function (i, j) {
                    console.log(i + ' -454- ' + j['tm']);

                    if (j['flag'] === '') {

                        // console.log(i + ' -458- ' + 'jdjg is null');
                        Layui_SetDataTableRowColor('table', i + 1, '#b1070a');
                    } else {
                        // console.log(i + ' -458- ' + 'jdjg is not null');
                        Layui_SetDataTableRowColor('table', i + 1, '#0e1ab1');
                    }

                });


            }

        });
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用 layui 的表格渲染功能,通过设置单元格的样式来实现表格某一的背景颜色。具体实现方式如下: 1. 在表格渲染之前,定义一个回调函数,用于设置单元格的样式。 ```javascript var table = layui.table; table.render({ elem: '#demo', cols: [[ //表头 {field: 'id', title: 'ID', width:80}, {field: 'username', title: '用户名', width:120}, {field: 'sex', title: '性别', width:80, sort: true}, {field: 'city', title: '城市', width:100}, {field: 'sign', title: '签名', width: 200}, {field: 'experience', title: '积分', width: 80, sort: true}, {field: 'score', title: '评分', width: 80, sort: true}, {field: 'classify', title: '职业', width: 100}, {field: 'wealth', title: '财富', width: 135, sort: true}, {fixed: 'right', title:'操作', toolbar: '#barDemo', width:150} ]], data: tableData, done: function(res, curr, count) { // 设置第二的背景颜色为 #F2F2F2 var trElem = $('.layui-table tbody tr:nth-child(2)'); trElem.css('background-color', '#F2F2F2'); } }); ``` 在上述代码中,通过 done 回调函数来实现对表格的单元格样式进设置。 2. 在回调函数中通过 jQuery 选择器选中表格中的某一,并设置该单元格的背景颜色。 ```javascript var trElem = $('.layui-table tbody tr:nth-child(2)'); trElem.css('background-color', '#F2F2F2'); ``` 在上述代码中,通过 jQuery 选择器选中表格中的第二,并设置该单元格的背景颜色为 #F2F2F2。 通过以上实现方式,您就可以在 layui 的表格中设置某一的背景颜色了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值