【问题标题】:How to print response on jqGrid in struts2如何在struts2中的jqGrid上打印响应
【发布时间】:2012-02-11 08:59:48
【问题描述】:

您好,我在我的 struts2 hibernate 应用程序中使用 jqgrid。我在我的视图页面中正确获得了所有响应,我可以通过我的 firebus 工具看到这一点。但我的问题是如何在网格中打印值。我将我的jsp页面响应在这个页面下面。

这是我的jsp页面中的脚本

       <script type="text/javascript">

jQuery(document).ready(function(){
  jQuery("#list2").jqGrid({ 
    url:'server.action?q=2', 
    datatype: "json", 
    colNames:['id','name'], 
    colModel:[ {name:'id',index:'id'}, 
               {name:'name',index:'name'}], 
    rowNum:10, 
    pager: '#pager2', 
    sortname: 'id', 
    viewrecords: true, 
    sortorder: "desc", 
    caption:"JSON Example" }); 
jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});
});

</script>

我的回复是这样的

{"allUsers":"success",
"allUsersViaScript":"success",
"cell":[
   {"id":1,"name":"Aaronsburg","stateCode":"39"},
   {"id":2,"name":"Abbeville","stateCode":"2"},
   {"id":3,"name":"Abbeville","stateCode":"11"},
   {"id":4,"name":"Abbeville","stateCode":"19"},
   {"id":5,"name":"Abbeville","stateCode":"26"},
   {"id":6,"name":"Abbeville","stateCode":"41"},
   {"id":7,"name":"Abbot","stateCode":"22"},
   {"id":8,"name":"Abbotsford","stateCode":"49"},
   {"id":9,"name":"Abbott","stateCode":"44"},
   {"id":10,"name":"Abbottstown","stateCode":"39"},
   {"id":11,"name":"Abbyville","stateCode":"17"},
   {"id":12,"name":"Abell","stateCode":"21"},
   {"id":13,"name":"Abercrombie","stateCode":"29"},
   {"id":14,"name":"Aberdeen","stateCode":"14"},
   {"id":15,"name":"Aberdeen","stateCode":"18"},
   {"id":16,"name":"Aberdeen","stateCode":"21"},
   {"id":17,"name":"Aberdeen","stateCode":"26"},
   {"id":18,"name":"Aberdeen","stateCode":"28"},
   {"id":19,"name":"Aberdeen","stateCode":"36"},
   {"id":20,"name":"Aberdeen","stateCode":"42"},
   {"id":21,"name":"Aberdeen","stateCode":"48"},
   {"id":22,"name":"Aberdeen Proving Ground","stateCode":"21"},
   {"id":23,"name":"Abernant","stateCode":"2"},
   {"id":24,"name":"Abernathy","stateCode":"44"},
   {"id":25,"name":"Abie","stateCode":"30"},
   {"id":26,"name":"Abilene","stateCode":"17"},
   {"id":27,"name":"Abilene","stateCode":"44"},
   {"id":28,"name":"Abingdon","stateCode":"15"},
   {"id":29,"name":"Abingdon","stateCode":"21"},
   {"id":30,"name":"Abingdon","stateCode":"46"},
   {"id":31,"name":"Abington","stateCode":"7"},
   {"id":32,"name":"Abington","stateCode":"20"},
   {"id":33,"name":"Abington","stateCode":"39"},
   {"id":34,"name":"Abiquiu","stateCode":"33"},
   {"id":35,"name":"Abita Springs","stateCode":"19"},
   {"id":36,"name":"Abrams","stateCode":"49"},
   {"id":37,"name":"Absaraka","stateCode":"29"},
   {"id":38,"name":"Absarokee","stateCode":"27"},
   {"id":39,"name":"Absecon","stateCode":"32"},
   {"id":40,"name":"Acampo","stateCode":"5"},
   {"id":41,"name":"Accident","stateCode":"21"},
   {"id":42,"name":"Accokeek","stateCode":"21"},
   {"id":43,"name":"Accomac","stateCode":"46"},
   {"id":44,"name":"Accord","stateCode":"20"},
   {"id":45,"name":"Accord","stateCode":"35"},
   {"id":46,"name":"Accoville","stateCode":"50"},
   {"id":47,"name":"Ace","stateCode":"44"},
   {"id":48,"name":"Achille","stateCode":"37"},
   {"id":49,"name":"Achilles","stateCode":"46"},
   {"id":50,"name":"Ackerly","stateCode":"44"}
],
"cityList":null,
"page":1,
"records":29738,
"rowNum":null,
"rows":50,
"total":595,
"userList":[],
"userList1":null
}

