重新加载不起作用

时间:2018-06-04 15:38:33

标签: jqgrid free-jqgrid

在我的jqGrid中,我使用multiselect参数,以便每行都有一个复选框。 网格显示一个列表,其中每一行都有一个" Open"状态。可以检查多个行以及何时从分包商"关闭故障"单击按钮,将为所有勾选的条目分配一个新状态"已关闭"。 由于此网格仅显示"打开"条目,数据库被重新获取,并且需要显示新的打开条目列表。 为此,需要使用新数据重新加载网格。 目前reloadGrid不起作用,显示的数据与之前相同。

$(function () {
    getDataForGrid(populateGrid);
});

var populateGrid = function (data) {
    var grid = $("#grid");
    grid.jqGrid({
        data: data,
        multiselect: true,
        colNames: ["Category", "Description", "Opened", "Urgency", "Location"],
        colModel: [
            { name: "category", index: "category", width: 200, align: "left" },
            { name: "description", index: "description", width: 200, align: "left" },
            {
                name: "dateOpened",
                index: "dateOpened",
                width: 100,
                align: "left",
                formatter: "date",
                formatoptions: { newformat: "d-M-Y" }
            },
            { name: "urgency", label: "urgency", width: 200, align: "left" },
            { name: "location", label: "location", width: 100, align: "left" }
        ],
        //guiStyle: "bootstrap",
        prmNames: { id: "faultId" },
        localReader: { id: "faultId" },
        cmTemplate: { autoResizable: true },
        rowNum: 20,
        pager: "#pager",
        shrinkToFit: true,
        rownumbers: true,
        sortname: "category",
        viewrecords: true
    }).jqGrid("gridResize")
        .navGrid("#pager", {
            edit: false,
            add: false,
            del: false,
            search: false,
            refresh: false
        }).navButtonAdd("#pager",
        {
            caption: "Close Faults from Subcontractor",
            buttonicon: "ui-icon-circle-check",
            position: "first",
            title: "Blue Button",
            onClickButton: function () {
                var pdfFile = $("#documentName").val();
                if (pdfFile === undefined || pdfFile === null || pdfFile.length === 0) {
                    errorValidationMessage("You must upload a Subcontractor's confirmation pdf first before closing faults");
                    return false;
                }

                var selRowIds = grid.jqGrid("getGridParam", "selarrrow");
                if (selRowIds.length === 0) {
                    errorValidationMessage("You did not select any faults to close");
                    return false;
                }

                var contractSubcontractorId = GetHiddenField("sir-contract-subcontractor-id");
                var documentName = pdfFile.replace(/\\/g, "/").replace(/.*\//, "");
                var url = GetHiddenField("sir-close-faults-from-subcontractor-url");
                var postData = JSON.stringify({ faultIds: selRowIds, contractSubcontractorId: contractSubcontractorId, documentName: documentName });
                var callback = reloadGrid;
                dataService.putData(url, postData, callback);
            }
        });
    $("#divLoading").hide();
}

var getDataForGrid = function (callback) {
    var url = GetHiddenField("sir-get-open-faults-list");
    dataService.getList(url, callback);
}

var reloadGrid = function () {
    showSuccessMessage("Selected Properties Closed");
    getDataForGrid(reloadGridWithUpdatedData);
}

var reloadGridWithUpdatedData = function (data) {
    var grid = $("#grid");
    grid.jqGrid("setGridParam", { data: data });
    grid.trigger("reloadGrid", data);
}

1 个答案:

答案 0 :(得分:1)

在我看来,问题的原因在于使用setGridParam。只有在更改不是数组的参数时,才应该使用该方法。您当前的代码合并旧的data新代码(请参阅setGridParam代码的the main line,该代码调用$.extend的深层版本)。如果是数组,您应该使用overwrite的第二个setGridParam)参数:

var reloadGridWithUpdatedData = function (data) {
    var grid = $("#grid");
    grid.jqGrid("setGridParam", { data: data }, true);
    grid.trigger("reloadGrid");
}

或根本就不要使用setGridParam。方法getGridParam为内部对象提供引用,内部对象包含所有参数。它允许以非常简单的方式替换(覆盖)任何参数:

var reloadGridWithUpdatedData = function (data) {
    var grid = $("#grid"),
        p = grid.jqGrid("getGridParam"); // get reference to all parameters
    p.data = data; // replace data parameter
    grid.trigger("reloadGrid");
}