2010-10-12 55 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