layui 表格按照某一列的值划分颜色

本文详细介绍如何使用layui框架自定义表格样式的具体方法,包括背景颜色、文字颜色的动态设置,以及根据特定条件(如账号状态)改变行样式。通过实例展示了如何遍历表格数据,实现交替行颜色的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:

table.render({
          elem: "#test",
          // id: "table_cell_data",
          url: "http://localhost:3000/all",
          toolbar: "#toolbarDemo", //指向自定义工具栏模板选择器
          defaultToolbar: [], //除去打印,导出
          title: "用户数据表",
          cols: [[ ]],
          page: false,
          done: function (res, curr, count) {
            console.log(res.data);
            merge(res);
            $('tr').css({'background-color': '#009688', 'color': '#fff'});//表头样式修改

            var that = this.elem.next();
            console.log(this.elem)
            console.log(that)
            res.data.forEach(function (item, index) {
                if (item.accstatus === "封禁") {
                  var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                  tr.css("background-color", "yellow");
                  tr.find(".laytable-cell-1-0-9").css("color","red");

                } 
            });
          }
        });

我的改造

,done: function (res, curr, count) {
                var that = this.elem.next();
                var tempValue = '';
                var num = 1;
                var colors = [ "#FFC8B4",'#CCDDFF'];
                res.data.forEach(function (item, index) {
                    if (item.file != tempValue) {
                        tempValue = item.file;
                        num = num + 1
                    }
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    //tr.find(".laytable-cell-1-0-9").css("color","red");
                    tr.css("background-color", colors[num%2]);
                });
            }

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值