• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nathan England
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ActionForm == Struts-specific. Moved to the Struts forum.
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
  •  
    Nathan England
    Greenhorn
    Posts: 20
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2904
    1
    Java Spring Ubuntu
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 20
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 2904
    1
    Java Spring Ubuntu
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic