由于某些原因,我试图将以下RoR视图代码转换为GSP视图:
List<Object> objects
包含我要在3列中显示的数据
<%
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 %>
这是我到目前为止所得到的:
#{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}
这个想法是将项目组织成3列,例如1 | 0 | 1 4 | 5 | 4或5 | 4 | 5,
我真的不明白#{list items:cols[i]}
是否会重现ruby的cols[i].times do
。
到目前为止,Java视图不会显示两个以上的元素。
答案 0 :(得分:0)
只是评论(抱歉没有特权):
我认为你的html上面的代码应该转到你的控制器,因为编码风格会使你的MVC框架无效:P只是说
答案 1 :(得分:0)
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}