2011-06-17 81 views
1

我試圖從jsp響應中加載數據,這是JSON使用JQGridJQGrid數據未加載JSON服務器響應

我的index.html

<pre> 
<script type="text/javascript" src="jqgrid/src/i18n/grid.locale-en.js"></script> 
<script type="text/javascript" src="jqgrid/src/grid.loader.js"></script> 
<script type="text/javascript" src="jqgrid/js/jquery.jqGrid.min.js"></script> 
</pre> 

function getEmployeeDetails(){ 
    jQuery("#list").jqGrid({ 
    datatype: function(postdata) { 
      jQuery.ajax({ 
       url: 'execute.jsp?command=command', 
       mtype:'POST', 
       datatype:"json", 
        colModel : [ 
        {name:'amount', width:30, sortable:true, align:'center'}, 
        {name:'age', width:40, sortable:false, align:'center'}, 
        {name:'name', width:180, sortable:true, align:'left'}, 
        {name:'total', width:380, sortable:true, align:'left'} 
        ], 

        jsonReader : { 
         root: "key", 
         page: "currentpage", 
         total: "total", 
         records: "totalrecords", 
         repeatitems: true, 
         cell: "cell", 
         id: "id", 
         userdata: "userdata" 
         }, 
         width: 700, 
         height: 200, 

        pager: '#pager', 
        sortorder: 'desc', 
        viewrecords: true, 
        caption: 'My first grid' 
      }); 
      } 
    }); 
}  

而且我execute.jsp代碼

**<pre> 
response.setContentType("application/json"); 
response.getWriter().write(wr.toJSONString()); 
</pre>** 

JSON string從Java對象生成。

 
{ 
    "totalrecords":"20", 
    "tota1":"10", 
    "currentpage":"runCommand.jsp", 
    "key":[ 
    {"id":"1","cell":["12000","30","Myname","10000"]} 
    ] 
} 

數據未加載到表中。我嘗試的方式不同,我不能成功。總是表空來,並說加載.....請幫助我。這裏有什麼不對...

+0

您不應該同時使用'grid.loader.js'和'jquery.jqGrid.min.js'。如果您需要調試jqGrid的JavaScript代碼,最好使用'jquery.jqGrid.src.js'而不是'jquery.jqGrid.min.js'(當然不要使用'grid.loader.js')。 – Oleg

回答

1

您以錯誤的方式使用datatype。你不需要手動撥打jQuery.ajax jqGrid會爲你做這個。 JavaScript代碼可能如下:

$(document).ready(function() { 
    $("#list").jqGrid({ 
     url: 'execute.jsp?command=command', 
     mtype:'POST', 
     datatype:"json", 
     colModel : [ 
      {name:'amount', width:30, align:'center'}, 
      {name:'age', width:40, sortable:false, align:'center'}, 
      {name:'name', width:180}, 
      {name:'total', width:380} 
     ], 
     jsonReader : { 
      root: "key", 
      page: "currentpage", 
      total: "total", 
      records: "totalrecords" 
     }, 
     width: 700, 
     height: 200, // the value 'auto' could be probably better 
     pager: '#pager', 
     sortorder: 'desc', 
     viewrecords: true, 
     caption: 'My first grid' 
    }); 
}); 

查看演示here。例如,您應該將您的JSON數據中的值"currentpage":"runCommand.jsp"更正爲某個數值,例如"currentpage":"1"

+0

@奧列格。謝謝你解決這個問題。你救了我的日子,我得到了它的工作。謝謝,謝謝,謝謝。 – mathi

+0

@mathi:不客氣!我很高興聽到(讀)我可以幫助你很多。 – Oleg