2013-03-25 29 views
0

我正在使用JQGrid显示数据库结果。现在我需要用户更新行。我曾尝试使用Inline Navigator。我已经使用下面的代码来制作网格。如何更新JQGrid中的行?

$("#MyGrid").jqGrid({ 
     url: service, 
     datatype: "json", 
     mtype: 'GET', 
     colNames: ['Col1', 'Col2'], 
     colModel: [ 
    { name: 'Col1', index: 'Col1', sortable: true, resizable: true, editable: true, sorttype: "text" }, 
    { name: 'Col2', index: 'Col2', align: 'left', sortable: true, resizable: true, width: 50, editable: true }, 

     pager: '#pagerLab', 
     rowNum: 1000, 
     rowList: [10, 30, 100, 1000], 
     sortname: 'modified', 
     viewrecords: true, 
     gridview: true, 
     loadonce: true,   
     editurl: '/Service.svc/UpdateGrid', 
    }); 
     jQuery("#MyGrid").jqGrid('navGrid', "#pagerLab", { edit: true, add: false, del: false, search:false }); 
    jQuery("#MyGrid").jqGrid('inlineNav', "#pagerLab"); 

}

现在我不知道如何编写服务器端代码保存在数据库用户的更改。我正在使用支持AJAX的Web服务。

这里是显示网格我的web服务代码:

[OperationContract] 
     [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] 
     public JQGridViewTable MyGrid(string ID) 
     { 
      Reader reader = new Reader(); 

      return Reader.ReadDetails(ID); 
     } 

而且在阅读器类我的C#代码(在数据模型):

public JQGridViewTable ReadDetails(string ID) 
    {   
      JQGridViewTable table = new JQGridViewTable(); 
    // read data from database and store in table 
     return table; 
    } 

我需要帮助:

1-我需要使用Post而不是Get吗?请注意,我正在一个函数中显示和编辑网格。我是否需要在Javascript中添加任何其他内容?例如编辑或恢复功能?在文档中,他们在内联编辑中编辑和恢复功能,但不在内联导航中编辑和恢复功能。 3-以何种格式将数据发送到Web服务进行编辑?为了显示,它是JQGridView格式。我不知道如何在Web服务中实现UpdateGrid方法,因为我不知道Inline Navigator函数究竟做了什么,它发送给Web服务的数据以及它期望从服务器得到的数据。

我搜索了整个网页,但是每个人都以不同的方式使用它。 我将不胜感激任何帮助。

+0

如果我的理解正确,您可以在服务器端使用WCF服务(SVC)。你使用哪个版本的Visual Studio(2012或2010)? – Oleg 2013-03-25 17:48:44

+0

我使用2010年,是的,我的WCF在服务器端。 – user2208346 2013-03-25 19:54:09

回答

0

根据您的示例代码中的jqGrid会做

editurl: '/Service.svc/UpdateGrid' 

您需要在您的WCF服务创建一个这种方法后处理。 jqGrid将调用HTTP Post并调用此方法,并发送列值。

您将杂草来添加一个操作,simalar以下到您的WCF服务:

[OperationContract] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json)] 
    public void UpdateGrid(string col1, string col2) 
    { 
      //code to actually do the update to the database 
    } 

的WCF操作将resposible对数据库中的表实际上定位的右排(我假设你可以根据存储在col1或col2中的值执行此操作)并执行更新操作。

