Liferay Search Container Example

Liferay Search Container Example is quick start  to create search container in liferay.  liferay-ui tag lib provides the search container tag to display the entities in grid view. Search Container is tightly integrated with Service builder entities, so it can not be used with other business layer provider.

Search Container:

The below is basic structure of search-container tags:

<liferay-ui:search-container>
     <liferay-ui:search-container-results results="resultList" %>" >
     <% 
     //Optional
      searchContainer.setTotal(total);
      searchContainer.setResults( LocalServiceUtil.getAllRecords());
    %>
 </liferay-ui:search-container-results>
 
 <liferay-ui:search-container-row className="Entity Name" modelVar="modalLocalVariable" keyProperty="Entity Primary Key" > 
      <liferay-ui:search-container-column-text property="Database Column Name" name="Display Name" href=""/> 
      <!-- Here columns goes -->
  </liferay-ui:search-container-row>
  <liferay-ui:search-iterator />
</liferay-ui:search-container>

In scriptlets, use searchContainer object to setTotal and results.

As earlier said, search container requires service.xml file, so we will create a sample service.xml. Let’s consider leave application requirement and service.xml is defined as below:

  • Service.xml
    1. <?xml version="1.0"?>
      <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" 
      "http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd">
      <service-builder package-path="org.javasavvy.leave">
       <namespace>js</namespace>
       <entity local-service="true" name="Leave" remote-service="false" uuid="true">
       <!-- PK fields -->
       <column name="leaveId" primary="true" type="long" />
       <!-- Group instance -->
       <column name="groupId" type="long" />
       <column name="companyId" type="long" />
       <column name="userId" type="long" />
       <column name="userName" type="String" />
       <column name="createDate" type="Date" />
       <column name="modifiedDate" type="Date" />
      
       <column name="leaveName" type="String" />
       <column name="startDate" type="Date" />
       <column name="endDate" type="Date" />
       <order by="asc">
       <order-column name="createDate" />
       </order>
       <!-- Finder methods -->
      
       <finder name="userId" return-type="Collection">
       <finder-column name="userId" />
       </finder>
       <!-- References -->
       <reference entity="AssetEntry" package-path="com.liferay.portlet.asset" />
       <reference entity="AssetTag" package-path="com.liferay.portlet.asset" />
       </entity>
      </service-builder>
  • In JSP page, we will add the search container tags to  display leave objects in table grid view:
    • iteratorURL is mandatory field and set that to current JSP file only.
    • search-container-row requires className,ModalVar and keyProperty to access the leave object from the list.
    • <%
       PortletURL leaveItrUrl = renderResponse.createRenderURL();
      leaveItrUrl.setParameter("mvcPath", "/leave/view.jsp");
      
      %>
      <liferay-ui:search-container emptyResultsMessage="no-leaves-found" iteratorURL="<%=leaveItrUrl %>" >
       <liferay-ui:search-container-results results="<%= LeaveLocalServiceUtil.getLeaves(searchContainer.getStart(), searchContainer.getEnd()) %>" >
       </liferay-ui:search-container-results>
       
       <liferay-ui:search-container-row className="org.javasavvy.leave.model.Leave" modelVar="leave" keyProperty="leaveId" > 
       <portlet:renderURL var="rowURL"> 
       <portlet:param name="backURL" value="<%=currentURL %>" /> 
       <portlet:param name="leaveId" value="${leave.leaveId}" /> 
       <portlet:param name="mvcPath" value="/leave/leaveInfo.jsp"/>
       </portlet:renderURL>
       <liferay-ui:search-container-column-user userId="${leave.userId}" showDetails="false" name="User"></liferay-ui:search-container-column-user>
       <liferay-ui:search-container-column-text property="userName" name="User Name" href="${rowURL}"/> 
       <liferay-ui:search-container-column-text property="leaveName" name="Leave Name" href="${rowURL}"/> 
       <liferay-ui:search-container-column-date property="startDate" name="Start Date"></liferay-ui:search-container-column-date>
       <liferay-ui:search-container-column-date property="endDate" name="End Date"></liferay-ui:search-container-column-date>
       </liferay-ui:search-container-row>
       <liferay-ui:search-iterator />
      </liferay-ui:search-container>
  • Finally Leave objects in Search Grid are looks like:Liferay 7 CRUD Tutorial

Hope this helps!!!

3 thoughts on “Liferay Search Container Example”
  1. How to customize search container results. In the example you are returning as entity. Lieu, can I return objects as I wish and set to column text. Any clue?

  2. how to do this if you click a user it showing particular user details, how to do that without using service builder

Comments are closed.