我的struts.xml也是这样的。

<action name="server" class="com.actions.userList" method="getAllUsersViaScript" >
   <result name="success" type="json" >/WEB-INF/list.jsp</result>
</action>

这是代码。

我还为我在 java 页面中初始化的所有变量编写了 setter 和 getter。任何人都知道我这边的错误是什么。

编辑#1

当我粘贴代码时,它会在我的 jsp 页面中生成 jqgrid 模板,但数据没有填充到 jqgrid 中,即使我在控制台中正确获取了所有数据。分页和所有在网格中工作,但我需要列出的数据我无法插入到网格中。

【问题讨论】:

  • “打印”?您的意思是在网格中显示它们吗?
  • 是的,实际上我在控制台中正确获取了数据。我已经打印了问题中的数据。我想在我的网格中查看该数据。
  • 你为什么不用jqGrid插件?它将帮助您毫无问题地显示网格

标签: json jqgrid struts2


【解决方案1】:

在你的 html 正文部分使用它

 <table id="list2" cellpadding="0" cellspacing="0" class=child>    
</table>
 <div id="pager2" class="scroll" style="height:50px"></div> 
 <table>

编辑: 好的,您使用的是哪个版本的 jqGrid?我可以看到你没有使用“postData”。

从我的工作代码中检查以下示例:

  $grid = $("#table");
        $grid.jqGrid({
            url: '../../Services/BranchService.asmx/getBranches',
            datatype: 'json',
            mtype: 'POST',
            ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
            serializeGridData: function (postData) {

                if (postData.searchField === undefined) postData.searchField = null;
                if (postData.searchString === undefined) postData.searchString = null;
                if (postData.searchOper === undefined) postData.searchOper = null;
                //if (postData.filters === undefined) postData.filters = null;
                return JSON.stringify(postData);
            },

            jsonReader: {
                root: "d.rows",
                page: "d.page",
                total: "d.total",
                records: "d.records",
                id: "d.names"
            },
            colModel: [
                     { name: 'select', label: 'select' },                        
                     { name: 'code', label: 'Branch Code' },
                     { name: 'name', label: 'Branch Name' },
                     { name: 'status', label: 'Branch Status'}

                ],


            rowNum: 10,
            rowList: [10, 20, 300],
            sortname: 'name',
            sortorder: "asc",
            pager: "#pager",
            viewrecords: true,
            gridview: true,
            rownumbers: true,
            height: 250,
            autowidth: true,
            caption: 'Branch List',              

        }).jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, search: true });

【讨论】:

  • 嗨,dark,我想你弄错了我的问题。实际上我的问题是这样的,我的网格布局是生成的,分页和总页数在网格布局中正确生成。但是网格中没有加载任何值。
  • 我正在使用最新的 jqgrid。是jqGrid4.0
  • 谢谢你,dark 非常感谢你。我想问的另一件小事是,有没有办法更新这个 jqgrid 函数中的结果字段。我想如果一个字段值是真的,我想将它打印为“是”,否则为“否”。有什么选择吗?
  • 很高兴它为您工作。我通常从我的后端代码中执行此操作,以根据 true 或 false 设置字段值 yes 或 no,这是一种更简单的方法。但我从来没有尝试过在 jqgrid 上加载它。
  • 是的,我会尝试从后端更改它。再次感谢黑暗。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多