« Back

Dynamic Data Lists III

Company Blogs January 18, 2012 By Marcellus Tavares Staff

Hey all, this is the third post about Dynamic Data Lists ant it's focused on how to write the Lists Templates mentioned on the previous post.

As already said, List Templates provides all the flexibility needed to build your own UI. Within the templates context you will have access to all records and fields of these records for the configured list.
 
We're going to demonstrate this by creating a simple user registation list and then we'll diplay it diffrently using a velocity template.
 
First thing to do is to instantiate the Dynamic Data List Display portlet and create a new list. The definition of this list will have two fields: name (text field) and avatar (file upload field)  (Figure 1).
 
Figure 1 - List creation
 
After that we're ready to populate the list. Figure 2 shows the records added.
 
Figure 2 - User Registration records
 
Finally, in order to create templates click on the "Add List Template" icon (red mark in Figure 2). Then a pop-up will show up and you can either upload the template file or write one using the code editor. Here's the template used for this example:

And here's the end result:

Threaded Replies Author Date
Can you do a blog post how you can expose a... Bradley Wood January 19, 2012 2:18 PM
wow its working in Liferay 6.1 GA very fine ..... Navin Agarwal January 19, 2012 10:42 PM
Navin, I created a new dynamic data list and... Harish Kumar January 22, 2012 10:00 PM
Hi Harish, this issue will be fixed in... Marcellus Tavares January 23, 2012 10:07 AM
thanks Marcellus Harish Kumar January 23, 2012 8:53 PM
I built ddm, ddl and template as Marcellus... Nicola Grippaldi February 8, 2012 3:08 AM
Hey Nicola, what version are you running? Marcellus Tavares February 14, 2012 9:52 AM
Hi, does anynone know how populate a list of... Damien Pruvost February 16, 2012 1:49 AM
Hey Damien, you can populate your records... Marcellus Tavares February 16, 2012 5:16 AM
Hi Marcellus sorry for late reply I've just... Nicola Grippaldi February 20, 2012 6:51 AM
Hi Marcellus, It's a nice blog, learned a lot.... Vipin Bardia March 19, 2012 11:45 AM
#set ($portalBeanLocator =... Dmitry Far March 22, 2012 2:10 AM
Thanks Marcellus. Great work! I was trying to... Pius Onobhayedo March 24, 2012 5:12 AM
Hi Marcellus, Could you show me how to link to... Danny Jiang May 15, 2012 1:19 PM
hi guys, i created... iqbal ahmed May 15, 2012 10:49 PM
I've managed it to get it to work with the... WM MTV June 5, 2012 12:41 AM
If is use the same velocity code, how could I... long zhao June 8, 2012 7:56 AM
I know it was asked above (Danny Jiang) - but... Randy Parsons June 14, 2012 11:27 AM
Thanks for interesting post, it's very useful.... Dmitry Sergeev June 26, 2012 7:49 AM
Can anyone please explain me what is the... João Cartaxo August 16, 2012 7:18 AM
Hi, Try this solution:... Stijn Haus October 23, 2012 6:23 AM
Stijn, Thanks for posting this. It was very... Ben Chapman February 1, 2013 3:53 AM
Hi Ben, I actually made 2 posts: ... Stijn Haus February 1, 2013 4:02 AM
Nice one Marcellus Pravin Pawar March 7, 2013 3:29 AM
Hi Marcellus In the following line: #set... Paulo Fernandes January 30, 2014 9:31 AM
is it possible to filter records (using... nadi h February 8, 2014 11:30 AM
Hi Marcelus! Nice explanation :) It is... Ivan Josa March 20, 2014 5:39 AM
:( why ? using code in vesion 6.2 CE not... Tuan vu manh October 9, 2014 2:24 AM
On LR6.2GA4 #set ($DDLRecordLocalService =... Cyber Katze May 8, 2015 5:51 AM

Bradley Wood
Can you do a blog post how you can expose a Dynamic DataList from webcontent velocity template?
Posted on 1/19/12 2:18 PM.
wow its working in Liferay 6.1 GA very fine .. Marcellus give some information with velocity template ...
Posted on 1/19/12 10:42 PM.
Navin,

I created a new dynamic data list and one detail template and list template also.
When I go to configration section in the portlet to select newly created detail template and list template, I found that list template is not listed there to choose from whereas detail template is there and upon selection its also working fine.

any suggestions would be helpful..
Posted on 1/22/12 10:00 PM in reply to Navin Agarwal.
Hi Harish, this issue will be fixed in LPS-25059. Thanks.
Posted on 1/23/12 10:07 AM in reply to Harish Kumar.
thanks Marcellus
Posted on 1/23/12 8:53 PM in reply to Marcellus Tavares.
I built ddm, ddl and template as Marcellus suggested and I called template1 the list template.
I added a Dynamic Data List Display portlet in My public pages and all works fine.

I created from DDL display portlet a new template just like template1 but without the raw
<img height="100" src='/documents/ddm/${className}/${classPK}/${fileName}' width="100" />
to show the list without images, and I saved it as template2.
I selected template2 but the behaviour of portlet doesn't change.
I can select default template with no problem but if I select template1 or template2 I get the same data visualization with images.

HORRIBLE WORKAROUND
1. In DDL portlet I selected template2 which didn't work
2. shutdown tomcat
3. startup tomcat
DDL portlet now show correct data list without images
If I select template1 or template2 I got the same data visualization without images

The same behaviour if, rather than use another template, remove the images code row from template1.

What have I to do?
Posted on 2/8/12 3:08 AM in reply to Harish Kumar.
Hey Nicola, what version are you running?
Posted on 2/14/12 9:52 AM in reply to Nicola Grippaldi.
Hi,
does anynone know how populate a list of records from an SQL database.
Is it possible to reference methods defined in a Java class thanks to Velocity template ?
Thanks for ur help.
Posted on 2/16/12 1:49 AM in reply to Marcellus Tavares.
Hey Damien, you can populate your records through the DDL API. DDLRecordSetService allows you to create lists and the DDLRecordService is to be used to create the list records.

Yes. It is. You should only set the classes you want to be visible within the velocity context. Take a look VelocityVariablesImpl.java
Posted on 2/16/12 5:16 AM in reply to Damien Pruvost.
Hi Marcellus
sorry for late reply
I've just updated trunk and the problem persist

Nicola
Posted on 2/20/12 6:51 AM in reply to Marcellus Tavares.
Hi Marcellus,

It's a nice blog, learned a lot.

But when i tried to create list template.

<b>It gives this error -</b>


An error occurred while processing the template.

Invocation of method 'createJSONObject' in class com.liferay.portal.json.JSONFactoryImpl threw exception com.liferay.portal.kernel.json.JSONException: java.lang.NullPointerException at 20null[line 25, column 48]

1## Get the reference of DDL Record Service via serviceLocator, which is automatically set in the context

2

3#set ($ddlRecordService = $serviceLocator.findService('com.liferay.portlet.dynamicdatalists.service.DDLRec­ordLocalService'))

4

5## $reserved_record_set_id is also an object set in the context and it has the value of the list id configured for the portlet instance

6

7#set ($recordSetId = $getterUtil.getInteger($reserved_record_set_id.data, 0))

8

9## call to the retrieve all records of the list

10

11#set ($records = $ddlRecordService.getRecords($recordSetId))

12

13#foreach($record in $records)

14 ## each record has a collection of fields (name and avatar in our case)

15

16 #set ($fields = $record.getFields())

17

18 #set ($nameField = $fields.get("name"))

19

20 #set ($avatarField = $fields.get("avatar"))

21

22 ## the avatar is a file upload field and its value holds a JSON object

23 ## jsonFactoryUtil help us to convert string value into a JSON object

24

25 #set ($avatarJSONObject = $jsonFactoryUtil.createJSONObject($avatarField.value))

26

27 #set ($fileName = $avatarField.getName())

28 #set ($className = $avatarJSONObject.getString("className"))

29 #set ($classPK = $avatarJSONObject.getString("classPK"))

30

31 <div style="padding-top: 10px;">

32 <img height="100" src='/documents/ddm/${className}/${classPK}/${fileName}' width="100" />

33

34 <span style="padding-left: 5px;">

35 $nameField.getValue()

36 <span>

37 </div>

38#end

Please let me know, if there is any work around.
Thanks,
Vipin Bardia
Posted on 3/19/12 11:45 AM in reply to Nicola Grippaldi.
#set ($portalBeanLocator = $portal.getClass().forName('com.liferay.portal.kernel.bean.PortalBeanLocatorUtil­'))
#set ($jsonFactoryUtil = $portalBeanLocator.locate('com.liferay.portal.kernel.json.JSONFactoryUtil'))
Posted on 3/22/12 2:10 AM in reply to Vipin Bardia.
Thanks Marcellus. Great work!

I was trying to find out how to combine the use of List template as you explained above with drill down to detail view/edit. In other words, I would like to display content in a custom manner and at the same time be able to perform actions on the displayed records. Secondly, is there a way to make some fields non-editable?

Kindly advice? Thank you in advance.
Posted on 3/24/12 5:12 AM in reply to Dmitry Far.
Hi Marcellus,

Could you show me how to link to record details view in your list template?

Thanks

Danny
Posted on 5/15/12 1:19 PM.
hi guys,
i created dynamic data list exactly as shown image is not displaying
other data is displaying how to resolve it plz help i am new to liferay
Posted on 5/15/12 10:49 PM.
I've managed it to get it to work with the following code. It seems that with at least 6.1 the JSON names are changed. I've printed out the value of the imagefield and the fields are recordId, name, path.

#set ($titleImageField = $record.getField("titleimage"))
#set ($titleImageValue = $titleImageField.getValue())

#set ($imageJSONObject = $jsonFactoryUtil.createJSONObject($titleImageField.value))

#set ($imageFieldName = $titleImageField.getName())
#set ($imageRecordId = $imageJSONObject.getString("recordId"))

<img src='/documents/ddl/${imageRecordId}/${imageFieldName}' width="100" />

Very annoying is a bug, which requires a restart after changing the template, else you will see the result of the previous template definition. So make sure to have a working template before deploying it into a production environment, to minimize downtime of your production server.
Posted on 6/5/12 12:41 AM in reply to iqbal ahmed.
If is use the same velocity code, how could I show a same list on another page?
I need to show the same information but in different page.

And how to add the default row of "Actions"
Thanks
Posted on 6/8/12 7:56 AM in reply to RH HannMTV.
I know it was asked above (Danny Jiang) - but no-one answered.

How do you link (create the link in the VM of the List Template) a record on a List Template to open the record using a specific Detail Template - very similar to Asset Publisher does with Table option?

Thanks.
Posted on 6/14/12 11:27 AM in reply to long zhao.
Thanks for interesting post, it's very useful.
Could someone please tell me where is the DDL data located? I'm talking about uploaded files and "documents and media" objects.
Is it in DB or somewhere in filesystem?
And yes, how can I render standard "actions" button in my custom list?
Posted on 6/26/12 7:49 AM in reply to Randy Parsons.
Can anyone please explain me what is the advantage of this List Templates?

I ask this because on one hand we can customize the presentation, but on the other hand, the capability of performing actions over the records is lost...It must be....otherwise there would be an answer not only to me in the forum (http://www.liferay.com/community/forums/-/message_boards/message/15599617) but also to the other three people that have asked this same question in this very same comment list.
Posted on 8/16/12 7:18 AM.
Hi,

Try this solution: http://webapplicationdeveloper.blogspot.be/2012/10/liferay-dynamic-data-lists-re­cord.html for displaying the record actions.
Posted on 10/23/12 6:23 AM in reply to João Cartaxo.
Stijn,

Thanks for posting this. It was very useful - but it looks like the URL to your post may have changed: http://webapplicationdeveloper.blogspot.be/2012/10/liferay-dynamic-data-list-det­ail.html was where I found it. Thanks!
Posted on 2/1/13 3:53 AM in reply to Stijn Haus.
Hi Ben,

I actually made 2 posts:

http://webapplicationdeveloper.blogspot.be/2012/10/liferay-dynamic-data-li­st-detail.html for going to a detail page

and

http://webapplicationdeveloper.blogspot.be/2012/10/liferay-dynamic-data-lists-re­cord.html to dosplay the actions on a record.
Posted on 2/1/13 4:02 AM in reply to Ben Chapman.
Nice one Marcellus
Posted on 3/7/13 3:29 AM.
Hi Marcellus

In the following line:
#set ($recordSetId = $getterUtil.getInteger($reserved_record_set_id.data, 0))

I removed the ".data" to work in Liferay 6.2.10 EE
Nice article!
Posted on 1/30/14 9:31 AM.
is it possible to filter records (using DDLRecordLocalService.getRecords or something) ?
Posted on 2/8/14 11:30 AM.
Hi Marcelus!
Nice explanation emoticon

It is possible to get the portlet id where the dynamic list is shown with velocity?

Many thanks for your work and explanations!
Posted on 3/20/14 5:39 AM.
emoticon why ? using code in vesion 6.2 CE not working.

help me
Posted on 10/9/14 2:24 AM.
On LR6.2GA4

#set ($DDLRecordLocalService = $serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRec­ordLocalService"))

#set ($recordSetId = $getterUtil.getLong($reserved_record_set_id))
#set ($records = ${DDLRecordLocalService.getRecords($recordSetId)})

#if (!$records.isEmpty())
#foreach ($cur_record in $records)
$cur_record.getFieldValue("myFieldValue", $locale)
#end
#end
Posted on 5/8/15 5:51 AM in reply to Tuan vu manh.