Orderable Search Container in Liferay

Look in to this blog post (sort-search-container-result) if you don't want to use comparator for sorting search container results.

Here is the example how you can make your search container orderable using comparators.
As you click on any of the cloumn of search container result will be sorted this one is the easy way of sorting . To make your search container you need to create custom comparator for each and every cloumn so when ever you click on that column apprpriate comparator for that cloumn will be called and result will be sorted according to that.




Below is the code that you need to copy and paste in your JSP this code will identify which user clicked on which column and on also identify the sorting type like either in dec or asc.


PortalPreferences portalPrefs = PortletPreferencesFactoryUtil.getPortalPreferences(request);
String sortByCol = ParamUtil.getString(request, "orderByCol");
String sortByType = ParamUtil.getString(request, "orderByType");
if (Validator.isNotNull(sortByCol ) && Validator.isNotNull(sortByType )) {
portalPrefs.setValue("NAME_SPACE", "sort-by-col", sortByCol);
portalPrefs.setValue("NAME_SPACE", "sort-by-type", sortByCol);
} else {
orderByCol = portalPrefs.getValue("NAME_SPACE", "sort-by-col", "First Name");
orderByType = portalPrefs.getValue("NAME_SPACE", "sort-by-type ", "asc");
}

Below is the code for search container.

<liferay-ui:search-container delta='20' emptyResultsMessage="No Form Submitted" orderByCol="<%= sortByCol %>" orderByType="<%= sortByType %>">
<liferay-ui:search-container-results>
<%
List<User> userList = UserLocalServiceUtil.getUser(-1,-1);
OrderByComparator orderByComparator =
CustomComparatorUtil.getUserOrderByComparator(sortByCol, sortByType);
Collections.sort(userList,orderByComparator);
results = ListUtil.subList(userList, searchContainer.getStart(),
searchContainer.getEnd());
if (userList.size()< total)
{total = userList.size();
}
pageContext.setAttribute("results", results);
pageContext.setAttribute("total", total);
%>
</liferay-ui:search-container-results>
<liferay-ui:search-container-row
className="com.liferay.portal.model"
keyProperty="Id"
modelVar="user">
<liferay-ui:search-container-column-text
name="First Name"
property="firstName"
orderable="<%= true %>"
/>
<liferay-ui:search-container-column-text
name="Last Name"
property="lastName"
orderable="<%= true %>"
orderableProperty="lastName"
/>
<liferay-ui:search-container-column-text
name="Screen Name"
property="screenName"
orderable="<%= true %>"
orderableProperty="ScreenName"
/>
<liferay-ui:search-container-column-text
name="Job Title"
property="JobTitle"
orderable="<%= true %>"
orderableProperty="JobTitle"
/>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>

By using above code you can create a search container. To make your search container orderable you need to set few of the properties as described in code.


If you look into the above code we are fetching first list of all user from database which will return list. So when ever user clicks on any of the column of search container name of that column is stored into the preferences (orderByCol and orderByType) so we can easily identify based on which column we need to do sorting. Once we get that column we need to call appropriate comprator for the same. So if you can look into the above code we are passing those sortByCol and sortType to the one utility class called CustomComparatorUtil so based on parameter passed method of this class will identify which comparator to call.

public class CustomComparatorUtil {
public static OrderByComparator getUserOrderByComparator(
String orderByCol, String orderByType) {
boolean orderByAsc = false;
if (orderByType.equals("asc")) {
orderByAsc = true;
}
OrderByComparator orderByComparator = null;
if (orderByCol.equalsIgnoreCase("First Name")) {
orderByComparator = new FirstNameComparator(orderByAsc);
}
else if (orderByCol.equalsIgnoreCase("Last Name")) {
orderByComparator = new LastNameComparator(orderByAsc);
}
else if (orderByCol.equalsIgnoreCase("Screen Name")) {
orderByComparator = new ScreenNameComparator(orderByAsc);
}
else if (orderByCol.equalsIgnoreCase("Job Title")) {
orderByComparator = new JobTitleComparator(orderByAsc);
}
return orderByComparator;
}
}

Below is custom comparator implemented for column


public class FirstNameComparator extends OrderByComparator {
public static String ORDER_BY_ASC = "status ASC";
public static String ORDER_BY_DESC = "status DESC";
public FirstNameComparator()
{
this(false);
}
public FirstNameComparator(boolean asc) {
_asc = asc;
}
public int compare(Object obj1, Object obj2) {
User instance1 = (User) obj1;
User instance2 = (User) obj2;
int value = instance1.getFirstName().toLowerCase().compareTo(instance2.getFirstName().toLowerCase());
if(_asc)
{
return value;
} else
{
return -value;
}
}
public String getOrderBy() {
if (_asc) {
return ORDER_BY_ASC;
}
else {
return ORDER_BY_DESC;
}
}
private boolean _asc;
}

Next PostNewer Post Previous PostOlder Post Home