jqGrid + JSON + WebService 完整示例


真沒找到這樣的例子,於是自已寫了個,分享出來。

第一步,首先在WebService上,添加[System.Web.Script.Services.ScriptService]屬性標簽,讓WebServer支持JSON.

namespace jqGrid_JSON_WebService_Sample.Services
{
/// <summary>
/// Summary description for WebServiceGrid
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebServiceGrid : System.Web.Services.WebService
{
}
}

接着,添加ajax調用的后端代碼,獲取數據,返回JSON對象:

        [WebMethod]
public object Grid(bool? _search, string nd, int page, int rows, string sidx, string sord, string searchField, string searchString, string searchOper)
{
int count;
var data = dc.Query<Issue>(string.IsNullOrWhiteSpace(searchField) ? null : new string[] { searchField }, new string[] { searchOper }, new object[] { searchString }, null, new string[] { string.IsNullOrWhiteSpace(sidx) ? "IssueID" : sidx }, new string[] { sord }, (page - 1) * rows, rows, out count);
return (new
{
total = Math.Ceiling((float)count / (float)rows),
page = page,
records = count,
rows = data.Select(item => new { id = item.IssueID, cell = new object[] { item.IssueID, item.Title, item.Description, item.Progress, item.CreateTime, item.Locked } })
});
}

第二步,添加前台頁面,首先添加各種的js,css引用,然后添加jqGrid所需的<div>和js代碼:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebFormGrid.aspx.cs" Inherits="jqGrid_JSON_WebService_Sample.WebFormGrid" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<link href="/Content/smoothness/jquery-ui-1.8.17.custom.css" rel="stylesheet" type="text/css" />
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script>
<link href="/Content/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="/Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function ()
{
$("#list #grid").jqGrid(
{
url: '/Services/WebServiceGrid.asmx/Grid',
mtype: 'POST',
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
serializeGridData: function (postData)
{
if (postData.searchField === undefined) postData.searchField = null;
if (postData.searchString === undefined) postData.searchString = null;
if (postData.searchOper === undefined) postData.searchOper = null;
return JSON.stringify(postData);
},
jsonReader:
{
root: "d.rows",
page: "d.page",
total: "d.total",
records: "d.records"
},
datatype: "json",
colNames:
[
'IssueID',
'Title',
'Description',
'Progress',
'CreateTime',
'Locked'
],
colModel:
[
{ name: 'IssueID', width: 100, index: 'IssueID' },
{ name: 'Title', width: 100, index: 'Title' },
{ name: 'Description', width: 300, index: 'Description' },
{ name: 'Progress', width: 150, index: 'Progress' },
{ name: 'CreateTime', width: 100, index: 'CreateTime', formatter:'date', sorttype:'datetime', datefmt:'M d h:i' },
{ name: 'Locked', width: 100, index: 'Locked' }
],
rowNum: 10,
rowList: [10, 15, 20, 25, 40],
pager: '#pager',
viewrecords: true,
sortorder: "desc",
width: 900,
height: 240,
});

$("#list #grid").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false });
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="list">
<table id="grid">
</table>
<div id="pager">
</div>
</asp:Content>

注意jqGrid函數據前面的部分代碼:

                url: '/Services/WebServiceGrid.asmx/Grid',
mtype: 'POST',
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },

通過url指定WebService方法,mtype指定使用POST方法,contentType指定為json,這樣WebService才會返回json對象。

可是,返回的數據是放在一個屬性名為d的對象里,所以還要添加 jsonReader,來作數據映射:

 jsonReader:
{
root: "d.rows",
page: "d.page",
total: "d.total",
records: "d.records"
},

最后,為了保證查詢時能夠POST正確的參數,還要對POST的參數值進行檢查:

                serializeGridData: function (postData)
{
if (postData.searchField === undefined) postData.searchField = null;
if (postData.searchString === undefined) postData.searchString = null;
if (postData.searchOper === undefined) postData.searchOper = null;
return JSON.stringify(postData);
},

到此,一個完整的jqGrid示例就完成了,成果展示:


完整示例代碼:jqGrid_JSON_WebService_Sample.zip

汗!數據庫文件還有版本問題,低版本的數據庫文件在這下載, 低版本數據庫文件的完整示例代碼:jqGrid_JSON_WebService_Sample(v2).zip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM