Featured post

Docker setup for Liferay 7 with MySQL

Showing posts with label sorting search container. Show all posts
Showing posts with label sorting search container. Show all posts

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)) {
if(orderByType==null || orderByType.equals(StringPool.BLANK)) {
orderByType="asc";            //Default value for order by type

// 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);


//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());

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

     name="Start Date"
       value="<%=sDate_dis %>"
       orderable="<%=true %>"

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