jqGrid设置新添加的Row intoGrid的id

时间:2012-02-02 20:06:09

标签: jqgrid

 jQuery.extend(
jQuery.jgrid.edit, {
    ajaxEditOptions: { contentType: "application/json" }, //form editor
    reloadAfterSubmit: true
//        afterSubmit: function (response, postdata) {
//    return [true, "", $.parseJSON(response.responseText).d];
//}
});

$.extend($.jgrid.defaults, {
    datatype: 'json',
    ajaxGridOptions: { contentType: "application/json" },
    ajaxRowOptions: { contentType: "application/json", type: "POST" },
               //row              inline     editing
    serializeGridData: function(postData) { return JSON.stringify(postData); },
    jsonReader: {
        repeatitems: false,
        id: "0",
        cell: "",
        root: function(obj) { return obj.d.rows; },
        page: function(obj) { return obj.d.page; },
        total: function(obj) { return obj.d.total; },
        records: function(obj) { return obj.d.records; }
    }

});


 $("#grantlist").jqGrid({
            url: 'webservice.asmx/GetGrant',
            colNames: ['ID', 'Name', 'Title'],
            colModel: [
            { name: 'ID', width: 60, sortable: false },
            { name: 'name', width: 210, editable: true },
            { name: 'title', width: 210, editable: true }
            ],
            serializeRowData: function(data) {
                var params = new Object();
                params.ID = 0;
                params.name = data.name;
                params.title = data.title;
                return JSON.stringify({ 'passformvalue': params, 'oper': data.oper, 'id': data.id });
            },
            mtype: "POST",
            sortname: 'ID',
            rowNum: 4,
            height: 80,
            pager: '#pager',
            editurl: "webservice.asmx/ModifyGrant"
        });
        $("#grantlist").jqGrid('navGrid', '#pager', { add: false, edit: false, del: false, refresh: false, search: false });
        $("#grantlist").jqGrid('inlineNav', '#pager');

//this is my server code
[WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public JQGrid<GrantComittee> GetGrantComittee(int? page, int? rows, string sidx, string sord, bool _search)
    {
        JQGrid<GrantComittee> jqgrid = new JQGrid<GrantComittee>();

        List<GrantComittee> data = GetGComittee();
        jqgrid.records = data.Count; //total row count
        jqgrid.total = (int)Math.Ceiling((double)data.Count / (double)rows); //number of pages
        jqgrid.page = page.Value;

        //paging
        data = data.Skip(page.Value * rows.Value - rows.Value).Take(rows.Value).ToList();
        foreach(GrantComittee i in data)
            jqgrid.rows.Add(i);


        return jqgrid;


    }

[WebMethod(EnableSession = true), ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int ModifyGrantComittee(GrantComittee passformvalue, string oper, string id)
    {
        if (String.Compare(id, "_empty", StringComparison.Ordinal) == 0 ||
            String.Compare(oper, "add", StringComparison.Ordinal) == 0)
        {

            GrantComittee data = new GrantComittee();
            List<GrantComittee> set = new List<GrantComittee>();
            set = (List<GrantComittee>)Session["grantcomittee"];
            data = passformvalue;
            data.ID = set.Max(p => p.ID) + 1;
            set.Add(data);
            Session["grantcomittee"] = set;
            return data.ID; 
        }
        else if (String.Compare(oper, "edit", StringComparison.Ordinal) == 0)
        {
            // TODO: modify the data identified by the id
            return 0;
        }
        else if (String.Compare(oper, "del", StringComparison.Ordinal) == 0)
        {
            // TODO: delete the data identified by the id 
            return 0;
        }
        return 0;
    } 

我正在使用JqGrid检索并向数据库添加新记录。到目前为止,我已经能够检索并向数据库添加新项目,我正在使用“json”。我确实在响应{“d”:“5”}中获取了DB中新创建的行的id。但是新ID不会显示在网格中。 如何将该值更新为新添加的行?

2 个答案:

答案 0 :(得分:4)

在大多数情况下,由于默认设置reloadAfterSubmit: true,您无需执行任何操作。这意味着在用户添加新行或修改现有行之后,将从服务器重新加载完整网格。

如果你想使用reloadAfterSubmit: false设置并且服务器返回响应中新创建的行的id,你需要实现afterSubmit回调函数,它将解码服务器响应并返回它供jqGrid使用。相应的代码可以是以下内容:

afterSubmit: function (response, postdata) {
    return [true, "", $.parseJSON(response.responseText).d];
}

您可以通过覆盖默认参数$.jgrid.edit来定义回调(请参阅herehere)。

答案 1 :(得分:0)

我正在使用'inlinNav'并且在添加新行之后我没有重新加载网格。我找到的解决方案是将参数添加到'inlineNav'声明中。所以我最终得到了我提供的代码作为参考:

$("#grantlist").jqGrid('inlineNav', '#pager', { addParams: { position: "last", addRowParams: {
            "keys": true, "aftersavefunc": function() { var grid = $("#grantlist"); reloadgrid(grid); }
        }
        }, editParams: { "aftersavefunc": function() { var grid = $("#grantlist"); reloadgrid(grid); } }
        });    
function reloadgrid(grid) {
grid.trigger("reloadGrid");

}

我使用了多个网格,这就是我将网格参数传递给重载函数的原因。