• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pulling a list of vendors and displaying them into columns JSP Functions

 
Greenhorn
Posts: 2
Android Mac OS X Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting a list of names and I would like to display them into (3) columns. Whats would be the best way to do this using a jsp function?

List:

apples
oranges
cherries
limes
pineapples
grapefruits
coconuts
watermelons
grapes
bananas
pears
peaches
Expected Output -

Column 1 - apples, oranges, cherries, and limes /
Column 2 - pineapples, grapefruits, coconuts, and watermelons /
Column 3 - grapes, bananas, pears, peaches

1. Lets say that I wanted to specify how many rows are in each column, what would be the best way to do this using a jsp function?

2. Another scenario would be, if there is a list of 13 items and you divide that by the number of columns (3), you would get 4.333333333..

What function example could I used to get 6 in the first column, 6 in the second column and 1 in the 3rd column?

Any thoughts or comments I would really be grateful for the help...


 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like just a little bit of math to me. The JSTL <c:forEach> tag can loop through the list, or even a number of steps (determined by dividing the list length by 3, for example).

To start, I'd figure out what the expected HTML should be, and then, write JSP to emit that HTML.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tricky bit is the ordering.
Printing out an HTML table, you normally specify a row at a time, rather than data for each column.

You want to do the transpose of that - all of column 1, then column2, then column3

To make the generation of the HTML easier, I would transform your list into a datastructure that mimics the layout of that table, and then print from that
- It could be a 2 dimensional table with some entries being blank
- It could be a List of rows then columns with null placeholders for a blank column.

You might find the subList function in List useful.


Here is some simple stupid java code that does something along those lines.
It takes the originally sorted list, and fiddles it into a list that can then be iterated over to produce your table (assuming new row after every 3 columns)
It is by no means perfect and not flexible at all, but may give you a couple of ideas.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic