从错误处理程序内部获取对 Kendo Grid 的引用 [英] Get a reference to Kendo Grid from inside the error handler

查看:17
本文介绍了从错误处理程序内部获取对 Kendo Grid 的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有有关如何获得自定义错误处理的问题以及答案,但所有这些答案都使用外部"参考/selector 到网格使其工作,例如:

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:
";  

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

是否可以从错误处理函数内部获取对网格的引用,而无需手动或外部"提供选择器(因为全局变量是meh)?这样错误处理脚本就可以完全独立.

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.

推荐答案

Version 'current' as of 2015-12-05

显然,现在可以通过 e.sender.table.context.id 检索源网格.谢谢,Akbari

Version 'current' as of 2015-12-05

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

以下解决方案不起作用.数据源中似乎缺少 table 成员.

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

我的解决方法很粗糙,只是使用选择器来获取所有 k-grid 元素,这些元素为 .data("kendoGrid") 返回非空值并比较数据arg.sender 的来源.当数据源匹配时 - 我们有一个引发错误的网格:

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.

在函数内部,this并没有绑定到grid,而是绑定到grid内部使用的一个DataSource,所以不能真正使用直接改变错误处理行为.需要一点记录不足的魔法.

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.

这意味着(从 Kendo UI MVC 版本 2013.3.1119.545 开始)可以使用以下内容:

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

e.sender.options.table.context

返回包装网格(DOM元素),而

to return the wrapping grid (DOM element), while

e.sender.options.table.context.id

返回网格的 ID.

这意味着,使用 jQuery,可以通过以下方式检索网格:

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.

从技术上讲,this 绑定在范围内和 sender 似乎是一回事 - 网格的 DataSource,所以它们应该可以互换上面的例子.

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.

这篇关于从错误处理程序内部获取对 Kendo Grid 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