How to implement splitting by columns alpha ordered list as shown on the picture?
I think you mean something like this:
items = sorted(file("items.txt").readlines())
current = ""
clines = 0
maxlines = 50
print "<table><tr><td>"
for i in items:
if i[0].upper() != current:
current = i[0].upper()
print "<h1>", current, "</h1>"
clines += 1
if clines >= maxlines:
print "</td></td>"
clines = 0
print i
print "</td></tr></table>"
Surely the HTML sucks, but that's the idea, when you exceed the maximum lines per column, switch to the next column.
If you want to make sure that a column always starts with a heading, then you should switch to the next column when the current number of lines plus the size of the next group exceeds the maximum number of lines per column.
I would probably approach it in the following manner:
The hope is that using this technique - allowing each column to overflow slightly - will balance out over time and space. Otherwise, we just keep pushing groups to the next column and our final column ends up significantly longer than the others.
Done, thanks a lot to fortran and others.
Here comes solution that is acceptable for me
public ActionResult Test()
{
var groupedByLetter = from l in Letters
join service in EntityBase.db.Services on
l equals service.Name[0] into grouped
select new ServiceInfo {
Letter = l.ToStr(),
Services = grouped,
Total = grouped.Count()
};
List<string> result = new List<string>();
foreach (var l in groupedByLetter)
{
if (l.Services.Count() > 0)
result.Add(string.Format("<li><h1>{0}</h1></li>", l.Letter));
else
result.Add(string.Format("<li class='disabled'><h1>{0}</h1></li>", l.Letter));
foreach (var service in l.Services)
{
result.Add(string.Format("<li><a href='/service/info/{0}'>{0}</a></li>", service.Name));
}
}
return View(result);
}
private static readonly char[] Letters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ0".ToCharArray();
And HTML rendering
<%
int clines = 0;
int maxlines = Model.Count / 5;
%>
<% for (var i = 0; i < Model.Count; i++)
{
if (clines == 0)
{
%>
<ul class="col">
<%
}
%>
<%=Model[i]%>
<%
clines += 1;
if (clines >= maxlines)
{
if (Model[i + 1]!=null && Model[i+1].Contains("</a>"))
{
continue;
}
%>
</ul>
<%
clines = 0;
}
}%>
Result
A HashMap using a String (Or even a character) as the key (Letter of the alphabet) and a array as the value.
Looks like a website, probably an overview.
The answer is: templates and php