« Back to Development

SearchContainer

Introduction #

Liferay's Search Container gives developers a utility to easily paginate their data.

Demystifying Liferay's Search Container #

Here's a basic example that will help get you started:

<liferay-ui:search-container delta="10" emptyResultsMessage="no-users-were-found">
	<liferay-ui:search-container-results
		results="<%= UserLocalServiceUtil.search(
			company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getActiveObj(), 
			userParams, searchContainer.getStart(), searchContainer.getEnd(),
 			searchContainer.getOrderByComparator()); %>"
		total="<%= UserLocalServiceUtil.searchCount(
			company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getActiveObj(), 
			userParams); %>"
	/>

	<liferay-ui:search-container-row
		className="com.liferay.portal.model.User"
		keyProperty="userId"
		modelVar="user"
	>
		<liferay-ui:search-container-column-text
			name="name"
			value="<%= user.getFullName() %>"
		/>

		<liferay-ui:search-container-column-text
			name="first-name"
			property="firstName"
		/>
	</liferay-ui:search-container-row>

	<liferay-ui:search-iterator />

</liferay-ui:search-container>

<liferay-ui:search-container delta="10" emptyResultsMessage="no-users-were-found">

This is the container. It performs a lot of set up work behind the scenes like instantiating the searchContainer object.

  • delta - The number of results per page
  • emptyResultsMessage - The message shown where there aren't results (it can be a key from your language.properties)
	<liferay-ui:search-container-results
		results="<%= UserLocalServiceUtil.search(
			company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getActiveObj(), 
			userParams, searchContainer.getStart(), searchContainer.getEnd(),
 			searchContainer.getOrderByComparator()); %>"
		total="<%= UserLocalServiceUtil.searchCount(
			company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getActiveObj(), 
			userParams); %>"
	/>
  • results - This is where you input the results. results should be of type List. The important part is to make sure that your method supports some way to search from a beginning index to an end index in order to provide a good performance pagination. Note how we use searchContainer.getStart() for the first index and searchContainer.getEnd() for the second index. As mentioned above, the searchContainer object is available because it has been instantiated already. Some other methods you can use:
    • searchContainer.getStart() - gets starting index of current results page.
    • searchContainer.getResultsEnd() - gets ending index of current results page or index of last result (i.e. will return 3 if delta is 5 but there are only 3 results).
    • searchContainer.getEnd() - gets last index of current results page regardless of size of actually results (i.e. will return 5 if delta is 5 even if there is only 3 results. Will throw out of bounds errors).
    • searchContainer.getCur() - gets number of current results page.
    • searchContainer.setTotal() - must be set so getResultsEnd() knows when to stop.
  • total - This is where you input the total number of items in your list:
<liferay-ui:search-container-row className="com.liferay.portal.model.User" keyProperty="userId" modelVar="user">
  • className - The type of Object in your List. In this case, we have a List of User objects.
  • keyProperty - Primary Key
  • modelVar - The name of the variable to represent your model. In this case the model is the User object.
<liferay-ui:search-container-column-text name="name" value="<%= user.getFullName() %>" />
  • <liferay-ui:search-container-column-text> - Text column
    • name - Name of the column
    • value - Value of the column
    • href - the text in this coulmn will be a link the this URL
    • orderable - allows the user to order the list of items by this column:
<liferay-ui:search-container-column-text name="first-name" property="firstName" />
  • property - This will automatically look in the User object for the "firstName" property. It's basically the same thing as user.getFirstName():
<liferay-ui:search-iterator />
  • <liferay-ui:search-iterator /> - This is what actually iterates through and displays the List

Hopefully, this gives you a jump start on how to use Liferay's SearchContainer in your own portlets.

0 Attachments
40897 Views
Average (9 Votes)
Comments

Showing 7 Comments

Gavin Meyers
1/22/10 4:17 AM

Hi ,
How do we get the RowChecker to work in the SearchContainer.

It isn't working for me.

Thanks
Gavin

Jeff Lavezzo
1/30/11 11:28 AM

> Hopefully, this gives you a jump start on how to use
> Liferay's SearchContainer in your own portlets.

No. It gives me an idea how to declare a SearchContainer in a portlet. Not how to use it.

If you'd actually said what a SearchContainer does or what I can do with it, THEN this article may have "Demystified" SearchContainer.

Dave Weitzel
3/24/11 1:26 PM

I agree with Jeff, and unfortunately is typical of a lot of "documentation" here.
I am trying to change the calendars for instance that contract everything to
<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
prior to that creates resultRows for the html to be out put in each row. this must be a default name passed somewhere, so if I want to create a different set of rows called resultRows2 where do I pass this to the iterator?

