RE: Dynamic List OrderBy in VM Template?

James Burris, modified 13 Years ago. New Member Posts: 4 Join Date: 3/16/12 Recent Posts
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>
James Burris, modified 13 Years ago. New Member Posts: 4 Join Date: 3/16/12 Recent Posts
Bump...

Any leads on how to "orderby" my results in a dynamic list template?

Thanks!
thumbnail
David H Nebinger, modified 13 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
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...
James Burris, modified 13 Years ago. New Member Posts: 4 Join Date: 3/16/12 Recent Posts
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.
thumbnail
David H Nebinger, modified 13 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Order by in this instance is not a string, it's an implementation of a class, the abstract class OrderByComparator.
thumbnail
John Nowlin, modified 11 Years ago. New Member Posts: 8 Join Date: 7/5/13 Recent Posts
I am also struggling with this. Can you provide a simple WORKING example?
nadi h, modified 11 Years ago. New Member Posts: 21 Join Date: 7/23/13 Recent Posts
In portlets :



records = DDLRecordLocalServiceUtil.getRecords(record_set_id, -1, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
    OrderByComparatorFactoryUtil.create("DDLRecord", "MODIFIEDDATE", true));



must be converted to template code.
nadi h, modified 11 Years ago. New Member Posts: 21 Join Date: 7/23/13 Recent Posts
For example, if you want to sort records by modified date (freemarker code):


&lt;#assign OrderByComparatorFactoryUtil = staticUtil["com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil"]&gt;
&lt;#assign orderby = OrderByComparatorFactoryUtil.create("DDLRecord", ["MODIFIEDDATE", false])&gt; 

&lt;#assign DDLRecordLocalService = serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService")&gt;
&lt;#assign records = DDLRecordLocalService.getRecords(reserved_record_set_id, -1, -1, -1, orderby)&gt;

&lt;#if records?has_content&gt;
	&lt;#list records as cur_record&gt;
		#${cur_record.recordId}<br>
	<!--#list-->
<!--#if-->
Stéphane Faure, modified 11 Years ago. New Member Posts: 14 Join Date: 4/22/14 Recent Posts
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.
______________________________________

<#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...
Justin Stanczak, modified 11 Years ago. New Member Posts: 9 Join Date: 12/6/13 Recent Posts
I'm getting the same issue. Did you find the solution for this?
thumbnail
Christoph Rabel, modified 10 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts
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.
thumbnail
Anderson Perrelli, modified 10 Years ago. Junior Member Posts: 81 Join Date: 6/12/13 Recent Posts
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)
thumbnail
Nicolas MEURET, modified 9 Years ago. New Member Posts: 3 Join Date: 2/8/13 Recent Posts
Here is the solution I found to sort DDL in VM template :

#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 ...
thumbnail
Alessandro Lachina, modified 9 Years ago. New Member Posts: 24 Join Date: 6/12/13 Recent Posts
Any news in Freemarker?
Ordeby Comparator seems to work only on ddlRecord fileds (creation date , modified date) not on custom filed of the structure..
thumbnail
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?
thumbnail
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) :
​​​​​​​
#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.
thumbnail
Manuel Manhart, modified 6 Years ago. New Member Posts: 9 Join Date: 6/28/13 Recent Posts
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