You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mi...@edgil.com on 2004/02/24 15:51:29 UTC

logic:iterate, checkboxes, and arrays of selected values...

Hi folks,

I have a problem which I have a kludgy solution for, and have to believe
there is a more elegant way of doing it...  Here's the scenario.

The page displays a non-editable list of records in a database, with a
checkbox at the beginning of each row.  The list is indeterminate length,
based on selection criteria entered by the user in the previous page.  The
user selects n number of rows, using the check boxes.  Those rows are to be
deleted from the database.

The trick is that the rows have a three column key.  One is constant based
on the user, so I need to get 2 pieces of information from each row before
I can delete it.

My kludge is to have the value of the checkboxes be the indexId of the
logic:iterate.  Each row has two hidden input fields, with the two required
key values.  Something like this:

  <logic:iterate id="item" name="listForm" property="txnList" type
="Transaction" indexId="counter">
    <tr>
      <td width="69" align="center">
        <input type="checkbox" name="selectedTxns" value="<%=counter%>"/>
      </td>
      ....  display data ...
      <td width="88"><%=item.getTotal()%></td>
      <!-- Hidden input fields so we can retrieve all the required data
about the selected transaction... -->
      <td class="sidelink" width="1">
        <input type="hidden" name="orderIds" value="<%=item.getOrderID
()%>"/>
      </td>
      <td class="sidelink" width="1">
        <input type="hidden" name="deptAreas" value="<%=item.getDeptArea
()%>"/>
      </td>
    </tr>
  </logic:iterate>

In the formData bean, I have three String [ ] attributes to accept the
values of the checkbox and the two hidden fields.

The problem is that since there is data in the hidden input fields for
every row, I get every value back in the array, instead of only the data
for the rows which are checked.  What I have to do in the action is read
the values from the String [ ] for the checkbox , and use that value as the
index into the other two string arrays.

Like I said, its kludgy, but it works...

But, I have to believe there's a way to send back an array of beans, with
only the values of the fields for those rows which are checked, but I can't
find anything in books or searching the web which describes how to do this.

Does anyone have a cleaner way to do this type of thing?

Thanx!

c'ya
Mike
----
Mike Boucher                  mboucher@edgil.com
Edgil Associates              www.edgil.com

"Don't take life too seriously, you'll never get out of it alive!"


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: logic:iterate, checkboxes, and arrays of selected values...

Posted by "Michael D. Norman" <mi...@probusoft.com>.
A couple of options:

1) Use a key for the checkbox that is a combination of the two keys,
combined with a character that cannot be in the keys themselves (e.g.
key1::key2).  They just separate them in the action, or better yet, have the
form separate them.

2) Use JavasSript on the check of a row to enable/disable the two hidden
fields.  Disabled fields do not get transferred to the server.  However,
this may not be an option because it is browser-dependant.

I'd probably go with the first option.

-- Michael D. Norman
   ProbuSoft -- Custom Software Development
   http://www.probusoft.com/
   913-390-6951
   michael@probusoft.com
 
