views:

111

answers:

2

Hi, for some reasons I am trying to translate the following RoR view code to a GSP view:

List<Object> objectscontains the data I want to display in 3 columns

<% 
modulo_objects = @objects.length % 3
base = @objects.length / 3
base = base.ceil
case modulo_objects
  when 0
 cols = [base, base, base]
  when 1
 cols = [base, base + 1, base]
  when 2
 cols = [base + 1, base, base + 1]
end

counter = 0
%>

<% 3.times do |i| %>
 <td width="220" align="center" style="padding-right: 15px;">
      <% cols[i].times do %>
  <h1><a href="/objects/show/<%= @objects[counter].urlname %>" ><%= @objects[counter].name %></a></h1>
  <% counter = counter + 1 %>
  <% end %>
 </td>
<% end %>

This is what I got so far:

#{extends 'main.html' /}
%{
modulo_objects = objects.size() % 3
base = objects.size() / 3
base = Math.ceil(base)
if(modulo_objects == 0)
    cols = [base, base, base]
else if(modulo_objects == 1)
    cols = [base, base + 1, base]
else if(modulo_objects == 2)
    cols = [base + 1, base, base + 1]
endif

counter = 0

}%

 #{list items:1..3, as:'i'}
     <td width="220" align="center" style="padding-right: 15px;">
         #{list items:cols[i]}
            <a href="@{Objects.show(objects.get(counter).name.replaceAll(" ", "-"))}" >${objects.get(counter).name}</a>
            %{ counter = counter + 1 }%
         #{/list}
     </td>
 #{/list}

The idea is to keep the items organised in 3 columns like 1|0|1 4|5|4 or 5|4|5 for example, I don't really understand if #{list items:cols[i]} will reproduce ruby's cols[i].times do.

So far the Java view is does not display more than two elements.

A: 

just a comment(sorry don't have privileges):

i do think the code above your html should go to your controller since that coding style renders your MVC framework void :P just saying

corroded
A: 
  public static List<Object>[] splitIn(List<Object> objects, int i) {

    int base_objects = objects.size() / i;
    int modulo_objects = objects.size() % i;
    int[] colSize = new int[i];

    switch (modulo_objects) {
        case 0:
            colSize[0] = base_objects;
            colSize[1] = base_objects;
            colSize[2] = base_objects;
            break;
        case 1:
            colSize[0] = base_objects;
            colSize[1] = base_objects + 1;
            colSize[2] = base_objects;
            break;
        case 2:
            colSize[0] = base_objects + 1;
            colSize[1] = base_objects;
            colSize[2] = base_objects + 1;
            break;
    }

    List<Object>[] columns = new List[i];

    int count = 0;
    for (int x = 0; x < i; x++) {
        List<Object> col_objects = new ArrayList();
        int colCount = 0;
        while (colCount < colSize[x]) {
            Object Object = (Object) objects.get(count);
            col_objects.add(Object);
            colCount++;
            count++;
        }
        columns[x] = col_objects;
    }
    return columns;
}






#{list cols, as:'column'}
  <td width="220" align="center" style="padding-right: 15px;">
      #{list column, as:'object'}
                <h1><a href="@{Objects.show(object.urlName())}" >${object.name}</a></h1>
      #{/list}
   </td>
   #{/list}
mnml