This is a migrated thread and some comments may be shown as answers.

Prevent detele last record

2 Answers 273 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrey
Top achievements
Rank 1
Veteran
Andrey asked on 17 Jun 2018, 04:03 PM

Hello,

I am using MVC wrappers without Edit button (Add and Delete only). What is the better way to prevent to delete very last record in the grid? I could use command().hidden(bool) but have not figured out yet how to wire JavaScript function there.

Thanks.

@(Html.Kendo().Grid<WebApplication42.Models.Phone>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.PhoneID);
            columns.Bound(p => p.PhoneNumberInt);
            columns.Bound(p => p.PhoneNumberString);
            columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
        })
        .ToolBar(toolbar => toolbar.Create())
        .Editable(editable => editable.Mode(GridEditMode.InLine).ConfirmDelete("Delete").DisplayDeleteConfirmation("Are you sure to delete?"))
        .Pageable()
        .Sortable()
        .Scrollable()
        .HtmlAttributes(new { style = "auto;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Model(model => model.Id(p => p.PhoneID))
            .Create(update => update.Action("EditingInline_Create", "Grid"))
            .Read(read => read.Action("EditingInline_Read", "Grid"))
            .Update(update => update.Action("EditingInline_Update", "Grid"))
            .Destroy(update => update.Action("EditingInline_Destroy", "Grid"))
            .Events(e => e.Error("onPhoneError"))

        )
)

2 Answers, 1 is accepted

Sort by
0
Accepted
Georgi
Telerik team
answered on 19 Jun 2018, 11:56 AM
Hi Andrey,

Based on the provided information I assume that the requirement is to hide the destroy command in case the grid contains only a single record. Please correct me if I am wrong.

A possible solution is to pass a handler to the Visible property of the command.

e.g.
// command configuration
 
 x.Destroy().Visible("handler");
 
// handler
 
    function handler(e) {     
        return $('\#grid').data('kendoGrid').dataSource.data().length > 1;
    }


Regards,
Georgi
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Andrey
Top achievements
Rank 1
Veteran
answered on 20 Jun 2018, 10:31 AM
Works great. Thank you!
Tags
Grid
Asked by
Andrey
Top achievements
Rank 1
Veteran
Answers by
Georgi
Telerik team
Andrey
Top achievements
Rank 1
Veteran
Share this question
or