2010-09-02 42 views
8

我的jqGrid工作時,我的JSON數據是在一個靜態文件,但如果我將數據複製到一個變種,然後嘗試加載到jqGrid的網址變種var它不顯示。如何在jqGrid中使用JSON字符串或JSON對象?

你可以通過一個字符串到jqGrid

例如, 這工作:

function GetJSON() { 
    var jsonFile = "EntityWithChildren.json"; 
    return jsonFile;//returning a file works fine. 
} 

$("#jsonmap").jqGrid({ 
    url: GetJSON(), 
    datatype: 'json', 

這並不:

function GetJSON() { 
    var json = '{"page":"1","total":"10", "records":"10", "Entities": [  {"Fields":["Entity1", "field1", "11"]},  {"Fields":["", "field2", "22"]},  {"Fields":["Entity2", "field3", "33"]},  {"Fields":["ChildEntity1", "cfield1", "111"]} ]}'; 
    return json; //doesnt work 

} 

$("#jsonmap").jqGrid({ 
    url: GetJSON(), 
    datatype: 'json', 
    //datatype: 'jsonstring',//this doesnt work either 

回答

16

得到它。 需要使用datastr而不是url

datatype: 'jsonstring', 
datastr: GetJSON(), 
+0

正是!您可以在文檔http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_string中找到它。如果你回答自己的問題並解決問題,你應該更好地將自己的答案標記爲「已接受」。它簡化了與其他人一起處理您的問題。 – Oleg 2010-09-02 16:43:27

相關問題