Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Ritesh Gajera
Structure/Template sorting in velocity?
May 7, 2012 9:20 PM
Answer

Ritesh Gajera

Rank: Junior Member

Posts: 56

Join Date: February 21, 2012

Recent Posts

Hi All,
I would tried to implement sorting in velocity with help of three exp which I defined as below:

1 )
#foreach ($obj in $sortTool.sort($objects, "fieldnametosorton")
Display data from the sorted list here like $obj.fieldname
#end

2) since we were sorting on a child field we had to first build an array of maps and then sort on that since the sort method can't seem to access the child field:
#set ($array = [])
#foreach ($obj in $objects)
#set ($void = $array.add( { "field1" : $obj.getData(), "childfield" : $obj.childfield.getData() } ) )
#end

#foreach ($obj in $sortTool.sort($array, "childfield")
<p>This is a line sorted on $obj.childfield</p>
#end
3)
OtherWay

#set ($menuItems= [])
#set ($tmp = $menuItems.add({"title" : "item1", "url" : "http://www.google.com"}))
#set ($tmp = $menuItems.add({"title" : "item3", "url" : "http://www.google.com"}))
#set ($tmp = $menuItems.add({"title" : "item2", "url" : "http://www.google.com"}))
#set ($tmp = $menuItems.add({"title" : "item4", "url" : "http://www.google.com"}))

#foreach ($item in $sortTool.sort($menuItems, 'title:asc'))
<a href="$item.url">$item.title</a><br>
#end

#foreach ($item in $sortTool.sort($menuItems, 'title:desc'))
<a href="$item.url">$item.title</a><br>
#end

but I'm not able get an proper idea how to implement sorting in velocity from these above example. so
Plz provide any suggestion or share any thought here,

Thanks & Regards,
Ritesh Gajera
Amos Fong
RE: Structure/Template sorting in velocity?
May 8, 2012 10:26 PM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1844

Join Date: October 7, 2008

Recent Posts

Hi Ritesh,

What is your issue? It seems to work for me.
Hiral Ramavat
RE: Structure/Template sorting in velocity?
May 9, 2012 9:26 AM
Answer

Hiral Ramavat

Rank: Junior Member

Posts: 98

Join Date: February 13, 2012

Recent Posts

Are you trying to do sorting with above three ways as you mentioned? Because it seems fine with code.

Thanks,
Hiral
Ritesh Gajera
RE: Structure/Template sorting in velocity?
May 13, 2012 10:14 PM
Answer

Ritesh Gajera

Rank: Junior Member

Posts: 56

Join Date: February 21, 2012

Recent Posts

Hi Amos,

I have a requiement to have a field(sibling) named "order" in structure which is of string type.The whole structure(item) should be displayed in sorted order based on this "order" field.There is an inbuilt method "sorter.sort" in velocity but I am not able to understand its usage.

Has any one sorted the content using specific sibling field in template.vm?

Please provide me any pointers or guidance related to this issue.

Thanks & Regards,
Ritesh Gajera
Ritesh Gajera
RE: Structure/Template sorting in velocity?
May 13, 2012 10:18 PM
Answer

Ritesh Gajera

Rank: Junior Member

Posts: 56

Join Date: February 21, 2012

Recent Posts

Hi,

I tried two of them but its not working fine based on my specific requirement to related the issue.

Issue is:

I have a requiement to have a field(sibling) named "order" in structure which is of string type.The whole structure(item) should be displayed in sorted order based on this "order" field.There is an inbuilt method "sorter.sort" in velocity but I am not able to understand its usage.

Has any one sorted the content using specific sibling field in template.vm?

Please provide me any pointers or guidance related to this issue.

Thanks & Regards,
Ritesh Gajera
Amos Fong
RE: Structure/Template sorting in velocity?
May 14, 2012 1:35 AM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1844

Join Date: October 7, 2008

Recent Posts

Can you post your exact code that you are trying to run?

It also would help if you print out all the sibling objects directly to the screen so you can see their exact field values.
Ritesh Gajera
RE: Structure/Template sorting in velocity?
May 21, 2012 5:38 AM
Answer

Ritesh Gajera

Rank: Junior Member

Posts: 56

Join Date: February 21, 2012

Recent Posts

Hi Amos,
Finally, I have found the little bit way without used the sorting in my template file and i achieved the things which i needed to implement in its.

Here, Template code as below:

<!DOCTYPE html>

#parse ($init)

<html class="#language("lang.dir")" dir="#language("lang.dir")" lang="$w3c_language_id">

<head>

</head>

<body>
<div class="spotlight" >
<h2>Spotlight</h2>
<ul id="newsslider">
#set($item1 = 0)
#foreach($item in $Content.getSiblings())
#set($item1 = $item1+1)
#if($item1<=8)
#if($item.Display.getData()!="")

<li>
<a><img src="$item.Image.getData()" width="113" height="35" alt="no Image available"/></a>
<h3><a href="$item.Link.getData()">$item.Title.getData()</a></h3>
#if($item.Description.getData()!="")
$item.Description.getData()
#else
<p>&nbsp;</p>
#end
</li>
#end
#end
#end
</ul>
</div>
</body>

</html>

But, Is there any other ways to rearranges the spotlight content through sorting methods in template file?

Any pointer will be helpful.

Thanks & Regards,
Ritesh Gajera
Ritesh Gajera
RE: Structure/Template sorting in velocity?
May 28, 2012 10:57 PM
Answer

Ritesh Gajera

Rank: Junior Member

Posts: 56

Join Date: February 21, 2012

Recent Posts

Hiral Ramavat:
Are you trying to do sorting with above three ways as you mentioned? Because it seems fine with code.

Thanks,
Hiral



Hi Hiral,

Above of three codes works fine but with the help of these three code when I'm implementing my spotlight portlet then its not work. Because the content of spotlight is not rearrange it for the same code. Like as I also tried to used bubble sort in velocity but I'm not able to do that.
If You know how to rearrages / reorders the content in template file then please suggest me.

Thanks & Regards,
Ritesh Gajera