Friday, February 13, 2009

Checkbox list problem in Struts 2

I had faced a problem using s:checkboxList in a delete multiple entries from a table. But I was able to find a way to delete multiple rows in a table using s:checkbox . Only few steps you need to do.
  • First you have to create a list in the Action class.It has to be type of "String" list.Lets say I add string array called "String[] userIds" and the getters and setters for it.
  • Then how we can input list of checkboxes in to a table using struts 2.I used the "s:iterator " tag for that
  • You need a list for the "s:iterator" tag.In my case lets say List<"type"> users=New ArrayList<"type">(); which has id,name,email,mobile... like properties.Then insert a check box inside a tag.And your check box name has to be the name of the above string array name"userIds".Your code should look like this after that
  • s:iterator value= users" status="count">
    userIds" cssClass="checkboxSelect" fieldValue="%{getId()}"
    theme="simple"/>
  • Then I used a checkbox to select/deselect the list of check boxes in the table. I added the following check box in the column head
  • s:checkbox name="selectAll" cssClass="checkboxSelect" onclick="checkAllElements(document.deleteMultiple.selectAll);"theme="simple" "/>"
  • In here "deleteMultiple" is the name of the html form.
  • Then about the "onclick" function you need a javascript to select all the checkboxes in the table rows.
  • "function checkAllElements(toggle) {
    //checking all checkboxes in the data rows
    var elements = document.getElementsByName("userIds");
    if (elements && elements.length > 0) {
    for (var i = 0; i < checked =" toggle.checked;" headerelements =" document.getElementsByName("> 0) {
    for (var i = 0; i < checked =" toggle.checked;">
  • Rest is upto you.....(action mapping and action implementation ex:add a delete button and submit the above form to delete rows.In struts xml map the action with the class and implemet the delete method according to your application)


1 comments:

  1. I haven't tried it yet.But thanks for the post.will let you know.

    ReplyDelete

Custom Search

My Video Bar

Loading...