You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by JohnLangan <la...@homes.com> on 2007/10/02 17:02:12 UTC

Accessing a row in a table on a JSP page.

I call the database and create an ArrayList of beans to hold the data. Using
JSTL I iterate over the list
to display the data in an html table inside a form in a JSP page.

<c:set var="dataItems" value="${lmdao.listingManagerData}" />
<c:forEach var="item" items="${dataItems}" varStatus="row" >
I can give each row a number using ${row.index}

The last but one column is a Struts html:select that allows the user to
select what they want to do next. The last column contains a button to press
to implement the selected action. So far so good.

My problem is that in whichever row the button is pressed it always acts on
the data in the first row.
I have tried several ways including indexed properties or using an
ActionForm with an array of beans so each row is a copy of the bean, but I
cannot get the action to act on the required row.   

Is there a way in Struts for the action to know in which row the button was
pressed? Apart from the html:select dropdown all the other values required
for the action are hidden values. 

Thanks in advance for any help with this.

John.
-- 
View this message in context: http://www.nabble.com/Accessing-a-row-in-a-table-on-a-JSP-page.-tf4555354.html#a13000517
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Accessing a row in a table on a JSP page.

Posted by Nikhil Walvekar <wa...@gmail.com>.
Hi,

You can try using javascript, on button submit call a function which will
set row-index to a hidden field and which will be submitted with form. On
server side you can use this field to identify the row.

Regards,
Nikhil

On 10/2/07, JohnLangan <la...@homes.com> wrote:
>
>
> I call the database and create an ArrayList of beans to hold the data.
> Using
> JSTL I iterate over the list
> to display the data in an html table inside a form in a JSP page.
>
> <c:set var="dataItems" value="${lmdao.listingManagerData}" />
> <c:forEach var="item" items="${dataItems}" varStatus="row" >
> I can give each row a number using ${row.index}
>
> The last but one column is a Struts html:select that allows the user to
> select what they want to do next. The last column contains a button to
> press
> to implement the selected action. So far so good.
>
> My problem is that in whichever row the button is pressed it always acts
> on
> the data in the first row.
> I have tried several ways including indexed properties or using an
> ActionForm with an array of beans so each row is a copy of the bean, but I
> cannot get the action to act on the required row.
>
> Is there a way in Struts for the action to know in which row the button
> was
> pressed? Apart from the html:select dropdown all the other values required
> for the action are hidden values.
>
> Thanks in advance for any help with this.
>
> John.
> --
> View this message in context:
> http://www.nabble.com/Accessing-a-row-in-a-table-on-a-JSP-page.-tf4555354.html#a13000517
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Nikhil

Re: Accessing a row in a table on a JSP page.

Posted by Rick Reumann <ri...@gmail.com>.
On 10/2/07, JohnLangan <la...@homes.com> wrote:
>
> I call the database and create an ArrayList of beans to hold the data. Using
> JSTL I iterate over the list
> to display the data in an html table inside a form in a JSP page.
>
> <c:set var="dataItems" value="${lmdao.listingManagerData}" />
> <c:forEach var="item" items="${dataItems}" varStatus="row" >
> I can give each row a number using ${row.index}
>
> The last but one column is a Struts html:select that allows the user to
> select what they want to do next. The last column contains a button to press
> to implement the selected action. So far so good.
>
> My problem is that in whichever row the button is pressed it always acts on
> the data in the first row.

You can do what you want, but you have to be careful when working on
items in a collection  like that based off the index. Personally, I'd
like to work off some unique id that represents that item and do
another lookup of it when I get to my Action.

For example...
<c:forEach items="${myList} item="foo">
   <tr>
       <td><a href="/EditMe.do?id=${foo.id}">${foo.name}</td>
   </tr>
</c:forEach>

Now, when the user clicks that link(or you can make it a button also),
you go to Action and look up tthe item from the db again and set up
the form to display for the user to edit on the next page.

If you want to use the collection approach. You have to first fix a few things.
First off using <c:set var="dataItems" value="${lmdao.listingManagerData}" />
will be VERY bad.  Imagine you display your list after the above, now
you pick the 3rd item. How do you plan to get the 3rd item again? Are
you going to call that lmdao.getListinManagerData again in your action
and then pick the 3rd index? For one, the list might have changed so
now you are editing the wrong one, and also, why get the whole list
again? If you go that route your much better using the approach I
showed. The list index approach will work if you first put that list
in session scope BEFORE you go to your page and then you'd just do

//no c:set - use the session scoped list...
<c:forEach var="item" items="${dataItems}" varStatus="row" >
   <tr><td><a href="/DoSomething.do?index=${row.index}">${item.name}</td></tr>
</c:forEach>

Now you have the index when you click on the link so in your next
action you can pull the list out of session scope and then get the
item. (Again, I'm not a huge fan of this approach - mainly because now
you just stuffed a large collection in the session that really doesn't
need to be there. Approach one doesn't have this problem.)

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


Re: Accessing a row in a table on a JSP page.

Posted by JohnLangan <la...@homes.com>.
Thanks Nikhil and rickcr for the suggestions.

I have resolved the problem by creating a form within the iterator so that
each row is a separate form. Each row (form) uses the same ActionForm and
Action and returns the row specific info. This works for what we want as
only one row on the page will ever be selected. 

John.


JohnLangan wrote:
> 
> I call the database and create an ArrayList of beans to hold the data.
> Using JSTL I iterate over the list
> to display the data in an html table inside a form in a JSP page.
> 
> <c:set var="dataItems" value="${lmdao.listingManagerData}" />
> <c:forEach var="item" items="${dataItems}" varStatus="row" >
> I can give each row a number using ${row.index}
> 
> The last but one column is a Struts html:select that allows the user to
> select what they want to do next. The last column contains a button to
> press to implement the selected action. So far so good.
> 
> My problem is that in whichever row the button is pressed it always acts
> on the data in the first row.
> I have tried several ways including indexed properties or using an
> ActionForm with an array of beans so each row is a copy of the bean, but I
> cannot get the action to act on the required row.   
> 
> Is there a way in Struts for the action to know in which row the button
> was pressed? Apart from the html:select dropdown all the other values
> required for the action are hidden values. 
> 
> Thanks in advance for any help with this.
> 
> John.
> 

-- 
View this message in context: http://www.nabble.com/Accessing-a-row-in-a-table-on-a-JSP-page.-tf4555354.html#a13024543
Sent from the Struts - User mailing list archive at Nabble.com.


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