i am using data table to show the customer records on jsp page where i show customer name,last name,department,joining date in data table. i have one more column which is select box. now user can user select any number of customer records from ui and send to action on server side for update. In my jquery function i get the selected rows in data table. But i am not able to figure out how to send the these rows to action method? what i am expecting is i send these rows from ajax to action method which has one method parameter as list(representing the selected rows) and i can extract field values from each row in list representing selected rows from UI. But i am unsure what will be type of object that list will contain?Can someone point me in right direction with some brief example ?
2 Answers
Why dont you give a checkbox in each row whose value will be the id of the corresponding record
<input type="checkbox" name="rec" value="{id of the record}"/>
Then when you submit get the selected ids
var selectedRec="";
$("input[name='rec']:checked").each(function(){
selectedRec += $(this).val()+",";
});
you can now pass selectedRec to the action as a string variable. In your action you can split this string as array
String[] recArray = selectedRec.split(",");
Then in the loop you can get each id as follows
int customerId = Integer.parseInt(recArray[i]);
4 Comments
This is another way to do, where you don't need to iterate your selected record in JQuery and create a string in Script.
Step 1. Create checkbox
<s:checkbox name="Bean.record" id="record" fieldValue="%{id of the record}" />
Step 2. Create String[] in your Bean class
private String[] record;
That's all.... so whatever record you will select on your JSP, value will be added into String Array and you can grab it on your action.
Hope this will help.