jQuery jqgrid - 在 ajax 文件上传后刷新网格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4497130/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:21:31  来源:igfitidea点击:

jqgrid - refresh a grid after an ajax file upload

jqueryajaxjqgrid

提问by Lothar

How would I refresh a jqgrid based grid from outside of the grid itself? Within the code for a grid exists the option to call reloadGrid. However, I want to reload the grid after doing an ajax file upload, outside of the jqgrid code. How could I call reloadGridin that context?

我如何从网格本身的外部刷新基于 jqgrid 的网格?在网格代码中存在调用reloadGrid. 但是,我想在 jqgrid 代码之外执行 ajax 文件上传后重新加载网格。我怎么能reloadGrid在这种情况下打电话?

I realize I can completely reload the entire page with something like: location.reload();but that reloads the whole lot and puts me back to the first page of results for a grid and kind of defeats the purpose of using ajax to upload a file in the first place.

我意识到我可以使用以下内容完全重新加载整个页面:location.reload();但这会重新加载整个页面并使我回到网格结果的第一页,这有点违背了首先使用 ajax 上传文件的目的。

Some code:

一些代码:

reloadGrid is called from within jqgrid as follows:

从 jqgrid 中调用 reloadGrid 如下:

$("#thegrid").trigger("reloadGrid");

$("#thegrid").trigger("reloadGrid");

but it does nothing when called from within my ajaxupload:

但是从我的 ajaxupload 中调用时它什么也不做:

        onComplete: function(file, response) {
            if (response == 'success') {
                        //location.reload();
                        $("#thegrid").trigger("reloadGrid");
                    }else if (response == 'error') {
                        alert("Doh!");
                    }

If I uncomment location.reload(), the page reloads but with the trigger uncommented (as in the above example) nothing happens at all. So how do I reload this grid?

如果我取消注释location.reload(),页面会重新加载,但触发器未注释(如上例所示)根本不会发生任何事情。那么我如何重新加载这个网格?

回答by bruno

Make a function of the code to load the jqgrid. And unload the Jqgrid first, and call the previous function:

使代码的功能加载jqgrid。并先卸载Jqgrid,并调用前面的函数:

$("#thegrid").GridUnload();
Loadthegrid();

I hope this helps.

我希望这有帮助。

回答by Oleg

I see no problem in using of $("#thegrid").trigger("reloadGrid")outside of jqGrid. Probably the problem is in relation between onCompletefrom the ajaxuploadand $("#thegrid")at all.

我认为在$("#thegrid").trigger("reloadGrid")jqGrid 之外使用没有问题。可能问题出在onCompletefromajaxupload$("#thegrid")at之间的关系。

For example you can define an external button on the same page as jqGrid with <table>element having id="thegrid" and use

例如,您可以在与 jqGrid 相同的页面上定义一个外部按钮,其中<table>元素具有 id="thegrid" 并使用

$("#button").click(function() {
    if ($("#thegrid").length === 0) {
        alert("no element with id='thegrid' is found");
    }
    if ($.isFunction($("#thegrid").jqGrid) !== true) {
        alert("$('#thegrid') is not jqGrid");
    }
    var e = $("#thegrid").data("events");
    if (typeof(e) !== "undefined" && typeof(e.reloadGrid) !== "undefined") {
        $("#thegrid").trigger("reloadGrid");
    } else {
        alert("$('#thegrid') is not bound to 'reloadGrid' function");
    }
}

you will see the the code work without any alerts. You can include the same code inside of the onCompletehandler of your ajaxupload.

您将看到代码在没有任何警报的情况下工作。您可以onComplete在 ajaxupload的处理程序中包含相同的代码。

To verify that 'reloadGrid' function work you can use loadComplete:

要验证“reloadGrid”功能是否有效,您可以使用loadComplete

loadComplete: function() {
    alert("grid is loaded/reloaded");
}

UPDATED: Starting with jQuery 1.8 one should use $._data($("#thegrid")[0], "events");instead of $("#thegrid").data("events")to get the list of all events of the grid.

更新:从 jQuery 1.8 开始,应该使用$._data($("#thegrid")[0], "events");而不是$("#thegrid").data("events")获取网格的所有事件的列表。