Incell editing with a foreign key dropdown list

0 Answers 19 Views
Grid
AP
Top achievements
Rank 1
Iron
Iron
Veteran
AP asked on 18 Jan 2022, 09:07 AM

I have a grid, with in-cell editing where only one field is editable, which is a foreign key field with a dropdown list. The grid is set to autosync, and batch mode is off.

This works fine, updating the records after an option is selected from the dropdown list. However, on the server-side I have implemented some consistency checks, to see if a user updates a record after it has been updated by another user, but before their grid was refreshed. This works, and I can get an error message displayed.

However, what I need to happen once an error occurs, is for the record to be reset back to its original state. At the moment, the selected item on the dropdown list is shown, along with a dirty flag.  I have tried calling a datasource read on the grid, but this isn't called.

How can I reset the grid after an error?

The code is :-

    @(Html.Kendo().Grid<RightToResideAudit.Models.RightToResideList>()
                     .Name("Grid")
                     //.Events(e => e.Edit("onEdit"))
                     .Columns(col =>
                     {
                         col.Bound(o => o.ID).Title("ID");
                         col.Bound(o => o.PatientID).Title("Patient ID");
                         col.Bound(o => o.PatientName).Title("Patient Name");
                         col.Bound(o => o.Site).Title("Site");

                     
                         col.ForeignKey(p => p.ReasonID, (System.Collections.IEnumerable)ViewData["reasons"], "ID", "Reason")
         .Title("Reason").Width(250);



                         //col.Command(command => { command.Edit(); command.Destroy(); });

                     })

                        .Editable(editable => editable
                         .Mode(GridEditMode.InCell))
                         .DataSource(ds => ds
                         .Ajax()
                         .Events(events => events.Error("errorHandler"))
                         .AutoSync(true)
                         .Batch(false)

                         .Model(m =>

                         { m.Id(p => p.ID);
                             m.Field(p => p.ID).Editable(false);
                             m.Field(p => p.PatientID).Editable(false);
                             m.Field(p => p.PatientName).Editable(false);
                             m.Field(p => p.Site).Editable(false);
                             m.Field(p => p.Ward).Editable(false);
                             m.Field(p => p.BedSpace).Editable(false);
                             m.Field(p => p.CurrentLOS).Editable(false);
                             m.Field(p => p.LOSGroup).Editable(false);
                             m.Field(p => p.Pathway).Editable(false);
                             m.Field(p => p.LastFlaggedAsR2R).Editable(false);

                         })
                 //.Events(events => events.Error("error"))
                 .PageSize(15)
                 .Read(rd => rd.Action("RD_RightToResideList", "Home").Data("filter")

                 )
                  .Create(create => create.Action("NA", "Home"))
                 .Update(update => update.Action("UpdateRecord", "Home"))
                 .Destroy(delete => delete.Action("NA", "Home"))
                 )
                  .Pageable(p => p.Refresh(true))
                 .Sortable()
                 .Filterable()
            )



</div>


<script type="text/javascript">

    function filter()
        {
        return {
            AuditNumber: 1
        };

    }


    function errorHandler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });

            var grid = $("#Grid").data("kendoGrid");

            grid.one("dataBinding", function (e) {

                e.preventDefault();   // cancel grid rebind if error occurs
                $("Grid").data("kendoGrid").dataSource.read();



            });
            alert(message);
           
        }
    }
</script>

 

AP
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 18 Jan 2022, 09:39 AM

It seems if I change    $("Grid").data("kendoGrid").dataSource.read();  to  

 var grid = $("#Grid").data("kendoGrid");

        grid.dataSource.read();

 

The grid refresh works..

Patrick | Technical Support Engineer, Senior
Telerik team
commented on 20 Jan 2022, 03:02 PM

Great, and thanks for the update!  I'm glad the Kendo UI Grid is resetting and refreshing for you.  

No answers yet. Maybe you can help?

Tags
Grid
Asked by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Share this question
or