如何在Freemarker中按日期对对象列表进行分组?

时间:2016-05-13 08:55:10

标签: freemarker

<#setting date_format="dd/MM/yyyy">
<#assign data1 = "14/05/2016" />
<#assign data2 = "20/05/2016" />
<#assign data3 = "19/06/2016" />
<#assign events_list = [
  {"name":"Event Lorem", "date":data1?date},
  {"name":"Event Lipsum", "date":data2?date},
  {"name":"Event Free", "date":data2?date},
  {"name":"Event Dolor", "date":data3?date},
  {"name":"Event Sit", "date":data1?date}
] />

我有这个事件列表,我按日期排序:

<h5>Order by date:</h5>
<#list events_list?sort_by("date") as event>
    <p>${event.name}: ${event.date}</p>
</#list>

enter image description here

现在我想按日期分组,因为每个人都可以有几个小时 就像在这个例子中一样:

enter image description here

如何在Freemarker中完成此操作?

2 个答案:

答案 0 :(得分:2)

<#setting date_format="dd/MM/yyyy">
<#assign dataNull = "01/01/1900" />
<#assign data1 = "14/05/2016" />
<#assign data2 = "20/05/2016" />
<#assign data3 = "19/06/2016" />
<#assign events_list = [
  {"name":"Event Lorem", "date":data1?date},
  {"name":"Event Lipsum", "date":data2?date},
  {"name":"Event Free", "date":data2?date},
  {"name":"Event Dolor", "date":data3?date},
  {"name":"Event Sit", "date":data1?date}
] />




<h5>Order by date:</h5>
<#assign lastDate = dataNull?date>
<#list events_list?sort_by("date") as event>

    <#if lastDate != event.date >
      <p><b> ${event.date}</b></p>
    </#if>

    <#assign lastDate = event.date >
    <p>${event.name}: ${event.date}</p>

</#list>

按日期排序:

14/05/2016

活动Lorem:14/05/2016 2016/05/14

活动时间:14/05/2016 2016/05/14

20/05/2016

活动Lipsum:20/05/2016 20/05/2016

免费活动:20/05/2016 20/05/2016

19/06/2016

活动Dolor:19/06/2016 19/06/2016

答案 1 :(得分:1)

另请参阅此答案,以了解如何使用宏实施分组列表。我认为这比其他答案更可重复使用:https://stackoverflow.com/a/61588885/606679