Kendo ui 如何根据数据在特定行上显示KendoUI网格中的编辑按钮

Kendo ui 如何根据数据在特定行上显示KendoUI网格中的编辑按钮,kendo-ui,kendo-grid,kendo-asp.net-mvc,Kendo Ui,Kendo Grid,Kendo Asp.net Mvc,我在asp.net应用程序中使用KendoUI来显示网格 我希望用户能够根据每一行的状态编辑这些行 网格如下所示: @(Html.Kendo().Grid<UIMuModel>() .Name("Grid") .Columns(columns => { columns.Select().Width("50px"); columns.Bound(c => c.Name).Width("120px"); columns.Boun

我在asp.net应用程序中使用KendoUI来显示网格

我希望用户能够根据每一行的状态编辑这些行

网格如下所示:

@(Html.Kendo().Grid<UIMuModel>()
.Name("Grid")
.Columns(columns =>
{
        columns.Select().Width("50px");
        columns.Bound(c => c.Name).Width("120px");
        columns.Bound(c => c.State).Width("120px");
        if (Model.AllowEdit)
        {
            columns.Command(command => { command.Edit(); });
        }
})            
.Editable(editable => editable.Mode(GridEditMode.InLine))
@(Html.Kendo().Grid())
.名称(“网格”)
.列(列=>
{
columns.Select().Width(“50px”);
columns.Bound(c=>c.Name).Width(“120px”);
columns.Bound(c=>c.State).Width(“120px”);
if(允许的型号)
{
Command(Command=>{Command.Edit();});
}
})            
.Editable(可编辑=>Editable.Mode(GridEditMode.InLine))

我可以为整个网格启用或禁用编辑命令,但我不知道如何在每行的基础上执行此操作:仅当
状态为“打开”时,才在此行显示编辑按钮。

数据绑定后,您需要在客户端执行此操作。首先,创建一个数据绑定处理程序:

<script>        
      function dataBound(e) {
           var grid = this;

           grid.tbody.find("tr[role='row']").each(function () {
               var model = grid.dataItem(this);

               if (!model.AllowEdit) {
                   $(this).find(".k-grid-edit").addClass("k-state-disabled");
               }
           });
       }
</script>

函数数据绑定(e){
var grid=此;
grid.tbody.find(“tr[role='row']”){
var模型=grid.dataItem(本);
如果(!model.AllowEdit){
$(this.find(“.k-grid-edit”).addClass(“k-state-disabled”);
}
});
}
…然后将此事件处理程序添加到小部件:

    @(Html.Kendo().Grid<UIMuModel>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Select().Width("50px");
        columns.Bound(c => c.Name).Width("120px");
        columns.Bound(c => c.State).Width("120px");
        if (Model.AllowEdit)
        {
            columns.Command(command => { command.Edit(); });
        }
    })            
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Events(e => e.DataBound("dataBound"))
@(Html.Kendo().Grid())
.名称(“网格”)
.列(列=>
{
columns.Select().Width(“50px”);
columns.Bound(c=>c.Name).Width(“120px”);
columns.Bound(c=>c.State).Width(“120px”);
if(允许的型号)
{
Command(Command=>{Command.Edit();});
}
})            
.Editable(可编辑=>Editable.Mode(GridEditMode.InLine))
.Events(e=>e.DataBound(“DataBound”))

我找到了解决方案:

我有一个javascript函数,将模型作为参数,并返回一个布尔值,以定义该行是否可以编辑:

function isLineEditable(model) {
    return model.State === "open";
}
在命令定义中,我们可以设置一个javascript函数来调用,设置按钮是否可见:

columns.Command(command => { command.Edit().Visible("isLineEditable"); });

我找到了一个更简单的方法,但我仍然支持你的答案,因为它很有效,而且它比我的更适合定制。这肯定更容易。谢谢分享!