2012-06-25 74 views
1

我正在用jax-rs開發一個web應用程序,我用jquery做了一個休息客戶端。我得到的結果爲json或xml,然後將它們顯示到html表格。爲了方便表格,我使用JqGrid庫。我的問題是,例如Jqgrid想要像下面那樣json對象;jquery JSON節點刪除

[ 
     {yaziNo:"1",yazar:"abc",yazi:"test",tarih:"2007-10-01"}, 
     {yaziNo:"2",yazar:"cdfe",yazi:"test2",tarih:"2007-10-01"}, 
     {yaziNo:"3",yazar:"cdfe",yazi:"test3",tarih:"2007-10-01"}, 
     {yaziNo:"4",yazar:"abc",yazi:"test",tarih:"2007-10-01"}, 
     {yaziNo:"5",yazar:"cdfe",yazi:"test2",tarih:"2007-10-01"}, 
     {yaziNo:"6",yazar:"abc",yazi:"test3",tarih:"2007-10-01"}, 
     {yaziNo:"7",yazar:"cdfe",yazi:"test",tarih:"2007-10-01"}, 
     {yaziNo:"8",yazar:"abc",yazi:"test2",tarih:"2007-10-01"}, 
     {yaziNo:"9",yazar:"abc",yazi:"test3",tarih:"2007-10-01"}  ] 

但是,從我的其餘服務器返回的JSON如下所示;

{"yazi": 
[{"tarih":"26.01.2012","yazar":"sdasdadsadasda","yazi":"gdfgdfgd","yaziNo":"1756"}, 
{"tarih":"26.01.2012","yazar":"sdasdadsadasda","yazi":"gdfgdfgd","yaziNo":"1755"}, 
{"tarih":"26.01.2012","yazar":"sdasdadsadasda","yazi":"gdfgdfgd","yaziNo":"1754"}, 
{"tarih":"26.01.2012","yazar":"sdasdadsadasda","yazi":"gdfgdfgd","yaziNo":"1753"}, 
{"tarih":"26.01.2012","yazar":"sdasdadsadasda","yazi":"gdfgdfgd","yaziNo":"1752"}] 
} 

如何刪除「yazi」節點,但保持在裏面。

編輯:

jQuery("#list27").jqGrid({ 
    url:'http://localhost:43842/KodcuComRESTful/kodcuRS/yazilar', 
    datatype: "json", 
    height: 255, 
    width: 700, 
    jsonReader: {root: "yazi", repeatitems: false}, 
    colNames:['Yazi No','Yazar', 'Yazi', 'Tarih'], 
    colModel:[ 
     {name:'yaziNo',index:'yaziNo', width:80, sorttype:"int"}, 
     {name:'yazar',index:'yazar', width:180}, 
     {name:'yazi',index:'yazi', width:370}, 
     {name:'tarih',index:'tarih', width:100, align:"right",sortype:"date"} 
    ], 
    rowNum:10, 
    rowTotal: 2000, 
    rowList : [20,30,50], 
    loadonce:true, 
    mtype: "GET", 
    rownumbers: true, 
    rownumWidth: 40, 
    gridview: true, 
    pager: '#pager27', 
    sortname: 'yaziNo', 
    viewrecords: true, 
    sortorder: "asc", 
    caption: "Loading data from server at once" 
}); 

回答

3

我看不出有什麼問題,從服務器返回的數據。您應該只使用jqGrid的jsonReader選項,它通知jqGrid如何從服務器響應中讀取數據。例如

jsonReader: {root: "yazi", repeatitems: false} 

修訂The demo使用您發佈確切的JSON數據並顯示在網格中的結果。我使用了您發佈的JavaScript代碼,並將其替換爲height: 255height: "auto"以獲得更緊湊的結果。

enter image description here

,我在你的代碼中看到的唯一的問題是完整的URL的用法:url:'http://localhost:43842/KodcuComRESTful/kodcuRS/yazilar'。由於same origin policy限制,無法從另一個源獲取每個Ajax的JSON數據作爲相同的站點和端口。因此,在使用datatype: "json"的情況下,您應該始終使用相對的網址路徑,例如url:'/KodcuComRESTful/kodcuRS/yazilar'

+0

非常感謝,奧列格:) –

+0

再次感謝奧列格,我完全重新安排了我的代碼,我想刪除,添加,更新操作。我添加添加,刪除,編輯頁面左側的按鈕欄,但我不知道我是如何實現/ POST或/ DELETE與jqGrid的行動。我可以用純Ajax函數做到,但我認爲jqGrid最好? –

+0

我認爲JqGrid最好,但文檔不好。 –