Kendo Grid Hiding/Showing Delete button

百般思念 提交于 2019-12-19 07:48:10

问题


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

I am building Kendo Grid.I would like to hide destroy(delete) command on page load on Kendo grid.After that when I click to button on same 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:

$(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;

});

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 kendo grid created. When I use it inside click function of edit button it hides delete buttons.


回答1:


If you use the same id for each columns you have many elements with the same id which is not legal. Try using the CSS class attribute that identifies a delete button and on creation (page load) hide it and then on click show it.

Code for hiding them

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

Code for showing them back

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

JSFiddle example here: http://jsfiddle.net/OnaBai/pSgeD/




回答2:


For change kendo-grid setting, you must re-create your grid. You can comment out "for offlide data", if you gird binding to remote 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();
    }

}


来源:https://stackoverflow.com/questions/19911238/kendo-grid-hiding-showing-delete-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!