设置layui动态表格某一行的背景色

       以下示例设置动态表格某一行的数据

ea.table.render({

                init: init,

                toolbar: ['refresh', 'delete','add'],

                cols: [[

                    {type: 'checkbox'},

                    {field: 'id', title: 'id'},

                    {field: 'cate.title', title: '分类'},

                    {field: 'image', title: '缩略图', templet: ea.table.image},

                    {field: 'name', title: '姓名'},

                    {field: 'phone', title: '电话'},

                    {field: 'license_number', title: '执业证号'},

                    {field: 'level', title: '等级'},

                    {field: 'province', title: '省'},

                    {field: 'city', title: '市'},

                    {field: 'good_sector', title: '擅长领域'},

                    {field: 'remark', title: '备注', templet: ea.table.text},

                    {field: 'create_time', title: '添加时间'},

                    {field: 'status', title: '状态', templet: ea.table.switch},

                    {field: 'price', title: '价格'},

                    {width: 250, title: '操作', templet: ea.table.tool},

                ]],

                done: function (res, curr, count) {

                    console.log(res);  //打印数据

                    console.log(curr);//打印当前行数

                    console.log(count);  //数据总条数

                    // 官方介绍  https://www.layui.com/doc/modules/table.html#templet

                    res.data.forEach(function (item, index) {   //循环数据

                        if (item.id == 1) {   //当数据id为1时

                            var tr = $(".layui-table").find("tbody tr[data-index='" + index + "']"); //查找某一行的数据

                            tr.css("background-color", "#98FB98"); //标记该行的背景色

                            // 给1-0-9列添加样式

                            tr.find(".laytable-cell-1-0-9").css("color","red");  //标记该行某一列的颜色

                        }

                    });

                }

            });

您可以使用 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表格设置一行的背景颜色了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值