невозможно связать данные JSON в jqGrid - PullRequest
0 голосов
/ 25 февраля 2011
$(document).ready(function () {
    jQuery("#tblGridDemo").jqGrid({
        url: 'GridDataService.svc/GetMyGridData',
        datatype: 'json',
        height: 250,
        type: "GET",
        width: 920,
        contentType: 'application/json; charset=utf-8',
        colNames: ['Id', 'Name','Organization'],
        colModel: [
            { name: 'id', index: 'id', width: 60 },
            { name: 'Name', index: 'Name', width: 90 },
            { name: 'Organization', index: 'Organization', width: 100 }
        ],
        multiselect: true,
        caption: "Bind service Data",
        rowNum: 10,
        pager: jQuery("#divPagerDemo"),
        rowList: [10, 15, 20, 30, 50, 100],
        sortname: 'Id',
        sortorder: "desc",
        loadtext: "Loading....",
        shrinkToFit: true,
        emptyrecords: "No records to view",
        rownumbers: true,
        viewrecords: true,
        sucess: function (a) {
            alert(a);
        }
    });
    jQuery("#tblGridDemo").jqGrid('navGrid', '#divPagerDemo',
                                  { edit: false, add: false, del: false });
});

--- служба

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public List<GridDataFormat> GetMyGridData()
{
    List<GridData> lstGridData = new List<GridData>();           
    List<GridDataFormat> lstGridDataFormat = new List<GridDataFormat>();
    try
    {
        for (int iCountRows = 1; iCountRows <= 10; iCountRows++)
        {
            GridData objGridData = new GridData();
            objGridData.Id = iCountRows;
            objGridData.Name ="Name"+iCountRows;
            objGridData.Organization = "Organization" + iCountRows;
            lstGridData.Add(objGridData);
        }

        int total = 10, page = 1;

        GridDataFormat obj = new GridDataFormat();
        obj.total = total;
        obj.page = page;
        obj.records = 10;
        obj.rows = lstGridData;              
        lstGridDataFormat.Add(obj);
    }
    catch (Exception ex)
    {
    }
    return lstGridDataFormat;
}

----- lstGridDataFormat.cs

[DataContract]
[Serializable]
public class GridDataFormat
{
    [DataMember]
    public int page;

    [DataMember]
    public int total;

    [DataMember]
    public int records;

    [DataMember]
    public List<GridData> rows;
}

1 Ответ

0 голосов
/ 25 февраля 2011

Вы должны использовать serializeGridData (см. здесь ), например, как

serializeRowData: function(data) { return JSON.stringify(data); }

В другом ответе вы найдете пример кода, который можно загрузить при необходимости.

...