如何在内联编辑后重新加载网格?

时间:2013-08-16 21:44:34

标签: php jqgrid

我正在使用jqGrid PHP并添加一个操作列。 编辑工作正常,但保存行后,网格不会重新加载。 当我手动刷新网格时,会显示更新的信息。

我尝试将编辑选项添加到addCol(参见下文),但我在某处读到editOptions仅适用于表单编辑。看一下文档,看起来我可以使用onSuccess或afterSave参数,但我不确定如何将reloadAfterSubmit方法添加到PHP中的任何一个参数中。

$grid->addCol(array( 
    "name"=>"Click to Edit", 
    "formatter"=>"actions", 
    "editable"=>false, 
    "sortable"=>false, 
    "resizable"=>false, 
    "fixed"=>true, 
    "width"=>45,
    "loadonce"=>false, 
    "formatoptions"=>array("keys"=>true, "editbutton"=>true, "delbutton"=>false,"editOptions"=>array("reloadAfterSubmit"=>true)) 
    ), "first"); 

对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我正在使用jQuery和jqGrid的单元格编辑功能,它在离开每个单元格时将新单元格值提交给服务器。在我的特定应用程序中,当一个单元格改变时,它会影响同一行中的其他(计算)列,并可能影响其他行,因此我需要更新可能遍布网格的值。

在我的情况下,当提交单元格并更新数据库时,计算本身在服务器上执行,因此我需要获取更新的数据以更新网格。我想尽可能快地编辑单元格,但也要保持行更新。我还想只更新由服务器确定的已修改的行。我也想要一些通用且可重复使用的东西。

我不知道这是如何转换为PHP的,但这就是我在JavaScript中所做的。

var xhrUpdate = null;

// Handle jqGrid's afterSubmitCell.
$("#someGridId").jqGrid(
    "setGridParam", 
    "afterSubmitCell",
    function (response, rowid, name, val, iRow, iCol) {

        // If an ajax request is already in progress from the last cell edit, 
        // abort it since the data is now moot with the new cell update.
        // Sorry server, nobody's listening!
        if (xhrUpdate != null) {
            xhrUpdate.abort();
            xhrUpdate = null;
        }

        // Call the generic grid update function (see below) asynchronously.
        xhrUpdate = updateGrid(
                        "someGridId", 
                        "someUpdateUrl", 
                        { someParam: someParamValue, ... }, 
                        "someIDFieldThatIdentifiesTheGridRow", 
                        true);

        // Return success.
        return [true, ""];
    });

// This function updates a jgGrid which already contains data.
// It will only update the rows and columns that are returned
// from the server.
function updateGrid(grid_id, url, data, idField, async) {
    var grid = $("#" + grid_id)
    return $.ajax({
                async: async,
                type: "GET",
                datatype: "json",
                url: url,
                data: data,
                success: function (result) {
                    // Loop over the returned rows.
                    $.each(result, function (index, row) {
                        // Loop over the returned fields in each row.
                        $.each(row, function (key, value) {
                            // Set the corresponding cell value.
                            // This won't touch any cell's that aren't 
                            // represented in the JSON data.
                            grid.jqGrid("setCell", row[idField], key, value);
                        });
                    });
                }
            });
}

注意事项:

1)这肯定会给服务器带来负担。

2)异步,这不会处理来自单元格提交的坏单元格数据或其他异常服务器响应。它非常乐观,但如果需要,可以处理这些问题。

3)服务器需要聪明才能返回适当的数据,或者你可以返回所有内容。

对不起,这不是PHP,但我希望这有帮助。

相关问题