This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes Selecting a single row in a table, then saving that index in the struts form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Selecting a single row in a table, then saving that index in the struts form" Watch "Selecting a single row in a table, then saving that index in the struts form" New topic
Author

Selecting a single row in a table, then saving that index in the struts form

Nathan England
Greenhorn

Joined: Jul 13, 2009
Posts: 20
I think this is more JSP related than struts specificaly, since what I am needing to do is in the JSP page.

I have my struts framework all set up, but I'm struggling with the page design and functionality. I have a jsp page, which is displaying a table. I need to be able to highlight and select exclusively a single row in the table when the row is clicked, and save that index in my ActionForm object. (I have a value in my ActionForm which will be a placeholder for the index).

I have the table displaying my list by using <logic:iterate>, but I have no idea how to get the row to "highlight" as selected, and save the index of the row into the ActionForm.

I've found some examples of using JavaScript to do the row highlighting, but I'm having second thoughts as to whether using JavaScript is the best way or if there is an alternative way using Struts tags, I'm willing to give that a try.

Here is some code:
My ActionForm:


in my jsp page, I am displaying the table:


Should I use JavaScript? Alternatives? This displays my table, but I need some guidance on how to do the row selection and row highlighting when the row is clicked.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61450
    
  67

ActionForm == Struts-specific. Moved to the Struts forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Nathan England wrote:I need some guidance on how to do the row selection and row highlighting when the row is clicked.

  • Row selection: Use "checkbox" with value as a unique row id or whatever the value which distinguish it from other rows
  • Row highlighting: Its basically a client side user action, and no one make it possible other than Javascript..


  • [LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
    Nathan England
    Greenhorn

    Joined: Jul 13, 2009
    Posts: 20
    Thanks for the tips. I really struggled with this. I'm not 100% out of deep water yet, but at least I'm getting the checkboxes to set a value in my data object submitted by the form.

    I'll post what I managed to get to work so far:
    Here is the <table> where my list is displayed:


    The part that really hung me up was the iterate, I had my id="person" when it should have been id="personList"
    After hours of digging I finally found something that mentioned this. The "id" and the "property" must be the same. (I'll worry about the "why" later).
    changed:
    <logic:iterate id="person" scope="session" name="PersonSearchForm" property="personList">
    to:
    <logic:iterate id="personList" scope="session" name="PersonSearchForm" property="personList">

    Now I need to find some JavaScript code somewhere that applies some fancy highlighting and selection for the table rows and I'll be all set...
    Sagar Rohankar
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 2902
        
        1

    Nathan England wrote:
    The part that really hung me up was the iterate, I had my id="person" when it should have been id="personList"
    After hours of digging I finally found something that mentioned this. The "id" and the "property" must be the same. (I'll worry about the "why" later).
    changed:
    <logic:iterate id="person" scope="session" name="PersonSearchForm" property="personList">
    to:
    <logic:iterate id="personList" scope="session" name="PersonSearchForm" property="personList">

    Wrong ! the "id" and "property" are two different thing for logic:iterate tag, former defined the current element of the collection on which you're iterating and later one is come with "name" attribute to get the bean which is stored in a scope.

    You might did some mistake elsewhere, maybe the logs file can explain the cause..
    Nathan England
    Greenhorn

    Joined: Jul 13, 2009
    Posts: 20

    Wrong ! the "id" and "property" are two different thing for logic:iterate tag, former defined the current element of the collection on which you're iterating and later one is come with "name" attribute to get the bean which is stored in a scope.

    You might did some mistake elsewhere, maybe the logs file can explain the cause..


    Well, that's the only thing I changed after trying dozens of different approaches, so may be wrong but it worked in my case.

    Actually, I should re-state my assertion. Here is a PDF I followed that helped me do this:

    http://www.laliluna.de/download/struts-html-checkbox-en.pdf

    What I mentioned about the value of the "id" and the value of the "property" being the same, I misquoted. From the PDF:

    Note: In order to refer a property of an element within the collection to a <html:checkbox> element
    the attribute id of the <logic:iterate> tag must have the same name like the collection of the form
    bean. Furthermore the attribute indexed of the <html:checkbox> have to be set to „true“.
    Sagar Rohankar
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 2902
        
        1

    I haven't tried this, but the Struts docs says something diffrent about the way it renders/pass the "property" for html:chckbox,
    here is the link for html:chckbo:
    http://struts.apache.org/1.3.10/struts-taglib/tagreference.html#html:checkbox

    this the quote that may be the cause:


    property* :
    Name of the request parameter that will be included with this submission, set to the specified value.


    And your bean data is in session scope, and your not finding the checkbook value there, find it in request parameter.

    HTH
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Selecting a single row in a table, then saving that index in the struts form