Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Dynamic List OrderBy in VM Template?
We've been LR users for a long time and we're trying take advantage of the DDL feature in 6.1. However, we are having a bit of a struggle including the correct "orderby" language in our list template. After having to bump the server a few times to recognize faulty template changes, we're reaching out for an assist. Thanks, in advance.
Simple list template, based on https://gist.github.com/1642366:
#set ($ddlRecordsUtil = $serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService"))
#set ($recordSetId = $getterUtil.getLong($reserved_record_set_id.data))
#set ($records = ${ddlRecordsUtil.getRecords($recordSetId)})
<table class="pp-table"><tr><th>NAME</th><th>FROM YEAR</th><th>TO YEAR</th></tr>
#foreach ($record in $records)
#set ($name = $record.getField("text5549").getValue())
#set ($from-year = $record.getField("text4701").getValue())
#set ($to-year = $record.getField("text5473").getValue())
<tr>
<td><strong>$name</strong></td>
<td>$from-year</td>
<td>$to-year</td>
</tr>
#end
</table>
Simple list template, based on https://gist.github.com/1642366:
#set ($ddlRecordsUtil = $serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService"))
#set ($recordSetId = $getterUtil.getLong($reserved_record_set_id.data))
#set ($records = ${ddlRecordsUtil.getRecords($recordSetId)})
<table class="pp-table"><tr><th>NAME</th><th>FROM YEAR</th><th>TO YEAR</th></tr>
#foreach ($record in $records)
#set ($name = $record.getField("text5549").getValue())
#set ($from-year = $record.getField("text4701").getValue())
#set ($to-year = $record.getField("text5473").getValue())
<tr>
<td><strong>$name</strong></td>
<td>$from-year</td>
<td>$to-year</td>
</tr>
#end
</table>
Bump...
Any leads on how to "orderby" my results in a dynamic list template?
Thanks!
Any leads on how to "orderby" my results in a dynamic list template?
Thanks!
Well, if you call getRecords() w/ the record set id, the status value, -1 for the start/end, and the order by comparator you can control the sorting...
Alternatively I'd probably create a class/method to proxy the call to getRecords() passing in the right stuff. Would be easier to use from the VM template...
Alternatively I'd probably create a class/method to proxy the call to getRecords() passing in the right stuff. Would be easier to use from the VM template...
David H Nebinger:
Well, if you call getRecords() w/ the record set id, the status value, -1 for the start/end, and the order by comparator you can control the sorting...
Alternatively I'd probably create a class/method to proxy the call to getRecords() passing in the right stuff. Would be easier to use from the VM template...
I'm probably blind to the simplicity of this. However, here is where we plugged in the getRecords details:
#set ($ddlRecordsUtil = $serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService"))
#set ($recordSetId = $getterUtil.getLong($reserved_record_set_id.data))
#set ($records = ${ddlRecordsUtil.getRecords($recordSetId,-1,-1,50,ORDER_BY_text4701_DESC)}) < - - HERE
<table class="pp-table"><tr><th>NAME</th><th>FROM YEAR</th><th>TO YEAR</th></tr>
#foreach ($record in $records)
#set ($name = $record.getField("text5549").getValue())
#set ($from-year = $record.getField("text4701").getValue())
#set ($to-year = $record.getField("text5473").getValue())
<tr>
<td><strong>$name</strong></td>
<td>$from-year</td>
<td>$to-year</td>
</tr>
#end
</table>
Of course, the ORDER_BY isn't working. Probably because we haven't defined the field yet. Everything else in the line works fine and makes sense but we must be using the DDL orderby nomenclature incorrectly.
Order by in this instance is not a string, it's an implementation of a class, the abstract class OrderByComparator.
I am also struggling with this. Can you provide a simple WORKING example?
In portlets :
must be converted to template code.
records = DDLRecordLocalServiceUtil.getRecords(record_set_id, -1, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
OrderByComparatorFactoryUtil.create("DDLRecord", "MODIFIEDDATE", true));
must be converted to template code.
For example, if you want to sort records by modified date (freemarker code):
<#assign OrderByComparatorFactoryUtil = staticUtil["com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil"]>
<#assign orderby = OrderByComparatorFactoryUtil.create("DDLRecord", ["MODIFIEDDATE", false])>
<#assign DDLRecordLocalService = serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService")>
<#assign records = DDLRecordLocalService.getRecords(reserved_record_set_id, -1, -1, -1, orderby)>
<#if records?has_content>
<#list records as cur_record>
#${cur_record.recordId}<br>
<!--#list-->
<!--#if-->
I am using Liferay 6.2 and this code does not work for me...
Do you have any idea of the problem?
Thanks in advance.
______________________________________
________ This code generates the following error __________
Error on line 16, column 1 in 10154#10194#24735
DDLRecordLocalService.getRecords(reserved_record_set_id, -1, -1, -1, orderby) is undefined.
It cannot be assigned to records
___________________________________________
This works when I replace :
by
But then, the possibility to order the list is lost...
Do you have any idea of the problem?
Thanks in advance.
______________________________________
<#assign OrderByComparatorFactoryUtil = staticUtil["com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil"]>
<#assign orderby = OrderByComparatorFactoryUtil.create("DDLRecord", ["attribut_n_1", false])>
<#assign DDLRecordLocalService = serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService")>
<#assign records = DDLRecordLocalService.getRecords(reserved_record_set_id, -1, -1, -1, orderby)>
________ This code generates the following error __________
Error on line 16, column 1 in 10154#10194#24735
DDLRecordLocalService.getRecords(reserved_record_set_id, -1, -1, -1, orderby) is undefined.
It cannot be assigned to records
___________________________________________
This works when I replace :
<#assign records = DDLRecordLocalService.getRecords(reserved_record_set_id, -1, -1, -1, orderby)>
by
<#assign records = DDLRecordLocalService.getRecords(reserved_record_set_id)>
But then, the possibility to order the list is lost...
I'm getting the same issue. Did you find the solution for this?
Um, reserved_record_set_id is null.
Just tried it. If you create a new ddl and use its ID, it works:
<#assign records = DDLRecordLocalService.getRecords(20030, -1, -1, -1, orderby )>
In my case, the ID is 20030, I just looked it up in control panel.
Just tried it. If you create a new ddl and use its ID, it works:
<#assign records = DDLRecordLocalService.getRecords(20030, -1, -1, -1, orderby )>
In my case, the ID is 20030, I just looked it up in control panel.
You could use Velocity SortTool, Liferay is ready to use in its velocity templates.
Take a look here
http://www.liferay.com/community/wiki/-/wiki/Main/CMS+Template+%28Velocity%29#section-CMS+Template+(Velocity)
Take a look here
http://www.liferay.com/community/wiki/-/wiki/Main/CMS+Template+%28Velocity%29#section-CMS+Template+(Velocity)
Here is the solution I found to sort DDL in VM template :
The trick is to use the Sorter objet + a temp array.
Hope this help somebody ...
#set ($ddlRecordService = $serviceLocator.findService('com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService'))
#set ($records = $ddlRecordService.getRecords($reserved_record_set_id))
#if(!$records.isEmpty())
#set ($sortRecords= [])
#foreach ($cur_rec in $records )
#set ($tmp = $sortRecords.add( {"Order" : $cur_rec.getFieldValue("Order", $locale) , "obj" : $cur_rec }))
#end
#foreach ($cur_obj in $sortTool.sort( $sortRecords , "Order"))
#set ($cur_record = $cur_obj.obj)
$cur_record.getFieldValue("firstName", $locale)
...
#end
#end
The trick is to use the Sorter objet + a temp array.
Hope this help somebody ...
Any news in Freemarker?
Ordeby Comparator seems to work only on ddlRecord fileds (creation date , modified date) not on custom filed of the structure..
Ordeby Comparator seems to work only on ddlRecord fileds (creation date , modified date) not on custom filed of the structure..
Achmed Tyrannus Albab, modified 9 Years ago.
Regular Member
Posts: 158
Join Date: 3/5/10
Recent Posts
Alessandro Lachina:
Any news in Freemarker?
Ordeby Comparator seems to work only on ddlRecord fileds (creation date , modified date) not on custom filed of the structure..
Hi,
Did you manage this in FreeMarker?
Achmed Tyrannus Albab, modified 6 Years ago.
Regular Member
Posts: 158
Join Date: 3/5/10
Recent Posts
In case anyone is trying to do this in Liferay 7.1 (since I have no idea how to sort by DDL field in Free Marker) :
Hope that helps someone.
#set ($records = $ddlDisplayTemplateHelper.getRecords($reserved_record_set_id))
#if(!$records.isEmpty())
#set ($sortRecords= [])
#foreach ($cur_rec in $records )
#set ($Date_DateObj = $ddlDisplayTemplateHelper.renderRecordFieldValue($cur_rec.getDDMFormFieldValues("DateField").get(0), $locale))
#set ($tmp = $sortRecords.add( {"Order" : $Date_DateObj , "obj" : $cur_rec }))
#end
#foreach ($cur_obj in $sortTool.sort($sortRecords , "Order:desc"))
#set ($cur_rec = $cur_obj.obj)
#set ($theDate = $ddlDisplayTemplateHelper.renderRecordFieldValue($cur_rec.getDDMFormFieldValues("DateField").get(0), $locale))
$theDate<br>
#end
#end
Hope that helps someone.
Following entry in stackoverflow works like a charm in 7.1 in Freemarker (at least the answer that iterates twice): https://stackoverflow.com/questions/14337681/freemarker-template-sort-by-one-or-other