如何将数据从JQGrid发送到我的查询以删除行?

时间:2012-09-07 19:32:16

标签: jquery jqgrid coldfusion

我无法从我的JQGrid中删除一行,因为我无法弄清楚如何将我需要的数据发送到包含MySQL的文件。我正在使用ColdFusion。

在我的JQGrid文件中,我的editurl参数设置如下:

editurl: url+"process_delete.cfc?method=process_delete&region="+region,

在我的process_delete.cfc文件中,我保存了我的MySQL查询,我有:

DELETE FROM awesome_table
WHERE region = '#region#' AND Field1 = '??????' AND Field2 = '???????'

我知道MySQL正在到达 - 没有问题。此外,该区域从URL填充得很好。没有问题。问题是我无法弄清楚如何从我想要删除的行中访问数据以填充Field1和Field2,从而有效地完成查询。有人可以帮忙吗?感谢。

对于删除,我有以下代码:

jQuery.jgrid.del = {
            caption: "Delete Item",
            msg: "Delete record?",
            bSubmit: "Delete",
            bCancel: "Cancel",
            beforeSubmit: function(postdata, formid) { 
                var rowid = $("#mygrid").getGridParam('selrow');
                $("#mygrid").jqGrid('saveRow',rowid,false,'clientArray');
                var rowvalues = $("#mygrid").getRowData(rowid);
                return [true, ""]
            }

当我在警告消息框中显示rowid时,我会回到“null”,所以也许这就是我的问题所在。

2 个答案:

答案 0 :(得分:6)

您可以将delData与属性field1field2定义为函数,也可以使用onclickSubmitbeforeSubmit来动态修改在DELETE操作中使用URL或使用serializeDelData回调。最好的方法可能取决于您使用的其他选项(例如,取决于用于删除操作的mtype)。在the answer中,我包含了对其他答案的引用,这些答案详细说明了所有方法。

例如,您可以使用

onclickSubmit: function (options, rowid) {
    // we suppose that use don't use multiselect: true option
    // in the case rowid parameter if the string with the id of the
    // deleted row

    // we can get the data about the deleted row with respect of
    // getCell, getLocalRow or getRowData methods
    var rowData = $(this).jqGrid("getRowData", rowid);

    // now we can modify the URL used in the Delete operation
    options.url += "?" + $.param({
        field1: rowData.field1,
        field2: rowData.field2
    });

    return {}; // you can return additional data which will be sent to the server
}

答案 1 :(得分:0)

如果没有看到从CFC回来填充网格的代码,那么真的无济于事。但是,一种方法是访问行的id并将其放在一些HTML元素中,例如锚标记,例如:

如果要创建循环以准备数据

//Some loop
<cfset dataRows[i]['id'] = #yourqueryId# />
<cfset dataRows[i]['cell'] = "<a href="##" class="delete" id="#yourqueryId#">Delete</a>" />

然后将JSON对象传递给CFM文件

 <cfset JSONReturn = {total=#totalPages#,page=#page#,records=#recordcount#,rows=dataRows} />

然后在显示网格的页面中添加一个处理锚标记点击的事件

$('a.delete').on('click', function(){
    var id = $(this).attr('id');
    //do something with the id
})

希望有所帮助!

相关问题