2012-07-12 106 views
1

我已經設置jqGrid來顯示我的數據,即使數據來自服務器,數據不會顯示在網格中。 這是服務器響應。 Data is coming from the server like thisjqGrid沒有填寫數據?

這裏是JavaScript。

<table id="Grid1" class="scroll"></table> 
<div id="pager" class="scroll" style="text-align:center;"> 

<script> 
$(document).ready(function() { 
    jQuery("#Grid1").jqGrid({ 
      url: 'api/matchingservicewebapi/getuser', 
      datatype: 'json', 
      mtype: 'GET', 
      colNames: ['Id', 'Account', 'Ref', 'BDate'], 
      colModel: [ 
       { name: 'Id', index: 'Id', width: 200 }, 
       { name: 'Account', index: 'Account', width: 300 }, 
       { name: 'Ref', index: 'Ref', width: 300 }, 
       { name: 'BDate', index: 'BDate', width: 300 } 

      ], 
      rowNum: 10, 
      rowList: [10, 20, 30], 
      pager: '#pager', 
      sortname: 'Id', 
      viewrecoreds: true, 
      sortorder: "desc", 
      imgpath: 'Themes/images' 
     }).navGrid(pager, { edit: true, add: true, del: true, refresh: true, search: true }); 
}); 

下面是數據

{"Data":{"rows":[{"id":200.0,"cell":["100","AMX.Data.Account","200","1/10/2010 12:00:00 AM"]},{"id":14726632.0,"cell":["600146","AMX.Data.Account","14726632","1/8/2010 12:00:00 AM"]},{"id":6633000.0,"cell":["668152","AMX.Data.Account","6633000","1/8/2010 12:00:00 AM"]},{"id":2053178.0,"cell":["600146","AMX.Data.Account","2053178","1/8/2010 12:00:00 AM"]},{"id":753550.0,"cell":["668152","AMX.Data.Account","753550","1/8/2010 12:00:00 AM"]},{"id":121774.0,"cell":["600146","AMX.Data.Account","121774","1/8/2010 12:00:00 AM"]},{"id":37900.0,"cell":["600146","AMX.Data.Account","37900","1/8/2010 12:00:00 AM"]},{"id":6569.0,"cell":["668152","AMX.Data.Account","6569","1/8/2010 12:00:00 AM"]},{"id":-1124870617.0,"cell":["668152","AMX.Data.Account","-1124870617","1/8/2010 12:00:00 AM"]},{"id":-813658270.0,"cell":["668152","AMX.Data.Account","-813658270","1/8/2010 12:00:00 AM"]}]},"JsonRequestBehavior":0} 

但數據沒有得到加載的JSON格式。我究竟做錯了什麼?

+0

也許我失去了一些東西,但你的反應JSON不包括頁,總,記錄,檢查此鏈接的http:/ /www.trirand.com/jqgridwiki/doku.php?id=wiki:first_grid – 2012-07-12 07:02:00

+0

是的,它似乎必須是您的數據格式的一些問題。你能在問題中以純文本的形式粘貼json嗎? – txominpelu 2012-07-12 07:05:50

+0

@txominpelu我添加了JSON格式的數據。 – thilok 2012-07-12 07:10:41

回答

1

正如Mostafa & txominpelu的評論所述。嘗試格式化你的JSON結果如下

{"total":1123, 
"page":1, 
"records":1123, 
"rows":[{"id":1174, 
      "cell":["1174","4","",...] 
     }] 
} 

,並在服務器端

public ActionResult GridData(int page, int rows, string searchField = "", 
     string searchString = "", string searchOper = "", string sidx = "MY_ID", 
     string sord = "desc") 
{ 
    //... 
    var jsonData = new { 
     total = totalPages, 
     page, 
     records = totalRecords, 
     rows = (from item in items.ToList() 
       select new { 
        id = item.MY_ID, 
        cell = new[] { 
         #region items 
         ToSafeString(item.MY_ID), 
         ToSafeString(item.ShiftID), 
         //... 
         #endregion 
        } 
       }).ToArray() 
    }; 
} 
+0

該ID應該是一個int嗎?或者如果ID是一個字符串它可以嗎? – thilok 2012-07-12 07:57:42

+1

我最好的猜測就是嘗試一下,到目前爲止,我所有的id都是int,但是我確定string只要它的唯一性就可以工作。只要確保單元格對象上的所有值都是字符串 – 2012-07-12 08:10:46

+0

好吧,只是嘗試了ID作爲字符串,它工作喲:) – 2012-07-12 08:12:49