【问题标题】:jqGrid not displaying the Json data from javajqGrid不显示来自java的Json数据
【发布时间】:2013-07-30 03:49:24
【问题描述】:

这是我的 java 服务

@RequestMapping(value="/refreshInterviewServiceList1", method = RequestMethod.GET)
public @ResponseBody JSONObject refreshInterviewServiceList1() throws JSONException{

    List<ReportInterview> reportInterview = reportInterviewServiceImpl.findByReportId(reportId);

    JSONObject output = new JSONObject();
    JSONArray toplevel = new JSONArray();
    output.put("page", 1);
    output.put("records", reportInterview.size());
    output.put("total",1);

    for(int i = 0;i<reportInterview.size();i++){

        JSONObject data = new JSONObject();
        JSONArray data1 = new JSONArray();
        data.put("id", i+1);

        data1.put(reportInterview.get(i).getName());
        data1.put(reportInterview.get(i).getOccupation());

        data.put("cell", data1);

        toplevel.put(data);

    }

    output.put("rows", toplevel);

    System.out.println(output);

    return output;
}

这是我的 javascript

<script type="text/javascript">

jQuery(document).ready(function () {
    jQuery("#projectTable").jqGrid({
        url: 'refreshInterviewServiceList1',
        datatype: "json",
        jsonReader : { 
            root: "rows", 
            page: "page", 
            total: "total", 
            records: "records", 
            repeatitems: true, 
            cell: "cell", 
            id: "id"
         },
        colNames:['Id','Name','Occupation'],
        colModel:[
            {name:'id',index:'id', width:10},
            {name:'name',index:'name', width:10},
            {name:'occupation',index:'occupation', width:10}
        ],
        rowNum:10,
        rowList:[10,20,30],
        height:460,
        width:700,
        pager: "#pagingDiv",
        viewrecords: true,
        caption: "Projects"
    });
});

我的 json 对象的输出

{
"total": 1,
"page": 1,
"records": 2,
"rows": [
    {
        "id": 1,
        "cell": [
            "232",
            "12"
        ]
    },
    {
        "id": 2,
        "cell": [
            "45",
            "454"
        ]
    }
]
}

我现在的问题是 jqgrid 无法在 jqgrid 中显示 json 数据...仅显示空白列表...请帮助...谢谢!

【问题讨论】:

    标签: java javascript jquery json jqgrid


    【解决方案1】:

    这是我一段时间前为个人项目编写的实用程序。它生成 jqGrid 可以呈现的数据。

    import java.util.List;
    
    public class JQGridContainer {
        private Integer page;
        private Integer total;
        private Integer records;
        private List<JQGridRow> rows;
    
        //getters and setters are omitted for brevity
    }
    
        import java.util.List;
    
        public class JQGridRow {
    
        private Integer id;
        private List<String> cell;
    
        //getters and setters are omitted for brevity
    
    
        }
    
    
    
    import java.lang.reflect.InvocationTargetException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    
    import org.apache.commons.beanutils.PropertyUtils;
    import org.apache.commons.lang.ObjectUtils;
    import org.apache.log4j.Logger;
    
    public class JQGridFormatterUtil {
    
        private static final Logger logger = Logger
                .getLogger(JQGridFormatterUtil.class);
    
        public static String getJSON(int currentPageNo,
                int totalRecords, Set objectsToBeAdded,
                List orderedPropertyNames) {
    
            Integer pages = 0;
    
            if(totalRecords % 50 >0){
                pages = (totalRecords/50)+1;
            }
            else{
                pages = (totalRecords/50);
            }
    
            JQGridContainer container = new JQGridContainer();
            container.setPage(currentPageNo);
            container.setTotal(pages);
            container.setRecords(totalRecords);
            List rows = new ArrayList();
            if (!objectsToBeAdded.isEmpty()) {
                for (Object obj : objectsToBeAdded) {
                    JQGridRow row = new JQGridRow();
                    row.setId(new Integer(getPropertvalue(obj, "id")));
                    List cells = new ArrayList();
                    for (String propertyName : orderedPropertyNames) {
                        cells.add(getPropertvalue(obj, propertyName));
                    }
                    row.setCell(cells);
                    rows.add(row);
                }
            }
            container.setRows(rows);
            return JSONUtil.convertToJSON(container);
    
        }
    
        private static String getPropertvalue(Object bean, String propName) {
            String val = null;
            try {
                val = ObjectUtils.toString(PropertyUtils.getProperty(bean, propName));
            } catch (IllegalAccessException e) {
                logger.error(e);
            } catch (InvocationTargetException e) {
                logger.error(e);
            } catch (NoSuchMethodException e) {
                logger.error(e);
            }
            return val;
        }
    
    
    import org.apache.log4j.Logger;
    
    import com.google.gson.Gson;
    
    public class JSONUtil {
    
        private static final Logger logger = Logger.getLogger(JSONUtil.class);
    
    
        public static String convertToJSON(Object obj){
            String json = null;
            Gson gson = new Gson();     
            json=(gson.toJson(obj));
            logger.debug(json);
            return json;
        }
    
    }
    
    }
    

    【讨论】:

    • 感谢您的回答,但我需要回答我的代码有什么问题导致 jqgrid 无法正确显示数据...
    • 我猜它与定义了 3 个值并且在 json 输入中只有 2 个值的 col-model 有关。如果第三列没有值,我会发送 null。
    • 您可以发布您从服务中返回的实际 JSON 吗? (这不是 s.o.p 输出)
    • {"total":1,"page":1,"records":2,"rows":[{"id":1,"cell":["232","12 "]},{"id":2,"cell":["45","454"]}]}.......这是没有任何输出安排……
    • 我仍然觉得 col 模型是问题所在。首先,尝试删除单元格并在“行”中发送一个 json 数组。 JQGrid 足够聪明,可以根据 col 模型对其进行映射。
    【解决方案2】:

    这不是您问题的解决方案,但它可能会帮助您避免未来的错误:

    请从您的代码中删除comma,因为那里不需要它,当我犯同样的错误时,它会在 IE (8) 中出错

    {name:'occupation',index:'occupation', width:10}, //Remove the comma at last position
    

    从下面一行:

    id: "id", //Remove the comma at last position
    

    【讨论】:

    • 是的,它没有解决问题,但仍然,感谢您的建议:)
    猜你喜欢
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多