Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  100] [ 7]  / answers: 1 / hits: 15385  / 11 Years ago, thu, january 2, 2014, 12:00:00

There already are questions how to get custom error handling, with answers, but all those answers use 'external' reference/selector to the grid to make it work, for example:



function onError(e) {
if (e.errors) {
var message = Error:n;

var grid = $('#gridID').data('kendoGrid'); // <<- here
(...)
}


Is it possible to get the reference to the grid from inside the error handling function without providing the selector by hand or 'externally' (because global variables are meh)? That way the error handling script could be totally self-contained.


More From » asp.net-mvc

 Answers
10

Version 'current' as of 2015-12-05



Apparently, the source grid can now be retrieved via e.sender.table.context.id. Thanks, Akbari!



KendoUI 2014.1.318



Solution below won't work. It seems that table member is missing from data source.



My workaround was quite crude, just using selectors to grab all k-grid elements which return not-null for .data(kendoGrid) and compare the data sources with arg.sender. When the data sources match - we have a grid which raised the error:



$(.k-grid).each(function() {
var grid = $(this).data(kendoGrid);
if (grid !== null && grid.dataSource == args.sender) {
// We have a winner!
}
});





Original answer



Turns out - after browsing the Internet for quite a bit - that it is possible.
So here it goes, for anyone searching for the answer sometime in the future, maybe even future-me.



Inside the function, this is not bound to a grid, but to a DataSource that the grid uses internally, so it can't really be used directly to alter the error-handling behavior. A little bit of poorly documented magic is needed.



It means that (as of Kendo UI MVC version 2013.3.1119.545) the following can be used:



e.sender.options.table.context


to return the wrapping grid (DOM element), while



e.sender.options.table.context.id


returns grid's ID.



It means that, with jQuery, the grid can be retrieved by:



var grid = $(e.sender.options.table.context).data(kendoGrid);


And the rest of the error-handling script remains exactly the same.



Technically, both this bound in the scope and sender seem to be the same thing - grid's DataSource, so they should be interchangeable in the example above.


[#73421] Wednesday, January 1, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
miles

Total Points: 256
Total Questions: 111
Total Answers: 104

Location: Benin
Member since Fri, Mar 24, 2023
1 Year ago
;