简体   繁体   中英

Unable to load jqGrid using json in struts2

This is my Jsp Page using jQuery

   jQuery("#jqGrid01").jqGrid({

            url: "JqGridDemoJson.action",
            datatype:"json",
            height: 200,
            rowNum: 10,
            rowList: [10,20,30],

 colNames:['Inv No','Name'],
  colModel:[
             {name:'id',index:'id', editable: true,sorttype:"int",search:true},
             {name:'name',index:'name', editable: true,width:30}
            ],
            pager: "#jqGridPager01",
            viewrecords: true,
            add: true,
            edit: true,
            addtext: 'Add',
            edittext: 'Edit',
            caption: "Data",
            hidegrid:false
        });

This is my action retriveing list:

     {"JQgridAction":"success",
         "mitnolist":
         [{"id":1,"name":"MIT\/1009\/SUF-"},
         {"id":2,"name":"MIT\/1010\/SUF-"},
         {"id":5,"name":"MIT\/1011\/SUF-Adma Site"},
     ]}

This list unable to load on the above JQgrid.

Your json is an object while jqgrid uses [{}, {}, {},...] array of multiple objects so you have to return your json as this example:

         [{"id":1,"name":"MIT\/1009\/SUF-"},
         {"id":2,"name":"MIT\/1010\/SUF-"},
         {"id":5,"name":"MIT\/1011\/SUF-Adma Site"}]

or there is another way that you write a js ajax function and pass the required data to your jqgrid:

$.ajax({
   url: "JqGridDemoJson.action",
   dataType: 'json',
   type: 'post',
   success: function(data){
      makeGrid(data.mitnolist); // as this seems to be populated in grid
   }
});

now in your jqgrid you can do this:

function makeGrid(gData){ // pass in args
   $("#grid").jqGrid({
     data: gData, //  your data to populate in grid
     datatype: "local", // now change the datatype to local
      .....
  });
}

A small working example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM