• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic HTML Multi Select Approach  RSS feed

 
Darren Edwards
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is easiest to explain with a short example, so bear with me!

A bean Person with attribute favouriteColour.
A bean Colour with attributes id and name.

Pass a list of Colour beans and one person bean through to a JSP to present a select list.



How would you achieve the same thing if you wanted a multi select list? You could do some ugly nested jsp looping, an EL function, a wrapper bean with selected attribute or custom EL ... what would you recommend as I'm undecided as to the best solution.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you achieve the same thing if you wanted a multi select list?


I don't understand how this changes anything except for putting the multiple attribute on the select element.

Given the way you described this, a person can still only have one favorite color.

P.S. Terminology correction:

A bean has properties, not attributes.
 
Darren Edwards
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I should have clarified - a Person can now have multiple favourite colours, meaning there could be multiple selected options.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, the plot thickens.

In that case I would probably create an EL function that you could easily plug into your existing <c:if> that would return a boolean true if the passed color is one of the favorites.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my approach would probably be somewhere along the lines of:

<option name="${colour.id}" ${person:hasFavouriteColorOf(person,colour.id) ? 'selected' : ''}>${colour.name}</option>

P.S. Should not the attribute on <option> be value rather than name?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66154
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.P.S. In actuality, this type of test is common enough that I've created a generic EL function that tests if a value is contained within a collection that could be used in a similar fashion.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!