@MrChief 有上面关于丑陋黑客的想法......我把它放在一起。这是我用来执行此操作的主要代码。事实上,它确实会在 ajax 调用上线之前劫持它。关键是修改正在发送的 URL,因为网格将从 HttpContext.Request.Path 中获取该 URL。并将其插入锚元素的 onclick 中。
我把它放在我的主要 common.js 中,并且将简单地附加一个函数来捕获在发送数据之前发生的 ajaxSend 事件。
// Used to hijack the sending of all AJAX calls. Before it sends the call to the server, it checks to see if the
// active element (the element that prompted the call) is marked with a given class. If so, then it will perform
// the given operation.
$(document).ajaxSend(function (event, jqXHR, ajaxOptions) {
var activeElement = document.activeElement;
if ($(activeElement).attr('redosorturl') != null) {
// If this is a sort anchor link from a grid that needs to have the sort link redone, do it here.
// the code is in the eipGrip.js file.
if ($(activeElement).attr('redosorturl').toString() == 'redoSortURL') {
var newURL = RedoGridSortURL(activeElement, ajaxOptions.url.toString());
ajaxOptions.url = newURL.toString();
}
}
return false;
});
渲染页面时,我在列标题中使用名为“redosorturl”的类标记了包含不正确 URL 的标签,所以我知道当我劫持 ajax 调用时,必须对这个元素进行操作。然后我调用为我提供正确 URL 的自定义函数,然后使用该新 URL 重写 ajaxOptions.url。
我必须将 activeElement 传递给该重写函数,以便我可以遍历 DOM 以获取网格信息,其中我放置了控制器和操作方法等数据,以及用于 URL 的 ID 和其他信息. 同样,我传入当前的 url 字符串,因为网格将在我解析的 url 的末尾注入一个标记并放在新的 url 上。