【问题标题】:Passing dynamic params to delete url method in JQGrid传递动态参数以删除 JQGrid 中的 url 方法
【发布时间】:2010-06-29 12:38:44
【问题描述】:

我正在使用带有 json 服务的 JQGrid 来列出所有数据库用户成员信息,因此网格的 url 指向我的 json 服务和方法。到目前为止一切正常,我可以添加和编辑用户并修改数据并且保存得很好。然而,删除数据库用户是另一回事,因为 Membership.DeleteUser 将用户名作为其参数。 JQGrid 似乎只在添加或编辑模式下传递可编辑参数。但是,当您尝试删除它时,它似乎不允许返回任何参数,我觉得这很奇怪。我才刚刚开始使用 JQGrids,所以我可能只是很厚:-)。请问谁能告诉我如何做到这一点?我将用户名作为 JQGrid 本身的一列。到目前为止,我已经尝试了各种方法:

url: 'misc/myservice.svc/AddEditDeleteGridRow?UserName=' + $('#MyGridTbl').getCell('selrow', 'UserName')

在 navGrid 的删除部分。我也尝试在选择行事件中设置 URL,但我发现它需要重新加载才能将其插入网格,当发生这种情况时,所选行会丢失,因此会破坏对象。我只需要能够访问/获取 json 服务中的用户名,以便将其传递给 Membership.DeleteUser。我一直在互联网上搜索,似乎找不到任何东西。

这是我正在使用的 JQGrid。 json 服务基本上只有 GetData,它返回 JQGridJSONData(json 对象数据集)和 AddEditDeleteGridRow 方法,两者都是公共的。所有列数据都被发送到 json 服务以进行添加和编辑,但没有发送任何内容用于删除操作。

只是为了澄清我需要 json 服务中服务器端的用户名。

   $('#MyGrid').jqGrid({
    url: 'Misc/MyjsonService.svc/GetData',
    editurl: 'Misc/MyjsonService.svc/AddEditDeleteGridRow',
    datatype: 'json',
    colNames: ['UserId', 'UserName', 'Email Address', 'Password Last Changed', 'Locked'],
    colModel: [
                { name: 'UserId', index: 'UserId', hidden:true, editable: true, editrules:{edithidden: true}},
                { name: 'UserName', index: 'UserName', editable: true, width: 200, sortable: false, editrules: { required: true} },
                { name: 'Email Address', index: 'Email', editable: true, width: 500, sortable: false, editrules: { email: true, required: true} },
                { name: 'Password Last Changed', index: 'LastPasswordChangedDate', editable: false, width: 200, sortable: false, align: 'center' },
                { name: 'Locked', index: 'IsLockedOut', sortable: false, editable: true, edittype: "checkbox", formatter: 'checkbox', align: 'center' }
            ],
    rowNum: 20,
    hidegrid: false,
    rowList: [20, 40, 60],
    pager: $('#MyGridPager'),
    sortname: 'UserName',
    viewrecords: true,
    multiselect: false,
    sortorder: 'asc',
    height: '400',
    caption: 'Database Users',
    shrinkToFit: false,
    onPaging: function(pgButton) {
        this.DBUserId = null;
    },
    onSelectRow: function(Id) {
        if (Id && Id !== this.DBUserId) {
            this.DBUserSelect(Id);
        }
    },
    loadComplete: function() {
        if (this.DBUserId)
            this.DBUserSelect(this.DBUserId, true);
    },
    gridComplete: function() {
        var grid = $('#MyGrid');
        var body = $('#AvailableDBUsersArea');
        if ((grid) && (body)) {
            grid.setGridWidth(body.width() - 10);

            //keep the grid at 100% width of it's parent container
            body.bind('resize', function() {
                var grid = $('#MyGrid');
                var body = $('#AvailableDBUsersArea');
                if ((grid) && (body)) {
                    grid.setGridWidth(body.width() - 2);
                }
            });
        }
    }
}).navGrid('#MyGridPager',
        { add: true, edit: true, del: true, refresh: false, search: false }, //general options
        {
        //Options for the Edit Dialog
        editCaption: 'Edit User',
        height: 250,
        width: 520,
        modal: true,
        closeAfterEdit: true,
        beforeShowForm: function(frm) { $('#UserName').attr('readonly', 'readonly'); },
        beforeShowForm: function(frm) { $('#UserId').removeAttr('readonly'); },
        beforeShowForm: function(frm) { $('#UserId').attr('readonly', 'readonly'); }
    },
        {
            //Options for the Add Dialog
            addCaption: 'Add User',
            height: 250,
            width: 520,
            modal: true,
            closeAfterAdd: true,
            beforeShowForm: function(frm) { $('#UserName').removeAttr('readonly'); },
            beforeShowForm: function(frm) { $('#UserId').removeAttr('readonly'); },
            beforeShowForm: function(frm) { $('#UserId').attr('readonly', 'readonly'); }
        },
        {
            //Delete options
            width: 350,
            caption: 'Delete User',
            msg: 'Are you sure you want to delete this User?\nThis action is irreversable.'
        },
        {} //Search options
    );

【问题讨论】:

  • 不允许一次性定义像beforeShowForm这样的函数。您应该将“编辑”和“添加”对话框选项更改为只有一个 beforeShowForm 定义。

标签: javascript jqgrid


【解决方案1】:

有一些方法可以将附加参数添加到删除 URL。有 jqGrid 的定义会很有帮助,尤其是 colModel。

如果你有一个隐藏列,例如使用可以使用

hidden: true, editable: true, editrules: { edithidden: false }, hidedlg: true

参数。然后隐藏的列将不会在编辑对话框中看到,但会发送该列的值。

如果您需要在发送删除请求之前修改 URL,您可以选择另一种方式。您可以定义navGrid 的参数(参见http://www.trirand.com/jqgridwiki/doku.php?id=wiki:navigator#how_to_use 上的prmDel 参数),如下所示

{ onclickSubmit: function(rp_ge, postdata) {
      rp_ge.url = 'misc/myservice.svc/AddEditDeleteGridRow?UserName=' +
                  $('#MyGridTbl').getCell (postdata, 'UserName');
  }
}

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多