2011-08-31 45 views
1

如何將簡單的JSON對象綁定到jqGrid?將jqGrid綁定到JSON對象

這是我有:

var tableSrc = { "page":"1", "total":1, "records":"3", "rows": [ 
     { "title": "Title1", "subtitle": "subTitle", "authors": ["a1", "a2", "a3"] }, 
     { "title": "Title2", "subtitle": "subtitle", "authors": ["X", "Y"] }, 
     { "title": "Title3", "subtitle": "subTitle", "authors": ["1", "2", "3", "4"]}] 
    }; 

    $(".jqGridTarget").jqGrid({ 
     datastr: tableSrc, 
     datatype: "jsonstring", 
     colNames: ['title', 'subtitle'], 
     colModel: [ 
      { name: 'title', index: 'title', width: 55 }, 
      { name: 'subtitle', index: 'subtitle', width: 90}] 
    }); 

然後:

<table class="jqGridTarget"> 
</table> 

這產生了錯誤:

Uncaught Syntax error, unrecognized expression: # inside of jQuery 1.6.2

我使用JSON而不是與數據jsonstring也嘗試替換數據。這消除了錯誤,但網格仍然是空的。在兩種情況下都會出現undefined,或者在網格體中閃爍。

編輯

我也試過數據類型: 「本地」 與tableSrc數據。沒有錯誤或未定義,但網格中仍然沒有數據。

編輯完

而且,這裏是我所引用的腳本/ css文件:

<script type='text/javascript' src='jquery.min.js'></script> 
<script type='text/javascript' src='jquery.tmpl.js'></script> 
<script type='text/javascript' src='jquery.jqGrid.min.js'></script> 
<script type='text/javascript' src='knockout-1.2.1.js'></script> 
<link rel="Stylesheet" type="text/css" href="ui.jqgrid.css" /> 
+1

[沒有這樣的東西作爲JSON對象。](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) –

+0

我知道,我知道。這就是大多數人所稱的,所以我想我會把它稱爲... –

回答

2

需要三個變化,使你的代碼的工作(見here固定演示):

  1. 附加參數jsonReader:{repeatitems:假}`
  2. 添加和id<table>元件
  3. 包括i18n/grid.locale-en.jsjquery.jqGrid.min.js

此外,我會建議你總是使用gridview: true並且在大多數情況下,定義爲heightheight: 'auto'

+0

@Adam Rackis:不客氣! – Oleg

+0

它似乎沒有第3步就可以正常工作。如果我只和美國觀衆一起工作,我還需要嗎? –

+0

@Adam Rackis:要使用jqGrid,你必須包含'grid.locale-en.js'。沒有這個,你會有另一個錯誤。 .js文件的順序也非常重要。請參閱[文檔](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:how_to_install)。 – Oleg

1

我認爲你正在尋找datatype: 'local'data: tableSrc

datatype : Defines what type of information to expect to represent data in the grid. Valid options are xml - we expect xml data; xmlstring - we expect xml data as string; json - we expect JSON data; jsonstring - we expect JSON data as string; local - we expect data defined at client side (array data); javascript - we expect javascript as data; function - custom defined function for retrieving data.

data : A array that store the local data passed to the grid. You can directly point to this variable in case you want to load a array data. It can replace addRowData method which is slow on relative big data

http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options

+0

對不起,我也試過。沒有錯誤或「未定義」,但網格中仍然沒有數據。我會更新我的問題。 –