问题
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)
Originally it was alright to do using ServerTime and the function was like:
<#list x.data.record?sort_by("ServerTime") as s>
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)
回答1:
?sort_by
can't do this. This would require a custom comparator function, but ?sort_by(myComparatorFunction)
is not supported ATM.
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>
.
回答2:
I am still a newbie with freemarker, but I believe I solved this just now for myself. Its ugly, but it works. I'm not dealing with thousands or even hundreds of rows most of the time, so efficiency is not a huge concern with this case. NetSuite is the system feeding into Freemarker, and its a bit of a pain.
Essentially, I run through the list once and store one value or the other (server/local time for you) into a single variable (invoiceDate
, in my case). I then put that variable, along with the other display variables I need, into a hash. That hash is then one line in a list.
To display: I run through the list, which is sorted by my invoiceDate
variable and for each line (hash), I print out the info I need from that key/value pair.
I know this was 4 years ago but I needed an answer tonight and couldn't find one until I thought about it this way...hope this helps the next guy!
<#assign y=[]>
<#list record.lines as tmpLine>
<#assign invoiceDate = tmpLine.datecol>
<#if (tmpLine.someIdentifyingMarker != "")>
<#assign invoiceDate = tmpLine.custbody_p_ending_date>
</#if>
<#assign dataLine = {"displayDate":invoiceDate, "Description":tmpLine.description, "CheckNum":tmpLine.custbody_checknumprint, "InvoiceAmount":tmpLine.charge,"Credit":tmpLine.payment,"Remaining":tmpLine.amountremaining,"Balance":tmpLine.balance}>
<#assign y = y+[dataLine]>
</#list>
<#list y?sort_by('displayDate') as line><tr>
<td colspan="4">${line.displayDate}</td>
<td colspan="10">${line.Description}</td>
<td colspan="4"><span style="background-color: rgb(255, 255, 255);">${line.CheckNum}</span></td>
<td align="right" colspan="5">${line.InvoiceAmount}</td>
<td align="right" colspan="4">${line.Credit}</td>
<td align="right" colspan="4"><span style="text-align: -webkit-right; background-color: rgb(255, 255, 255);">${line.Remaining}</span></td>
<td align="right" colspan="5">${line.Balance}</td>
</tr>
</#list></table>
来源:https://stackoverflow.com/questions/14337681/freemarker-template-sort-by-one-or-other