在kendogrid中通过数据条件禁用命令

时间:2016-05-02 14:02:59

标签: c# asp.net-mvc kendo-ui kendo-grid kendo-asp.net-mvc

如何通过给定的布尔值禁用行上的删除按钮?

鉴于此示例模型:

public class Example
{
    public bool CanDeleted {get; set;} //Delete Button only if true
    //...        
}

KendoGrid<Example>

columns.Command(c => c.Destroy()); //I want something something like m => m.CanDeleted

2 个答案:

答案 0 :(得分:1)

感谢@Eldho,他指出了我right solution

使用Javascript:

function onDataBound() {
    var grid = $("#GridID").data("kendoGrid"); //Set GridID
    var gridData = grid.dataSource.view();
    for (var i = 0; i < gridData.length; i++) {
        var gridItem = gridData[i];
        if (!gridItem.CanBeDeleted) { //Condition
            grid.table.find("tr[data-uid='" + gridItem.uid + "']").find(".k-grid-delete").hide(); //Remove button
        }

        //Second Iteration, if we have grouped columns
        if (gridItem.items) {
            for (var j = 0; j < gridItem.items.length; j++) {
                var gridSubItem = gridItem.items[j];
                if (!gridSubItem.CanBeDeleted) { //Condtion
                    grid.table.find("tr[data-uid='" + gridSubItem.uid + "']").find(".k-grid-delete").hide(); //Remove button
                }
            }
        }
    }
}

KendoGrid Helper:

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

请查看我的other answer以获得更好的解决方案。

答案 1 :(得分:0)

通过对MVC的更多了解来回顾一下情况之后,我发现了一个非常好的解决方案,使用扩展方法:

public static class KendoExtensions
{
    public static GridTemplateColumnBuilder<TModel> DestroyConditional<TModel>(this GridColumnFactory<TModel> factory, Expression<Func<TModel, bool>> expression)
        where TModel : class
    {
        var template = "# if (" + ExpressionHelper.GetExpressionText(expression) +") { # <a class=\"k-button k-button-icontext k-grid-delete\"><span class=\"k-icon k-delete\"></span>" + TextStrings.Delete + "</a># } #";

        return factory.Template(e => "").ClientTemplate(template).Title("");
    }
}

随意将TextStrings.Delete替换为您感觉良好的任何内容。它简单的我的T4本地化逻辑。

现在在您的网格定义中,它真的只是:

.Columns(columns =>
{
    columns.DestroyConditional(c => c.CanBeDeleted).Width(120);
}))