2010-10-12 53 views
3

我已经能够从我的数据库使用jQuery/Ajax从web服务到jQGrid的数据。现在我想发送添加/编辑的数据回到web服务。我已经看到了一些使用PHP和editurl:命令的例子。这是否也适用于web服务(比如我最初是如何拉下数据的)?jqgrid添加行并发送数据到webservice插入

我已经看了几遍例子。我甚至发现another question与我所要求的相似,但我无法找到任何我需要的方法。有没有存在?

:更新时间:

jQuery(document).ready(function() { 
    jQuery("#list").jqGrid({ 
     datatype: processrequest, 
     mtype: 'POST', 
     jsonReader: { 
      root: "ListExercise", //arry containing actual data 
      page: "Page", //current page 
      total: "Total", //total pages for the query 
      records: "Records", //total number of records 
      repeatitems: false, 
      id: "ID" //index of the column with the PK in it 
     }, 
     colNames: ['Id', 'Exercise'], 
     colModel: [ 
     { name: 'exercise_id', index: 'exercise_id',editable:false }, 
     { name: 'exercise_value', index: 'exercise_value',editable:true } 
     ], 
     caption: 'MyFitnessApplication', 
     pager: '#pager', 
     rowNum: 10, 
     rowList: [10, 20, 30], 
     sortorder: "desc", 
     viewrecords: true, 
     height: '250px', 
     loadonce: true, 
     editurl: "../webService/exercise_ws.asmx/insertRecord"  
    }).navGrid('#pager', { edit: true, add: true, del: false }); 
}); 

正如你可以看到我添加了editurl标签。这似乎叫我的web服务。现在我错过了如何将实际参数传递给webservice。我错过了一些东西,并感谢帮助!

回答

5

首先,你可以更改用于添加/编辑一些默认选项:

jQuery.extend(jQuery.jgrid.edit, { 
    ajaxEditOptions: { contentType: "application/json" }, 
    recreateForm: true, 
    serializeEditData: function (postData) { 
     if (postData.exercise_value === undefined) { postData.exercise_value = null; } 
     return JSON.stringify(postData); 
    } 
}); 

(其中JSON.stringifyhttp://www.json.org/js.html定义的函数)。然后将被发送到服务器的数据将被JSON编码。几乎相同的设置,可用于删除

jQuery.extend(jQuery.jgrid.del, { 
    ajaxDelOptions: { contentType: "application/json" }, 
    serializeDelData: function (postData) { 
     if (postData.exercise_value === undefined) { postData.exercise_value = null; } 
     return JSON.stringify(postData); 
    } 
}); 

现在,您可以定义insertRecord像下面

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public int ModifyData (string exercise_value, string oper, string id) 
{ 
    if (String.Compare (id, "_empty", StringComparison.Ordinal) == 0 || 
     String.Compare (oper, "add", StringComparison.Ordinal) == 0) { 

     // TODO: add new item with the exercise_value and return new id 
     //  as the method result 
    } 
    else if (String.Compare (oper, "edit", StringComparison.Ordinal) == 0) { 
     // TODO: modify the data identified by the id 
    } 
    else if (String.Compare (oper, "del", StringComparison.Ordinal) == 0) { 
     // TODO: delete the data identified by the id 
    } 
} 

你不写其类型有exercise_id:整数或字符串。如果你使用字符串ids,上面的代码应该改变一点。

+1

您的编辑使这更容易阅读:)谢谢。我能够遵循你所说的话并使其正常工作。谢谢! – webdad3 2010-10-13 23:21:01

+0

@Jeff V:我很高兴听到它!没关系! – Oleg 2010-10-13 23:36:33