在Kendo MVC网格中更改行颜色,使用RowAction

5
我希望使用RowAction和lambda表达式来设置网格中几行数据的背景颜色,使其更加突出。
<%: Html.Kendo().Grid<HomeController.SuccessfulBuildsByDevice>()
                        .Name("Grid")
                        .Columns(columns =>
                        {                   
                            columns.Bound(p => p.A);
                            columns.Bound(p => p.B);
                        })
                        .Scrollable()
                        .Sortable()
                        .Filterable()
                        .RowAction(row =>
                            {
                                if(row.DataItem.A > row.DataItem.B)
                                    row.HtmlAttributes["style"] = "background:red";
                            })
                        .HtmlAttributes(new { style = "height:500" })  
                        .DataSource(dataSource => dataSource
                            .Ajax()
                            .Read(read => read.Action("_GetData", "Home"))
                            .ServerOperation(false)
                        )
                    %>

然而,当我使用以上代码时,RowAction()似乎没有被调用。我尝试设置断点等调试方法,但是我是否在RowAction()的预期使用中遗漏了些什么?是否有人看到了明显的问题?
2个回答

10

这个答案对我有用。我没有使用 .Ajax() 方法。 - Dun
1
数小时的研究和挖掘,最终找到了答案! - Bmize729

5

今天我遇到了这个问题,为了节省时间,以下是根据 stuck 的解释对我有用的简短答案:

将此内容添加到网格中。

.Events(e => e.DataBound("onDataBound"))

将此 JavaScript 添加到网格上方

function onDataBound() {
    // get the grid
    var grid = this;
    // iterate through each row
    grid.tbody.find('>tr').each(function () {
        // get the row item
        var dataItem = grid.dataItem(this);
        // check for the condition
        if (dataItem.IsArchived) {
            // add the formatting if condition is met
            $(this).addClass('bgRed');
        }
    })
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接