I may just use this page as a template for doing things long hand.

Nathan Bragg
3/31/11 7:21 AM

I'm not sure if it will help everyone, however this is how I got what I needed working.

This documentation is poor, and I wish there was a more complete example hopefully the below helps.

first I used an import of:

1<%@ page import="com.liferay.portal.kernel.util.ListUtil" %>


Second I used scriptlets, (there might be a better way), to get out List object out of the portlet context so I would be able to use it in the search container.

So something like this:
1PortletContext pc = renderRequest.getPortletSession().getPortletContext();
2List<SPRS> sprsList = (List)pc.getAttribute("sprsdata");


Third I used the ListUtil to help with the pagination.
So:

1<liferay-ui:search-container-results total="<%= sprsList.size() %>"    results="<%= ListUtil.subList(sprsList,searchContainer.getStart(),searchContainer.getEnd()) %>"
2    />


My complete code for the jsp is:

 1
 2<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 3<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
 4<%@ taglib prefix="s" uri="/struts-tags" %>
 5<%@ page import="javax.portlet.PortletContext" %>
 6<%@ page import="javax.portlet.PortletURL" %>
 7<%@ page import="com.liferay.portal.kernel.util.ListUtil" %>
 8
 9
10<%@ page import="java.util.List,myproject.jaxb.NewDataSet.SPRS" %>
11<portlet:defineObjects />
12
13<%
14
15PortletContext pc = renderRequest.getPortletSession().getPortletContext();
16List<SPRS> sprsList = (List)pc.getAttribute("sprsdata");
17if (sprsList == null || sprsList.isEmpty()){
18    System.out.println("sprs is null");
19}else{
20    SPRS ss = sprsList.get(0);
21    System.out.println("****");
22    System.out.println(ss.getNAME() + " " + ss.getCITYA());
23}
24
25PortletURL url = renderResponse.createRenderURL();
26//PortletURL url = renderResponse.createActionURL();
27
28System.out.println(url.toString());
29%>
30
31­<s:form action="index">
32<s:hidden name="reset" value="true" />
33<s:submit property="update" value="Back"/>
34</s:form>
35
36<liferay-ui:search-container delta="10" emptyResultsMessage="No Results Were found for the Selected Criteria">
37    <liferay-ui:search-container-results total="<%= sprsList.size() %>"    results="<%= ListUtil.subList(sprsList,searchContainer.getStart(),searchContainer.getEnd()) %>"
38    />
39    <liferay-ui:search-container-row modelVar="sprs" className="myproject.jaxb.NewDataSet.SPRS" >
40                                                      
41       
42        <liferay-ui:search-container-column-text name="Name" value="<%= sprs.getNAME() %>" />
43        <liferay-ui:search-container-column-text name="Street" value="<%= sprs.getSTREET1ADDR() %>"/>
44        <liferay-ui:search-container-column-text name="City" value="<%= sprs.getCITYA() %>"/>
45    </liferay-ui:search-container-row>
46    <liferay-ui:search-iterator />
47</liferay-ui:search-container>

Prakash Khanchandani
4/21/11 3:17 AM

The search-container is not working for me in configuration.jsp i.e. in the configuration mode, it gives this error when i click any of the pagination links and the page goes blank:
10:07:18,320 ERROR [PortletRequestProcessor:402] 86 does not have any paths specified

Pagination links like: next, last or if I select the drop-down to increase the number of pages gives the blank page & the error message

Though it is working fine on my edit.jsp page which is loaded after portlet's doEdit method is called.

I have tried giving custom iteratorURL using renderURL and then actionURL, but the same thing.

I will be highly obliged if anyone can help me. I have searched the source but I could not find an implementation of the Search Container in the configuration.jsp.

If anyone has any idea as to an implementation in the source or a solution to this. I will be thankful.

shashi charathu
9/6/11 3:51 AM

Can we use two tables to display one search-container-row?
is it possible...if possible can anybody please help me...

gas --
1/25/12 9:48 AM

I hope arrive at time,

I had the same error, I only see the where is the url in the buttons, then I know the parameters that I need.

To finalize, I write this code:
<%
Portlet_test = renderResponse.createRenderURL();
%>

in the search-conainer:

<liferay-ui:search-iterator >

<%
portletURL_test.setParameter("cur", String.valueOf(cur));
portletURL_test.setParameter("tabs", tabs);
.....
portletURL_test.setParameter("delta",String.valueOf(delta));
search­Container.setIteratorURL(portletURL_test);
%>
</liferay-ui:search-iterator>