> -----Original Message-----
> From: MichaelBoucher@edgil.com [mailto:MichaelBoucher@edgil.com]
> Sent: Tuesday, February 24, 2004 8:51 AM
> To: struts-user@jakarta.apache.org
> Subject: logic:iterate, checkboxes, and arrays of selected values...
> 
> Hi folks,
> 
> I have a problem which I have a kludgy solution for, and have to believe
> there is a more elegant way of doing it...  Here's the scenario.
> 
> The page displays a non-editable list of records in a database, with a
> checkbox at the beginning of each row.  The list is indeterminate length,
> based on selection criteria entered by the user in the previous page.  The
> user selects n number of rows, using the check boxes.  Those rows are to
> be
> deleted from the database.
> 
> The trick is that the rows have a three column key.  One is constant based
> on the user, so I need to get 2 pieces of information from each row before
> I can delete it.
> 
> My kludge is to have the value of the checkboxes be the indexId of the
> logic:iterate.  Each row has two hidden input fields, with the two
> required
> key values.  Something like this:
> 
>   <logic:iterate id="item" name="listForm" property="txnList" type
> ="Transaction" indexId="counter">
>     <tr>
>       <td width="69" align="center">
>         <input type="checkbox" name="selectedTxns" value="<%=counter%>"/>
>       </td>
>       ....  display data ...
>       <td width="88"><%=item.getTotal()%></td>
>       <!-- Hidden input fields so we can retrieve all the required data
> about the selected transaction... -->
>       <td class="sidelink" width="1">
>         <input type="hidden" name="orderIds" value="<%=item.getOrderID
> ()%>"/>
>       </td>
>       <td class="sidelink" width="1">
>         <input type="hidden" name="deptAreas" value="<%=item.getDeptArea
> ()%>"/>
>       </td>
>     </tr>
>   </logic:iterate>
> 
> In the formData bean, I have three String [ ] attributes to accept the
> values of the checkbox and the two hidden fields.
> 
> The problem is that since there is data in the hidden input fields for
> every row, I get every value back in the array, instead of only the data
> for the rows which are checked.  What I have to do in the action is read
> the values from the String [ ] for the checkbox , and use that value as
> the
> index into the other two string arrays.
> 
> Like I said, its kludgy, but it works...
> 
> But, I have to believe there's a way to send back an array of beans, with
> only the values of the fields for those rows which are checked, but I
> can't
> find anything in books or searching the web which describes how to do
> this.
> 
> Does anyone have a cleaner way to do this type of thing?
> 
> Thanx!
> 
> c'ya
> Mike
> ----
> Mike Boucher                  mboucher@edgil.com
> Edgil Associates              www.edgil.com
> 
> "Don't take life too seriously, you'll never get out of it alive!"
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: logic:iterate, checkboxes, and arrays of selected values...

Posted by Lynn Guy <lg...@yahoo.com>.
Put ALL the values in an arraylist and manipulate the arraylist values uppon return to the action.  All the values are available in the same row together.   The trick is getting the values from the arrayList back to the action when the form is submitted.  I'm a genuine newbie and I have managed to pull this off numerous times now and it has made life much easier.  I pieced together how to do this from searching the archives on this list including a previous post to someone else how to do it.  If you cant find it you can send me an email off line and I'll send you some sample code.

MichaelBoucher@edgil.com wrote:Hi folks,

I have a problem which I have a kludgy solution for, and have to believe
there is a more elegant way of doing it... Here's the scenario.

The page displays a non-editable list of records in a database, with a
checkbox at the beginning of each row. The list is indeterminate length,
based on selection criteria entered by the user in the previous page. The
user selects n number of rows, using the check boxes. Those rows are to be
deleted from the database.

The trick is that the rows have a three column key. One is constant based
on the user, so I need to get 2 pieces of information from each row before
I can delete it.

My kludge is to have the value of the checkboxes be the indexId of the
logic:iterate. Each row has two hidden input fields, with the two required
key values. Something like this:

="Transaction" indexId="counter">


 [input] " name=selectedTxns>

.... display data ...

about the selected transaction... -->

 [input] ()%>" name=orderIds>


 [input] ()%>" name=deptAreas>




In the formData bean, I have three String [ ] attributes to accept the
values of the checkbox and the two hidden fields.

The problem is that since there is data in the hidden input fields for
every row, I get every value back in the array, instead of only the data
for the rows which are checked. What I have to do in the action is read
the values from the String [ ] for the checkbox , and use that value as the
index into the other two string arrays.

Like I said, its kludgy, but it works...

But, I have to believe there's a way to send back an array of beans, with
only the values of the fields for those rows which are checked, but I can't
find anything in books or searching the web which describes how to do this.

Does anyone have a cleaner way to do this type of thing?

Thanx!

c'ya
Mike
----
Mike Boucher mboucher@edgil.com
Edgil Associates www.edgil.com

"Don't take life too seriously, you'll never get out of it alive!"


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org