1

我正在用 jax-rs 开发一个 web 应用程序,我用 jquery 做了一个休息客户端。我将结果作为 json 或 xml,然后将它们显示到 html 表。为了方便表格,我使用 JqGrid 库。我的问题是,例如 Jqgrid 想要像下面这样的 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"
});
4

1 回答 1

3

我认为从服务器返回的数据没有问题。您应该只使用jqGrid的 jsonReader 选项,它会通知 jqGrid 如何从服务器响应中读取数据。例如

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

更新演示使用您发布的确切 JSON 数据,并在网格中显示结果。我使用您发布和替换的 JavaScript 代码只是height: 255为了height: "auto"获得更紧凑的结果。

在此处输入图像描述

我在您的代码中看到的唯一问题是完整 URL: 的使用url:'http://localhost:43842/KodcuComRESTful/kodcuRS/yazilar'。由于同源策略限制,无法从同一站点和端口的另一个源获取每个 Ajax 的 JSON 数据。因此,在使用的情况下,datatype: "json"您应该始终使用相对URL 路径url:'/KodcuComRESTful/kodcuRS/yazilar',例如。

于 2012-06-25T12:27:38.097 回答