问题描述
我有一个Freemarker模板,涉及两次"LocalTime"和"ServerTime".如果我可以从数据中确定用户的LocalTime,则需要使用"LocalTime"& sort_by,否则我需要对ServerTime进行sort_by.整个数据存在于列表中(来自记录)
I have a Freemarker template and There are two times involved"LocalTime" and "ServerTime". If I can determine user's LocalTime from the data, i need to use "LocalTime" & sort_by it, else i need to sort_by ServerTime. The whole data exists within a list (from a record)
最初可以使用ServerTime进行操作,并且功能类似于:
Originally it was alright to do using ServerTime and the function was like:
<#list x.data.record?sort_by("ServerTime") as s>
但是,如果我要检查是否存在"LocalTime",该怎么办?
But if I want to check if there is "LocalTime" , how to do this?
我期待一些符合要求的事情.
I am expecting something in line..
<#list x.data.record?sort_by("ServerTime||LocalTime") as s>
(请注意,这与对两列进行排序不同)
(Please note, this is different from sorting two columns)
推荐答案
?sort_by
无法做到这一点.这将需要自定义比较器功能,但是?sort_by(myComparatorFunction)
不支持ATM.
?sort_by
can't do this. This would require a custom comparator function, but ?sort_by(myComparatorFunction)
is not supported ATM.
如果您不能将已经排序的列表添加到数据模型中,或者只想触摸表示层,您仍然可以用Java编写TemplateMethodModelEx
来返回排序后的列表,并将其拉入具有'<#assign sortByTime = com.example.SortByTimeMethod'?new()>
的模板,则可以编写<#list sortByTime(x.data.record) as s>
.
If you can't add the list already sorted to the data-model, or you only want to touch the presentation layer, you can still write a TemplateMethodModelEx
in Java that returns the sorted list, and pull it into the template with '<#assign sortByTime = com.example.SortByTimeMethod'?new()>
, then you can write <#list sortByTime(x.data.record) as s>
.
这篇关于Freemarker模板:"Sort_by"一个或另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!