javascript 从错误处理程序内部获取对 Kendo Grid 的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20886651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a reference to Kendo Grid from inside the error handler
提问by Patryk ?wiek
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.
是否可以从错误处理函数内部获取对网格的引用,而无需手动或“外部”提供选择器(因为全局变量是meh)?这样错误处理脚本就可以完全独立。
回答by Patryk ?wiek
Version 'current' as of 2015-12-05
截至 2015-12-05 的“当前”版本
Apparently, the source grid can now be retrieved via e.sender.table.context.id
. Thanks, Akbari!
显然,源网格现在可以通过e.sender.table.context.id
. 谢谢,阿克巴里!
KendoUI 2014.1.318
剑道UI 2014.1.318
Solution below won't work. It seems that table
member is missing from data source.
下面的解决方案不起作用。table
数据源中似乎缺少该成员。
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
返回非空的元素.data("kendoGrid")
并将数据源与arg.sender
. 当数据源匹配时 - 我们有一个引发错误的网格:
$(".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 directlyto alter the error-handling behavior. A little bit of poorly documented magic is needed.
在函数内部,this
没有绑定到网格,而是绑定到网格DataSource
内部使用的a ,因此它不能真正直接用于更改错误处理行为。需要一点记录不足的魔法。
It means that (as of Kendo UI MVC version 2013.3.1119.545) the following can be used:
这意味着(从 Kendo UI MVC 版本 2013.3.1119.545 开始)可以使用以下内容:
e.sender.options.table.context
to return the wrapping grid (DOM element), while
返回包装网格(DOM 元素),而
e.sender.options.table.context.id
returns grid's ID.
返回网格的 ID。
It means that, with jQuery, the grid can be retrieved by:
这意味着,使用 jQuery,可以通过以下方式检索网格:
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.
从技术上讲,两者都this
在范围内并且sender
似乎是同一件事 - grid's DataSource
,因此在上面的示例中它们应该可以互换。
回答by Sergey T
I would suggest passing the target grid id as an argument to your function. Example: .Events(events => events.Error("function(args){telerikGridAraxErrorhandler(args,'myGridId');}"))
我建议将目标网格 ID 作为参数传递给您的函数。示例:.Events(events => events.Error("function(args){telerikGridAraxErrorhandler(args,'myGridId');}"))
I think it will result in less support in case they change anything in future versions of Telerik Grid
我认为如果他们在 Telerik Grid 的未来版本中更改任何内容,将会减少支持
回答by Atanas Korchev
Indeed the error event is exposed by the data source and one can't easily get which grid triggered it. Also we should keep in mind that one data source can be shared by many widgets.
事实上,错误事件是由数据源公开的,人们无法轻易获得哪个网格触发了它。另外我们应该记住,一个数据源可以被多个小部件共享。
Another possible solution is to use a closure bound to the widget name:
另一种可能的解决方案是使用绑定到小部件名称的闭包:
function errorHandler(gridName) {
return function(e) {
// handle the event.
var grid = $(gridName).data("kendoGrid");
};
}
$("#grid").kendoGrid({
dataSource: {
error: errorHandler("#grid")
}
});
回答by Austin Areaux
I know this is old, but it seems out of date. I needed to access the grid's name within the error because the error was coming from an 'inner grid' (from a template in and within the main grid's detail template). This is from the Telerik forums so I don't want to take credit for it, but it took me a bit to find it so I'm sharing here. For razor helpers,
我知道这是旧的,但它似乎已经过时了。我需要在错误中访问网格的名称,因为错误来自“内部网格”(来自主网格的详细信息模板中和内部的模板)。这是来自 Telerik 论坛,所以我不想因此而受到赞扬,但我花了一点时间才找到它,所以我在这里分享。对于剃须刀助手,
@Html.Kendo().Grid<Model>()
.DataSource(dataSource => dataSource
.Events(events => events.Error("function(e){error_handler(e, 'GridName')"}))
For Jquery:
对于 Jquery:
$("#Promotions").kendoGrid({
dataSource: {
error: errorHandler("#Promotions")
}
});
and the Javascript function
和 Javascript 函数
Function error_handler(e, GridName)
{
}