JSON data from servlet to jqGrid not displaying

前端 未结 2 426
灰色年华
灰色年华 2021-01-27 07:53

I am new to jQuery and finding difficulty in displaying data from my servlet to jqGrid in my jsp. I have used google gson to convert data from ArrayList to a String variable jso

相关标签:
2条回答
  • 2021-01-27 08:12

    I had the same issue initially. I solved like converting json into local data, This is how i am populating json data into jqgrid. It may helps you.

    function getReport() {
    $.ajax({
                url : "totalSalesReport.do?method=searchSpendReport"
        type : "POST",
        async : false,
        success : function(data) {
            $("#gridtable").jqGrid('GridUnload');
    
                       var newdata = jQuery.parseJSON(data);
    
                    $('#gridtable').jqGrid({
                data : newdata,
                datatype : 'local',
                colNames : [ 'Name', 'Year', 'Period'],
                colModel : [ {
                    name : 'name',
                    index : 'name'
                }, {
                    name : 'year',
                    index : 'year'
                }, {
                    name : 'period',
                    index : 'period'
                }],
                rowNum : 10,
                rowList : [ 10, 20, 50 ],
                pager : '#pager',
                shrinkToFit : false,
                autowidth : true,
                viewrecords : true,
                height : 'auto'
            }).jqGrid('navGrid', '#pager', {
        add : false,
        edit : false,
        del : false,
        search : false,
        refresh : false
    },
    
    {}, /* edit options */
    {}, /* add options */
    {}, /* del options */
    {});
    }});}
    

    Let me know if you need further assistance in getting data from jsp page.

    Updated Ans :

    I am using jsp for formatting the List data to json array. The piece of code is given below. You need to add json object jar file for this purpose.

    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.util.*,java.util.ArrayList"%>
    <%@page import="org.json.simple.JSONObject"%>
    <%
    net.sf.json.JSONObject responcedata = new net.sf.json.JSONObject();
    net.sf.json.JSONArray cellarray = new net.sf.json.JSONArray();
    net.sf.json.JSONArray cell = null; //new net.sf.json.JSONArray();
    net.sf.json.JSONObject cellobj = null; //new net.sf.json.JSONObject();
    
    List<ReportDto> reportDtos = null;
    if (session.getAttribute("agencyReport") != null) {
        reportDtos = (List<ReportDto>) session
        .getAttribute("agencyReport");
    }
    
    ReportDto reportDto = null;
    
    int i = 0;
    if (reportDtos != null) {
        for (int index = i; index < reportDtos.size(); index++) {
            reportDto = reportDtos.get(index);
            cellobj = new net.sf.json.JSONObject();
            cell = new net.sf.json.JSONArray();
            cellobj.put("name", reportDto.getVendorName());
            cellobj.put("year", reportDto.getSpendYear());
            cellobj.put("period",reportDto.getReportPeriod());
            cellarray.put(cellobj);
            i++;
        }
        out.println(cellarray);
    }
    %>
    
    0 讨论(0)
  • 2021-01-27 08:33

    change your colModel name and index same as the pojo class variables name.

    Thanks , Amit Kumar

    0 讨论(0)
提交回复
热议问题