Prevent detele last record

3 posts, 1 answers
  1. Andrey
    Andrey avatar
    15 posts
    Member since:
    Jul 2014

    Posted 17 Jun Link to this post

    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. Answer
    Georgi
    Admin
    Georgi avatar
    368 posts

    Posted 19 Jun Link to this post

    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.
  3. Andrey
    Andrey avatar
    15 posts
    Member since:
    Jul 2014

    Posted 20 Jun in reply to Georgi Link to this post

    Works great. Thank you!
Back to Top