Reputation: 1377
I am using jquery grid and Struts2. I have displayed the jquery grid successfully. Now i want to select rows from jquery grid plus other data outside grid and submit that in my struts action for processing. I have a problem with retrieving the data from jquery grid. can anyone help on how to access the rows that i have selected from jquery grid in my action?
here is my jquery grid:
<s:form action="runTest" method="post">
<sjg:grid
id="gridTable"
formIds="mcp"
caption="Test Scripts"
dataType="json"
href="%{searchurl}"
pager="true"
gridModel="gridTDP"
rowList="10,15,20,30"
rowNum="30"
multiselect="true"
viewrecords="true"
reloadTopics="reloadItems"
loadonce="false"
viewsortcols="[true, 'horizontal', true]"
resizable="true"
>
<sjg:gridColumn name="idTestData"
index="idTestData"
key="true"
title="idTestData"
formatter="integer"
hidden="true"
/>
<sjg:gridColumn name="testName"
index="testName"
title="Test Name"
width="50"
sortable="true"
search="true"
searchoptions="{sopt:['eq','ne','lt','gt']}"
/>
<sjg:gridColumn
name="projectName"
index="projectName"
title="Project Name"
width="300"
sortable="true"
edittype="text"
/>
</sjg:grid>
<sj:submit value="Execute Test Scripts"/>
</s:form>
Here is the runTest.action which is successfully called on but not getting the jquery grid rows:
public String runTest() throws Exception{
Iterator<Test> gridIterator = gridTDP.iterator();
System.out.println(" running test--");
while(gridIterator.hasNext()){
System.out.println(" in the gridIterator");
}
return "success";
}
Upvotes: 2
Views: 1429
Reputation: 1377
To resolve this, I used the code mentioned by "looser" (thank you very much) and I made some modifications. Instead of treating the value of selectedRows as an array, I treated it as a String that has comma delimited values, eg., abc,def. Then in my action class, I used StringTokenizer to parse the String to get the values. Here is my action method:
public String runTest() throws Exception{
System.out.println(" running test--");
StringTokenizer ids = new StringTokenizer(selectedRows, ",");
while (ids.hasMoreTokens()) {
int removeId = Integer.parseInt(ids.nextToken());
System.out.println(" test id is " + removeId);
}
return "success";
}
Upvotes: 0
Reputation: 1219
If I am correct I think this might work.. If not let me know.
var checks=$("#gridtable").find('input[type=checkbox]');
var va =new Array();
// alert(checks.length);
for(var i=0;i<checks.length;i++)
{
if(checks[i].checked)
{
var p = checks[i].parentNode.parentNode;
var id=p.id;
va.push($("#gridtable").jqGrid('getCell',id,'cellvalue'));
// alert(va);
}
}
document.getElementById("hiddenVariableID").value=va;
that cellvalue
is the value that you want to send to the action class.
I think in your case it can be any column name.
in your jsp add thi line
<s:hidden id="hiddenVariableID" name="selectedRows" />
And in your action class create Getters
and setters
for selectedRows
with the type as array
Upvotes: 2