开发者

Deleting selected rows - Struts2

I'm trying to create a web page with an history of what I do using Struts 2. After submit, a row containing params of the previous page is added to my history table.

What i want to do is allow the user to delete records from this table by checking one or more rows and pressing the button "Delete Selected".

Edit: the table is created dynamicaly with a list object, so I don't want to delete html rows but I need to remove the record from the list.

Table is created as follows:

<table>
    <tr>
        <th></th>
        ...
    </tr>
    <s:iterator value="myList">
        <tr>
            <td>
                <input type="checkbox" />
            </td>
            <td>
                <!-- other stuff -->
            </td>
        </tr>
    </s:iterator>
</table>

This way, using what I know, I can iterate through checkboxes only using Javascript.

To get the row number I'm using Javascript

 function checkRows() {
     var rowsIndex;
     var rows = document.getElementById('historyTable').getElemen开发者_Go百科tsByTagName('tbody')[0].getElementsByTagName('tr');
     for (i = 1; i < rows.length; i++) {
         if(rows[i].getElementsByTagName('td')[0].getElementsByTagName('input')[0].checked) {
             // here "i" is the row number if checked
         }
     }
 }

This is the button

   <s:url id="urlRemove" action="myAction">
       <s:param name="rowsIndex">
           <!-- List of index of selected rows -->
       </s:param>
   </s:url>
   <s:a href="%{urlRemove}">
       <input type="button" value="Remove Selected" />
   </s:a>

I know that after I submit what has been modified with Javascript is lost so I guess I have to fill a list in the Action with the checked row numbers from Javascript or assign to <s:param name="rowsIndex"> a property but I still have to iterate through rows with javascript.

Any help is appreciated, thanks.


nmc's answer points to the standard way. To rely on Javascript is unnecesary and IMO ugly here. BUT nmc's solution has also a problem: it's VERY dangerous to identify the item to delete by the position it occupies in some dynamic listing. (What if in the meantime someone has add/delete some items? What if the user double submits, or goes back and resubmit?)

You should identify each item by some unique stable key, your action can take care of that. For example, say your action provides the items in a map, with a secure key:

public class CheckBoxSampleAction extends ActionSupport
  private Map<Integer, DataWrapper> mymap; // data to show, with "secure stable" keys

  public Map<Integer, DataWrapper> getDataAsMap() {
       // fill lazyly mymap and return it
  } 

  public Set<Integer> getMapKeys(){
        return getDataAsMap().keySet();
  }

  private class DataWrapper {
    private String myvalue;
    private boolean selected; // for checkbox
        ... // more properties, getters, setters...
  }
 }

And in your jsp:

<s2:form theme="simple">
<table>
<s2:iterator value="mapKeys"><tr>
<td><s2:property value="dataAsMap[top].mivalue"/></td>
<td><s2:checkbox name="dataAsMap[%{top}].selected" theme="simple"/></td>
...
</tr></s2:iterator>
</table>
<s2:submit value="Remove selected" method="remove"/>
</s2:form>


Without knowing other details of your specific situation, I would suggest changing you implementation. Surround the list iteration with a form tag so that you can easily submit it to an action while passing in all the necessary info like so:

<s:form id="urlRemove" action="myAction">
  <s:iterator value="historyList" status="rowStatus">
    <s:checkbox name="rowsIndexToRemove" value="#rowStatus.index"/>
    <!-- output row details -->
  </s:iterator>
  <s:submit value="Remove Selected" />
</s:form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