« 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().

Important to note here; regardless of attribute capitalisation in your service.XML, the property value must always start lower case. After that it seems to follow what you defined.

<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
87672 Views
Average (16 Votes)
The average rating is 3.4375 stars out of 5.
Comments
Threaded Replies Author Date
Hi , How do we get the RowChecker to work in... Gavin Meyers January 22, 2010 4:17 AM
> Hopefully, this gives you a jump start on how... Jeff Lavezzo January 30, 2011 11:28 AM
I agree with Jeff, and unfortunately is typical... Dave Weitzel March 24, 2011 1:26 PM
I'm not sure if it will help everyone, however... Nathan Bragg March 31, 2011 7:21 AM
The search-container is not working for me in... Prakash Khanchandani April 21, 2011 3:17 AM
Can we use two tables to display one... Meenu charathu September 6, 2011 3:51 AM
I hope arrive at time, I had the same error, I... gas -- January 25, 2012 9:48 AM
Hello Dear friends Could anyone tell me how to... Mahdi Lashkari June 23, 2012 9:47 PM
pagination working code on :... Jignesh Vachhani July 27, 2012 4:37 AM
So anyone knows how to user... Alex Curtui November 14, 2012 2:34 AM
@Alex Best implementation for search-toggle,... Akash B Gajjar November 25, 2012 7:45 PM
Thank you, I've found it then, but now I just... Alex Curtui January 25, 2013 2:27 AM
I want liferay to sh0w my data in a tabular... mehdi sunasara July 19, 2013 2:17 AM
How to implement List class... me liferay July 31, 2013 3:56 AM

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

It isn't working for me.

Thanks
Gavin
Posted on 1/22/10 4:17 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.
Posted on 1/30/11 11:28 AM.
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.
Posted on 3/24/11 1:26 PM.
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>
Posted on 3/31/11 7:21 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.
Posted on 4/21/11 3:17 AM.
Can we use two tables to display one search-container-row?
is it possible...if possible can anybody please help me...
Posted on 9/6/11 3:51 AM in reply to Prakash Khanchandani.
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>
Posted on 1/25/12 9:48 AM in reply to sasi kala.
Hello Dear friends
Could anyone tell me how to use "liferay-ui:search-toggle" tag ? in some documentations it has been mentioned that I should use
EntitySearchContainer container= (EntitySearchContainer)request.getAttribute("liferay-ui:search:searchContainer")­; but I haven't set any variable in the request? I mean where should I set "liferay-ui:search:searchContainer"? Please Help Meemoticon((
Posted on 6/23/12 9:47 PM.
pagination working code on : http://www.liferaysolution.com/2012/07/pagination-in-liferay-61.html
Posted on 7/27/12 4:37 AM.
So anyone knows how to user "liferay-ui:search-toggle" tag?

Regards,
Alex
Posted on 11/14/12 2:34 AM in reply to Jignesh Vachhani.
@Alex Best implementation for search-toggle, you will find in asset_search.jsp. Basically it is used to create search form and bind with search method with portlet.
Posted on 11/25/12 7:45 PM in reply to Alex Curtui.
Thank you, I've found it then, but now I just want to confirm that it worked.

PS: There is no "getResultsEnd" (note the S), but there is "getResultEnd".
Posted on 1/25/13 2:27 AM in reply to Akash B Gajjar.
I want liferay to sh0w my data in a tabular form from one field

http://www.liferay.com/community/forums/-/message_boards/message/26597202
Posted on 7/19/13 2:17 AM.
How to implement List class (com.liferay.portal.model.User in this case)? I have tried with simple and interface and pojo implementing that interface with set of properties but no success.
Posted on 7/31/13 3:56 AM.