Liferay - Sort Search Container Result

4 comments
To sort liferay search container result follow below steps.

1) Create liferay search container in your JSP.

<liferay-ui:search-container hover="false" searchContainer="${mySearchContainer}">
<liferay-ui:search-container-results
results="${mySearchContainer.results}"
total="${mySearchContainer.total}"/>
<liferay-ui:search-container-row
className="com.liferay.portlet.model.MyCustomBean"
keyProperty="Id" modelVar="myObj">
<liferay-ui:search-container-column-text name="no" property="arcNo" cssClass="width10pt"/>
<liferay-ui:search-container-column-text name="item.name" property="itemName" orderableProperty="itemName" orderable="true" />
<liferay-ui:search-container-column-text name="req.status" property="status" orderableProperty="status" orderable="true" cssClass="width10pt"/>
<liferay-ui:search-container-column-text name="delivery.type" property="deliveryType" orderableProperty="deliveryType" orderable="true" cssClass="width15pt"/>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>



2) Create search container in your controller and set result in search container object.

ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
String orderByCol = ParamUtil.getString(renderRequest, "orderByCol");
String orderByType = ParamUtil.getString(renderRequest, "orderByType");
String curValue = ParamUtil.getString(renderRequest, "curr");
SearchContainer<MyCustomBean> searchContainer = null;
//Create Portlet URL and set required param for pagination
PortletURL portletURL = PortletURLFactoryUtil.create(portletRequest, PORTLET_ID, themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
searchContainer = new SearchContainer<myCustomBean>(renderRequest, null, null, CUR_CONTENT, DELTA, portletURL, null,StringPool.BLANK);
searchContainer.setEmptyResultsMessage(LanguageUtil.get(themeDisplay.getLocale(), "empty-result-message"));
searchContainer.setIteratorURL(portletURL);
searchContainer.setOrderByCol(orderByCol);
searchContainer.setOrderByType(orderByType);
searchContainer.setResults(getSortedList(int start, int end, long companyId, String orderByType, String orderByColumn));
searchContainer.setTotal(MyLocalServiceUtil.getCustomBeanCount());
3) Get sorted results using dynamic query.
public static List<MyCustomBean> getSortedList(int start, int end, long companyId, String orderByType, String orderByColumn)
{
List<MyCustomBean> myList = new ArrayList<MyCustomBean>();
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyCustomBean.class);
dynamicQuery.add(PropertyFactoryUtil.forName("companyId").eq(companyId));
dynamicQuery.setLimit(start, end);
if (orderByType.equalsIgnoreCase("asc"))
{
dynamicQuery.addOrder(OrderFactoryUtil.asc(orderByColumn));
} else
{
dynamicQuery.addOrder(OrderFactoryUtil.desc(orderByColumn));
}
try
{
myList= MyLocalServiceUtil.dynamicQuery(dynamicQuery);
} catch (SystemException e)
{
LOGGER.error(e.getMessage());
}
return myList;
}

Next PostNewer Post Previous PostOlder Post Home

4 comments:

  1. hi, nce work..can u sahre the code once pls.

    ReplyDelete
  2. I don't have sample portlet for this . But you can use the code snippet i posted in this blog.

    ReplyDelete
  3. I am having 4 drop down i want to change the search result according to the drop down values ,Like if i want to values from particular state than it should show the complete state results .
    I used DynamicQueryFactoryUtil and criterion for this .Please give me any suggestion for this ..

    ReplyDelete
    Replies
    1. What problem you are facing ? Read the user selected inputs and pass the same in dynamic query to filter records.

      Delete