2012-07-06 56 views
0

我對jqGrid非常陌生。我正在嘗試使用asp.net web api加載簡單的jqgrid。 api發回emailDto的列表。該emailDto是普通類與3個公共屬性jqgrid with web api not get populated

問題是jqgrid沒有得到填充。很感謝任何形式的幫助。

function dataBindToGrid() { 
     var lastsel; 
     $("#emailgrid").jqGrid({ 
      url: '/api/email/', 
      datatype: "json", 
      mytype: 'GET', 
      ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
      colNames: ['Address ID', 'Type', 'Email'], 
      colModel: [{ name: 'Address_ID', width: 70, primaryKey: true, editable: false, sortable: false, hidden: false, align: 'left' }, 
        { name: 'Email_Type', width: 70, editable: true, align: 'left', sortable: false }, 
        { name: 'Email_Address', width: 200, editable: true, align: 'left', sortable: false } 

      ], 
      onSelectRow: function (id) { 
       if (id && id !== lastsel) { 
        var grid = $("#emailgrid"); 
        grid.restoreRow(lastsel); 
        grid.editRow(id, true); 
        lastsel = id; 
       } 
      }, 
      //This event fires after all the data is loaded into the grid 
      gridComplete: function() { 
       //Get ids for all current rows 
       var dataIds = $('#emailgrid').jqGrid('getDataIDs'); 
       for (var i = 0; i < dataIds.length; i++) { 
        //Put row in edit state 
        $("#emailgrid").jqGrid('editRow', dataIds[i], false); 
       } 
      }, 
      rowNum: 3, 
      viewrecords: true, 
      caption: "Email Addresses" 
     }); 
    } 
+1

你可以在url/api/email /那接收到請求 – OJay 2012-07-06 01:42:49

+0

yes的代碼。我沒有檢查它,它確實發佈。 – 2012-07-06 17:14:39

回答

0

當配置爲jsondatatype,jqGrid的在下面的JSON格式的數據預計:

{ 
    "total": "xxx", 
    "page": "yyy", 
    "records": "zzz", 
    "rows" : [ 
    {"id" :"1", "cell" :["cell11", "cell12", "cell13"]}, 
    {"id" :"2", "cell":["cell21", "cell22", "cell23"]}, 
     ... 
    ] 
} 

返回的數據必須符合這個否則將無法正常工作。這是默認的json結構(如果你願意,你可以改變它)。根據您使用的瀏覽器,您應該能夠看到ajax請求並響應網格正在構建時發送和返回的數據(Firefox使用螢火蟲,IE使用開發工具欄)

here

+0

感謝您的回覆! – 2012-07-06 17:13:10

+0

感謝您的回覆。實際上我不需要記錄的總數,頁數和記錄數。我只允許最多3個電子郵件地址。 – 2012-07-06 17:16:28

0

解決!

剛剛添加到jqGrid下面,它的工作原理。

jsonReader: { 
       repeatitems: false, 
       page: function() { return 1; }, 
       root: function (obj) { return obj; }, 
       records: function (obj) { return obj.length; } 
      },