Reputation: 555
I´m working on a Struts application and I need to populate a table in jqGrid with a list of objects.
I don´t want to use ajax for populate the table, I want to pass in the request the list of objects and in JSP generate the table with it.
How I can do it??
Searching I found a question with my same problem: http://www.coderanch.com/t/549822/Struts/fill-jqgrid-java-object-list
Regards
Upvotes: 0
Views: 2894
Reputation: 555
Finally i use JSON to pass the data across the request from Java to Javascript
In Struts Action I do that
List<Object> dataList = Service.getData();
request.setAttribute("dataList", new Gson().toJson(dataList));
Then in Javascript code in JSP I do an eval of JSON value and transform the string passed into an array:
<table id="tableExample"></table>
<script>
$("#tableExample").jqGrid({
datatype: "local",
height: 250,
data: eval('<%= request.getAttribute("dataList") %>'),
colNames:['col1','col2', 'col3', 'col4'],
colModel:[
{name:'a1', index:'a1', width:150, align:"right", sorttype:"float"},
{name:'a2', index:'a2', width:150, align:"right", sorttype:"float"},
{name:'a3', index:'a3', width:150, align:"center", sorttype:"date", formatter:'date', formatoptions:{"srcformat":"d/m/Y","newformat":"d/m/Y"}},
{name:'a4', index:'a4', width:150, align:"center", sorttype:"date", formatter:'date', formatoptions:{"srcformat":"d/m/Y","newformat":"d/m/Y"}}
],
caption: "Table Example"
});
</script>
Upvotes: 1
Reputation: 221997
You don't posted any details of the JavaScript code which create jqGrid. So I try just guess what you currently do and what you can do to fix the described problem. I suggest that you try to use datatype: "json"
, loadonce: true
and the following jsonReader
:
jsonReader: {
root: function (obj) { return obj; },
repeatitems: false
}
If there are exist the column of the grid which contains unique information like primary key or which is just unique in every row of the grid then you should add in jsonReader
additional property like id: "columName"
, where "columName"
is the name of the column with unique information.
Upvotes: 1