欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-25 13:25:47
...

参考:

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]);
                });
            }

效果:

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

相关标签: demo