5

I am new to Kendo MVC components as well as to jQuery.

I am building a Kendo grid. I would like to hide the destroy (delete) command in the grid on page load. After that, when I click a button on the page, it should be visible.

Kendo grid:

@(Html.Kendo().Grid<Model>() 
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(product => product.DESCRIPTION).Title("Description");
        columns.Bound(product => product.CODE).Title("Description");
        columns.Command(commands =>
        {
            commands.Destroy().HtmlAttributes(new { id = "buttondelete" }); 
        }).Title("Operations");
    })
    .ToolBar(toolbar =>
    {
        toolbar.Create().Text("Add Records"); 
        toolbar.Save(); 
    })
                                 
    .Editable(editable => editable.Mode(GridEditMode.InCell)) 
    .Pageable(pager => pager
        .PageSizes(true)
        .Input(true)
        .Refresh(true)
    )
    .DataSource(dataSource => dataSource
        .Ajax()
        .ServerOperation(true)
        .Events(events => events.Error("onError"))
        .Model(model =>
        {
            model.Id(product => product.ID); // Specify the property which is the unique identifier of the model
            model.Field(p => p.DESCRIPTION).Editable(false);
            model.Field(product => product.CODE).Editable(false);
        })
        .Create(create => create.Action("a", "www")) 
        .Read(read => read.Action("b", "www"))  
        .Update(update => update.Action("c", "www"))  
        .Destroy(destroy => destroy.Action("d", "www")) 
    )
)

JS:

<script type="text/javascript">

    $(document).ready(function () {
        //$("#grid").find(".k-grid-delete").hide() // hide delete button
        $("#grid").find(".k-toolbar").hide(); // hide toolbar
        $(".k-grid-delete", "#grid").hide();
    });
    
    $('#EnableEdit').click(function () {
        $("#grid").find(".k-toolbar").show();
        
        // $(".k-grid-delete", "#grid").show();
        var grid = $("#grid").data("kendoGrid");
        grid.dataSource.at(0).fields["CODE"].editable = true;
        grid.dataSource.at(0).fields["DESCRIPTION"].editable = true;
    });

</script>

Edit: changed some parts according to first answer. Now $(".k-grid-delete", "#grid").hide(); cannot hide k.grid-delete class. I guess JavaScript is loaded before the kendo grid is created. When I use it inside the click function of the edit button, it hides the delete buttons.

4

2 回答 2

4

如果您id对每一列使用相同的元素,那么您将有许多相同的元素,id这是不合法的。尝试使用标识delete按钮的 CSS 类属性,并在创建(页面加载)时隐藏它,然后单击显示它。

隐藏它们的代码

$(".k-grid-delete", "#grid").hide();

显示它们的代码

$('#EnableEdit').click(function () {
    $(".k-grid-delete", "#grid").show();
});

这里的 JSFiddle 示例:http: //jsfiddle.net/OnaBai/pSgeD/

于 2013-11-11T17:34:51.797 回答
3

要更改 kendo-grid 设置,您必须重新创建网格。如果您绑定到远程数据,您可以注释掉“for offlide data”。

setGridReadOnly: function (gridId, isReadOnly) {

    var grid;

    grid = $("#" + gridId).data("kendoGrid");

    var myOpt = grid.options;
    myOpt.editable.create = !isReadOnly;
    myOpt.editable.destroy = !isReadOnly;
    myOpt.editable.update = !isReadOnly;
    if (isReadOnly == true)
        myOpt.editable.mode = "null";
    else
        myOpt.editable.mode = "inline";

    //for offlide data.
    var data = grid._data;
    //

    grid.destroy();

    $("#" + gridId).kendoGrid(myOpt).data("kendoGrid");

    //for offlide data.
    $("#" + gridId).data("kendoGrid").dataSource.data(data);
    //

    if (isReadOnly == true) {
        $("#" + gridId).find(".k-grid-delete").hide();
        $("#" + gridId).find(".k-grid-edit").hide();
        $("#" + gridId).find(".k-grid-add").hide();
    } else {
        $("#" + gridId).find(".k-grid-delete").show();
        $("#" + gridId).find(".k-grid-edit").show();
        $("#" + gridId).find(".k-grid-add").show();
    }

}
于 2014-12-11T08:24:45.600 回答