Featured post

Docker setup for Liferay 7 with MySQL

Thursday, 26 January 2012

Column base sorting in Liferay's Search Container with BeanComparator.


Sometimes we have such requirement that we need sorting on our column basis,
   like by clicking on column it gives us sorted data in ascending/descending form.
You can define which column you want to sort and by which order.

By using BeanComparator, it gives you  flexibility not to create your own comparators.

1) Declare String variables to define order by column and order by type

String orderByCol = ParamUtil.getString(request, "orderByCol");
String orderByType = ParamUtil.getString(request, "orderByType");
if(orderByCol==null || orderByCol.equals(StringPool.BLANK)) {
request.setAttribute("orderByCol",orderByCol);
}
if(orderByType==null || orderByType.equals(StringPool.BLANK)) {
orderByType="asc";            //Default value for order by type
request.setAttribute("orderByType",orderByType);
}

// In your liferay Search Container pass this param orderByCol and orderByType

<liferay-ui:search-container  orderByCol="<%=orderByCol %>" orderByType="<%=orderByType %>" delta="10">

2) BeanComparator beanComparator=new BeanComparator(orderByCol);

if(orderByType.equals("desc"))
{
Collections.sort(list_Details,Collections.reverseOrder(beanComparator));
orderByType="asc";
}
else
{
Collections.sort(list_Details);
orderByType="desc";
}

//You have to make a copy of this list, otherwise it will throw an exception.

List<Test> copy_List_Details = list_Details;

// Assign it to results object of Search Container

results = ListUtil.subList(list_Details, searchContainer.getStart(), searchContainer.getEnd());

3)
                  //In your column's of Search Container make orderable true and pass orderableProperty,
                  //you can sort by as many colums as you want.


     <liferay-ui:search-container-column-text
     name="Start Date"
       value="<%=sDate_dis %>"
       orderable="<%=true %>"
         orderableProperty="regiStartDate"
     />


You are done with sorting, Try and Enjoy ....:))

2 comments:

Gadget

This content isn't available over encrypted connections yet.