[经典] easyui的datagrid的设置背景色之后,行默认的选中的背景色改变事件无效

在这次做项目中,就此问题,花费了我大半天时间,一度差点放弃,但作为后端开发人员,遇到难题怎能不攻克?贴出代码以供大家参考

先来一个style

<style>
    .redClass {
        background-color: pink;
        color: blue;
        font-weight: bold;
    }
</style>

然后我们来一个全局数据

 var rowIndexArr = [];

然后我们在datagrid 的rowStyler中这么写

rowStyler: function (index, row) {
                if (row.xxx != "是") {
                    //return 'background-color:pink;color:blue;font-weight:bold;';
                    return {class: 'redClass'}
                } else {
                    rowIndexArr.push(index);
                }
            },

然后在datagrid的onSelect中这么写

onSelect: function (index, row) {
                var opt = $(this).datagrid("options");
                var rows1 = opt.finder.getTr(this, "", "allbody", 1);
                var rows2 = opt.finder.getTr(this, "", "allbody", 2);
                if (rows1.length > 0) {
                    $(rows1).each(function () {
                        var tempIndex = parseInt($(this).attr("datagrid-row-index"));
                        if (tempIndex == index) {
                            $(this).removeClass("redClass");
                        } else {
                            var findIndex = $.inArray(tempIndex, rowIndexArr);
                            if (findIndex == -1) {
                                $(this).addClass("redClass");
                            }
                        }
                    });
                }
                if (rows2.length > 0) {
                    $(rows2).each(function () {
                        var tempIndex = parseInt($(this).attr("datagrid-row-index"));
                        if (tempIndex == index) {
                            $(this).removeClass("redClass");
                        } else {
                            var findIndex = $.inArray(tempIndex, rowIndexArr);
                            if (findIndex == -1) {
                                $(this).addClass("redClass");
                            }
                        }
                    });
                }

            }

然后就搞定啦,改变思路,其实一切就会变得很简单


版权声明:本文为mjxin123456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。