【问题标题】:add extra parameter in edit url using form dialog使用表单对话框在编辑 url 中添加额外参数
【发布时间】:2012-03-29 07:30:05
【问题描述】:

我已经使用 jqgrid 创建了一个网格,并且我正在使用表单对话框来添加、编辑和删除记录。

但在Edit Record i want to pass the id of a record along with the url 中,即使表单数据将包含那个,作为i am using RESTful url so to update a record i have to pass 相应的ID。如下

www.example.com/eventInfo/10 , Method is PUT 

到目前为止我已经尝试过

jQuery("#eventGrid").jqGrid('navGrid','#pager', {}, //options 

        {
                height:280,
                reloadAfterSubmit:false , 

               // How can i add Event id over here from grid 

                url:'http://www.eample.com/evenInfo',
                mtype:'PUT'


        }, // edit options 
        {
                height:280,
                reloadAfterSubmit:false
        }, // add options 
        {
                reloadAfterSubmit:false
        }, // del options
        {

        } // search options 


    ); 

此外,我想以 JSON 格式将数据发送到服务器,而不是作为表单数据

【问题讨论】:

    标签: jquery jqgrid jqgrid-php


    【解决方案1】:

    我描述了here,详细描述了在使用 RESTful 服务时应该做什么。回调onclickSubmit 是动态修改 URL 并将id 附加到它的最佳位置。

    github上的jqGrid的当前代码将网格的DOM元素设置为this。这样你就可以使用下面的了

    onclickSubmit: function (options, postdata) {
        options.url += '/' + encodeURIComponent(postdata.[this.id + "_id"]);
    }
    

    在 jqGrid 的下一个(4.3.1 之后)版本中。在当前版本的jqGrid中,代码大概如下

    var $grid = $("#eventGrid");
    
    // set defaults for Delete form
    $.extend($.jgrid.del, {
        mtype: "DELETE",
        reloadAfterSubmit: false
        serializeDelData: function () {
            return ""; // don't send and body for the HTTP DELETE
        },
        onclickSubmit: function (options, postdata) {
            options.url += '/' + encodeURIComponent(postdata);
        }
    });
    
    // set defaults for Edit and Add forms
    $.extend($.jgrid.edit, {
        height: 280,
        reloadAfterSubmit: false,
        onclickSubmit: function (options, postdata) {
            // options.gbox will be like "#gbox_eventGrid"
            var gridId = options.gbox.substr(6), // cut id from the gbox selector
                id = postdata.[gridId + "_id"];
            if (id !== "_empty") {
                options.url += '/' + encodeURIComponent(id);
            }
        },
        afterSubmit: function (responseData) {
            // in case of usage reloadAfterSubmit: false it's important
            // that the server returns id of the new added row
            // in the simplest form the server response should just contain
            // the id. In more complex response one should modify the next line
            // to extract the id only from the response
            return [true, '', responseData];
        }
    });
    
    // create jqGrid
    $grid.jqGrid({
        // all other parameters
        editurl: "/eventInfo" // you should use relative paths
    });
    
    // create navigator layer
    $grid.jqGrid('navGrid', '#pager', {/*navGrid options*/}, { mtype: 'PUT'}); 
    

    在上面的代码中,我添加了afterSubmit,这很重要,因为您使用了reloadAfterSubmit: false 选项。

    备注:我主要输入上面的代码或使用剪切和粘贴从另一个旧答案中复制一些部分。所以你应该在你的应用程序中测试上面的代码。

    【讨论】:

    • options.url 在控制台日志中打印 null 所以它不会附加到 eventInfo ,虽然 options.editurl 有一个有效的 url ,为什么会这样
    • 你能告诉我,我怎样才能以 json 格式而不是表单数据发送我的数据?
    • @Hunt:你应该使用serializeRowData,它使用JSON.stringify。详情请见the answer
    • serializeRowData 或 serializeRowData ,我想对于表单对话框我需要 serializeRowData 吧?
    • @Hunt:看看the answerthe corresponding demo,它们只是模拟成功的消息,在3 秒后消失并显示永久错误,以模拟如何实现errorTextFormat。可以将afterSubmit里面的demo部分代码移过来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 2012-10-29
    • 1970-01-01
    相关资源
    最近更新 更多