2012-09-07 69 views
1

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

在我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已经到达 - 没有问题。而且,该地区在网址中填充得很好。那里没有问题。问题是,我无法弄清楚如何从我试图删除的行中访问数据,以便填充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,我得到“空”回来了,所以也许这就是我的问题源于。

回答

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 
} 
+0

我使用的是MTYPE 'GET'。 – Lou

+0

@Louise:我想你不用'mtype:“GET”'来执行删除操作。 :-)删除操作的'mtype'默认值是''POST''。在某些情况下(RESTful服务),您可以更喜欢'mtype:“DELETE”'。 – Oleg

+0

你是对的 - 我在上面添加了删除。 – Lou

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 
}) 

希望有所帮助!

相关问题