layui 表格按照某一列的值劃分顏色

參考:

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

效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章