4

I need to show a list of boy and girl names in order. The data structure that send names to the view is List<String>.

The issue is that the result is as follows (for example, boy names that start with a) :

(ordered list is shown from left to right)

Aaron  Abraham   Adam   
Alen   Alex      Alexander  
..

I need to show them as (ordered list should be shown from top to button then go to second column)

Aaron     Alen   ...
Abraham   Alex
Adam      Alexander
...

Needless to say, each group has varied number of names. For example group 'a' might have more names compared to group 'b'.

<c:forEach var="name" items="${names}">
    <div class="col-md-4">
        ${name}
   </div>
</c:forEach>
Navas Basheer
  • 940
  • 2
  • 9
  • 17
Daniel Newtown
  • 2,873
  • 8
  • 30
  • 64

2 Answers2

3

First You need to fix the number of Columns used for displaying your data, For the explanation I'm taking it as 3

and let there are n number of Boys and girls, The Logic is for the First column we will display n/3 records, next row n/3 and in next column remaining records

Below is JSP Code for 3 column display :

<div class="col-md-4">
    <c:forEach begin="0" end="${fn:length(names)/3}" var="name" items="${names}" varStatus="loop">
        ${name}<br/>
    </c:forEach>
</div>
<div class="col-md-4">
    <c:forEach begin="${(fn:length(names)/3) + 1}" end="${(fn:length(staffs)/3)*2}" var="name" items="${names}" varStatus="loop">
        ${name}<br/>
    </c:forEach>
</div>
<div class="col-md-4">
    <c:forEach begin="${((fn:length(names)/3)*2)+1}" var="name" items="${names}" varStatus="loop">
        ${name}<br/>
    </c:forEach>
</div>

NB : Here i used Core JSTL function fn:length(names) for computing the length of the array, For using this you need to add

 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

at top of your page. See this

The sorting of Names should be done from the Server side itself for the required output. Also if you want to display in 4 column or 5 column just change the division of 3 by your required column number. It will work

Community
  • 1
  • 1
Navas Basheer
  • 940
  • 2
  • 9
  • 17
0

First Approach

  1. You can use column-count css property: https://css-tricks.com/almanac/properties/c/columns/

Second Approach

  1. Alternatively, you may want to change col-md-4 to col-md-6 to make them split into two columns: ``` ${name} ```
  2. Calculate how many names you have, then have an if else when half the count of name is reached, then switch to the second column
  3. This is hacky but works
William
  • 740
  • 2
  • 11
  • 18