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

Error Handling in DetailsTemplate Grid

2 Answers 401 Views
Grid
This is a migrated thread and some comments may be shown as answers.
AP
Top achievements
Rank 1
Iron
Iron
Veteran
AP asked on 25 Feb 2016, 09:08 AM

I have a sub-grid in a details template, that allows records to be inserted and deleted. I can put an error handler on the datasource, which will show an error message if the action fails, however as the grid name is dynamically generated, I can't see a way to call preventdefault(), to stop the failed inserts / deletes being displayed in the grid.

How can I get the grid name to call the preventDefault method?

Grid:-

<script id="subdetailsTemplate" type="text/kendo-tmpl">
 
    @(Html.Kendo().Grid<EVASLogicLibrary.UserDirectorateMap>()
              .Name("MapGrid_#=ID#")
              .Columns(columns =>
              {
                  columns.Bound(o => o.ID).Title("ID");
                  columns.ForeignKey(p => p.DirectorateID, (System.Collections.IEnumerable)ViewData["directorates"], "ID", "DirectorateName").Title("Directorate").Width(200);
                  //columns.Bound(o => o.UserID).Title("User");
                  columns.Command(command => { command.Destroy(); command.Edit(); });
 
              })
              .Events(e=>e.DataBound("gridBind"))
              .ToolBar(tb => tb.Create())
              .Editable(e=>e.Mode(GridEditMode.InLine))
          .DataSource(dataSource => dataSource
              .Ajax()
              .PageSize(10)
              .Events(e=>e.Error("Suberror"))
              .Model(m=>{
                  m.Id(p => p.ID);
                  m.Field(p => p.ID).Editable(false);
 
              })
               .Read(read => read.Action("RD_Maps", "Users", new { UserId = "#= ID #" }))
               .Create(u=>u.Action("InsertMap", "Users", new { UId = "#= ID #" }))
               .Update(u => u.Action("InsertMap", "Users"))
               .Destroy(u => u.Action("DeleteMap", "Users"))
              )
 
              .Pageable(p => p.Refresh(true))
 
              .ToClientTemplate()
 
    )
 
    </script>

Error handler:-

function Suberror(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";
                      });
 
                  }
 
              });
 
              alert(message);
          }
 
      }

And the main grid error handler (that I want to replicate the functionality of:-

function error(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
 
               });
 
               alert(message);
 
        
 
           }
       }

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 29 Feb 2016, 10:10 AM

Hello AP,

In order to capture the name of the child Grid you could use the second overload of the Error event and assign a function similar to the following:

        @(Html.Kendo().Grid<ViewModel>()
            .Name("grid_#=ID#") // template expression, to be evaluated in the master context      
            .DataSource(dataSource => dataSource
                .Ajax()
                /*..*/
                .Events(events => events.Error(@<text>
                    function(e) {                      
                       Suberror.call(this, e, "grid_#=ID#");
                    }
                </text>))               
            )
            /*..*/
            .ToClientTemplate()
         )
<script>
    function Suberror(e, gridName) {
        var grid = $("#" + gridName).data("kendoGrid");
 
        if (e.errors) {    
            /*..*/
        }
    }
</script>

Regards,
Rosen
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
AP
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 29 Feb 2016, 10:13 AM
Thanks, I'll give this a go.
Tags
Grid
Asked by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
Rosen
Telerik team
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Share this question
or