[default.html中]

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="Scripts/jquery-1.8.3.js" type="text/javascript"></script> 
    <script src="Scripts/jqGrid/grid.locale-en.js" type="text/javascript"></script> 
    <script src="Scripts/jqGrid/jquery.jqGrid.js" type="text/javascript"></script> 
    <script src="Scripts/jquery-ui-1.10.2.js"></script> 
    <link href="Content/ui.jqgrid.css" rel="stylesheet" /> 
    <link href="Content/themes/base/jquery-ui.css" rel="stylesheet" /> 

    <script type="text/javascript"> 
     var lastSel; 

     $(document).ready(function() { 

      var service = 'http://localhost:5127/Service.svc/GetData'; 

      $("#MyGrid").jqGrid({ 
       url: service, 
       datatype: "json", 
       height: 255, 
       width: 600, 
       colNames: ['Col1', 'Col2'], 
       colModel: [ 
           { name: 'Col1', index: 'Col1', sortable: true, resizable: true, editable: false, sorttype: "text" }, 
           { name: 'Col2', index: 'Col2', align: 'left', sortable: true, resizable: true, width: 50, editable: true }, 
       ], 
       jsonReader: { 
        root: "rows", 
        page: "page", 
        total: "total", 
        records: "records", 
        cell: "", 
        repeatitems: false 
       }, 
       rowNum: 1000, 
       rowList: [10, 30, 100, 1000], 
       pager: '#pagerLab', 
       sortname: 'Col1', 
       viewrecords: true, 
       gridview: true, 
       loadonce: true,  
       onSelectRow: function (id) { 
        if (id && id !== lastSel) { 
         $(this).restoreRow(lastSel); 
         lastSel = id; 
        } 
        jQuery(this).editRow(id, true); 
       }, 
       editurl: 'http://localhost:5127/Service.svc/UpdateData', 
       ajaxRowOptions: { contentType: 'application/json; charset=utf-8' }, 
       serializeRowData: function (data) { 
        return JSON.stringify(data); 
       } 

      }); 

      jQuery("#MyGrid").jqGrid('navGrid', "#pagerLab", 
       { edit: false, add: false, del: false, search: false } 
       ); 
      jQuery("#MyGrid").jqGrid('inlineNav', "#pagerLab"); 

     }); 
    </script> 
</head> 


<body> 
    <table id="MyGrid"></table> 
    <div id="pagerLab"></div> 
</body> 
</html> 

[IService.cs]

[ServiceContract] 
public interface IService 
{ 
    [OperationContract] 
    [WebInvoke(Method="GET", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json)] 
    jqGridModel<GridListItem> GetData(); 

    [OperationContract] 
    [WebInvoke(Method="POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, RequestFormat=WebMessageFormat.Json)] 
    void UpdateData(string id, string oper, string Col1, string Col2); 

} 

[DataContract] 
public class GridListItem 
{ 
    [DataMember] 
    public string Col1 { get; set; } 

    [DataMember] 
    public string Col2 { get; set; } 
} 

[DataContract] 
public class jqGridModel<T> 
{ 
    public jqGridModel() 
    { 
     this.total = 0; 
     this.rows = null; 
     this.records = 0; 
     this.page = 0; 
    } 

    [DataMember] 
    public int total { get; set; } 
    [DataMember] 
    // this is the property where you store the actual data model 
    public IList<T> rows { get; set; } 
    [DataMember] 
    public int page { get; set; } 
    [DataMember] 
    public int records { get; set; } 
    } 
} 

下面的代码片段使用您的样本为起点,从我的实现

[Service.svc.cs]

public class Service : IService 
{ 

    jqGridModel<GridListItem> IService.GetData() 
    { 
     jqGridModel<GridListItem> model = new jqGridModel<GridListItem>(); 
     List<GridListItem> list = new List<GridListItem>(); 
     GridListItem item = new GridListItem() { Col1 = "1", Col2 = "Dog" }; 
     list.Add(item); 

     item = new GridListItem() { Col1 = "2", Col2 = "Cat" }; 
     list.Add(item); 

     model.records = list.Count; 
     model.rows = list; 
     return model; 
    } 

    void IService.UpdateData(string id, string oper, string Col1, string Col2) 
    { 
     //do work here to save the updated data 
    } 
} 
+0

感谢您的回复。 我将此方法添加到我的Web服务中。在编辑一个字段后,我点击JQgrid中的“保存”按钮,它给了我400错误的请求错误。我需要添加其他任何东西到我的JavaScript? – user2208346 2013-03-25 21:57:07

+0

@ user2208346我添加了一个完整的工作示例;我希望这可以帮助你。 – codechurn 2013-03-27 03:17:55

+0

谢谢你帮了很多。 – user2208346 2013-03-29 15:52:58