How does the archive widget render the years and the months under each year?

Topics: ASP.NET 2.0
Jan 9, 2015 at 1:49 PM
I like the way the archive widget renders the years and the months under each year and want to do that for a project I am working on. Can someone explain how it's done? The project I am working is using Entity Framework and cannot figure out how to do it with EF.
Jan 9, 2015 at 6:23 PM
Don’t know anything about EF, but here’s a summary for the BE side of things.

Everything works of a sorted dictionary we create, having ‘date type’ as key (year, month, day) and ‘int’ as post count value.

Example
key and count
2015 1 1 (2)
2014 12 1 (5)
2014 11 1 (8)

Create Dictionary
Cycle through the BE post list and count the posts to your dictionary by their year/month part.
Unique post-date (or first occurrence of post-date) is added and counted as 1, thereafter any matching dates (on year/month) have their corresponding dictionary count incremented. We are interested only in the year and month part of the date key (can set day to 1, its irrelevant).

If all you wanted to do was render a flat month list, job done, but we want to group and render by year.

In BE this grouping is represented by means of nested unordered lists i.e. a single unordered list having list items (li) that represent years. Each year list item in turn contains a nested unordered list of month list items.

In MonthList.cs

The RenderMonth method is only concerned with formatting links for month list items.
Grouping is accomplished by applying two other methods:
RenderYearGroupBegin and RenderYearGroupEnd and these methods are applied whenever a boundary year is encountered (a change in year – see RenderControl method). So using the example from above:

Boundary – year start – apply RenderYearGroupBegin() - results in <li><ul>
2015 1 1 (2) apply RenderMonth() - results in <li>month link and count</li>
Boundary – year end – apply RenderYearGroupEnd() - results in </ul></li>

Boundary – year start – apply RenderYearGroupBegin() - results in <li><ul>
2014 12 1 (5) apply RenderMonth() - results in <li>month link and count</li>
2014 11 1 (8) apply RenderMonth() - results in <li>month link and count</li>
Boundary – year end – apply RenderYearGroupEnd() - results in </ul></li>

Hopefully this helps and can apply pseudo outline to EF.