« Volver a 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>

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 archivos adjuntos
186447 Accesos
Promedio (17 Votos)
La valoración media es de 3.2941176470588234 estrellas de 5.
Comentarios
Respuestas anidadas Autor Fecha
Hi , How do we get the RowChecker to work in... Gavin Meyers 22 de enero de 2010 4:17
> Hopefully, this gives you a jump start on how... asdf asdf 30 de enero de 2011 11:28
I agree with Jeff, and unfortunately is typical... Dave Weitzel 24 de marzo de 2011 13:26
I'm not sure if it will help everyone, however... Nathan Bragg 31 de marzo de 2011 7:21
The search-container is not working for me in... Prakash Khanchandani 21 de abril de 2011 3:17
Can we use two tables to display one... Meenu charathu 6 de septiembre de 2011 3:51
I hope arrive at time, I had the same error, I... gas -- 25 de enero de 2012 9:48
Hello Dear friends Could anyone tell me how to... Mahdi Lashkari 23 de junio de 2012 21:47
pagination working code on :... Jignesh Vachhani 27 de julio de 2012 4:37
So anyone knows how to user... Alex Curtui 14 de noviembre de 2012 2:34
@Alex Best implementation for search-toggle,... Akash B Gajjar 25 de noviembre de 2012 19:45
Thank you, I've found it then, but now I just... Alex Curtui 25 de enero de 2013 2:27
I want liferay to sh0w my data in a tabular... mehdi sunasara 19 de julio de 2013 2:17
How to implement List class... Mittal Patoliya 31 de julio de 2013 3:56
Does Anyone know how to change the column... Asif Billa 21 de mayo de 2015 5:31
easiest t o use cssClass="<your class name>" in... Dave Weitzel 21 de mayo de 2015 10:29
Thanks Dave, I have tried your alternate... Asif Billa 22 de mayo de 2015 8:34
Does Anyone know how to change the column... Asif Billa 21 de mayo de 2015 5:32

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

It isn't working for me.

Thanks
Gavin
Publicado el día 22/01/10 4:17.
> 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.
Publicado el día 30/01/11 11:28.
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.
Publicado el día 24/03/11 13:26.
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>
Publicado el día 31/03/11 7:21.
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.
Publicado el día 21/04/11 3:17.
Can we use two tables to display one search-container-row?
is it possible...if possible can anybody please help me...
Publicado el día 6/09/11 3:51 en respuesta a 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>
Publicado el día 25/01/12 9:48 en respuesta a 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((
Publicado el día 23/06/12 21:47.
pagination working code on : http://www.liferaysolution.com/2012/07/pagination-in-liferay-61.html
Publicado el día 27/07/12 4:37.
So anyone knows how to user "liferay-ui:search-toggle" tag?

Regards,
Alex
Publicado el día 14/11/12 2:34 en respuesta a 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.
Publicado el día 25/11/12 19:45 en respuesta a 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".
Publicado el día 25/01/13 2:27 en respuesta a 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
Publicado el día 19/07/13 2:17.
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.
Publicado el día 31/07/13 3:56.
Does Anyone know how to change the column header font ?
I have used below , but it's not working.

<liferay-ui:search-container-column-text name="Activity ID" property="activity_id" align="right" href="<%= transactivityDetailsURL.toString() %>">
<span style=" font-weight:bold;
font-style:italic;
font-size: 12px" >
</span>
</liferay-ui:search-container-column-text>
Publicado el día 21/05/15 5:31 en respuesta a me liferay.
Does Anyone know how to change the column header font ?
I have used below , but it's not working.

<liferay-ui:search-container-column-text name="Activity ID" property="activity_id" align="right" href="<%= transactivityDetailsURL.toString() %>">
<span style=" font-weight:bold;
font-style:italic;
font-size: 12px" >
</span>
</liferay-ui:search-container-column-text>
Publicado el día 21/05/15 5:32.
easiest t o use cssClass="<your class name>" in the tag.

alternatively look up the DM of the result an dad to your stylesheet something like
.searchcontainer-content th {font-weight:bold; font-style:italic; font-size: 12px;}
Publicado el día 21/05/15 10:29 en respuesta a Asif Billa.
Thanks Dave,

I have tried your alternate solution. (.searchcontainer-content th {font-weight:bold; font-style:italic; font-size: 10px;})
The surprising part is the column header is getting changed as Italic but the font size is not getting reflected.

I have tried the first approach as well. The grid data is getting reflected as per the css class but unfortunately I'm not able to change the column header as well as padding and spacing. PFB my code sample.

<liferay-ui:search-container-column-text name="Activity Code" property="activity_code" align="right" cssClass="grid-data-attributes" href="<%= transactivityDetailsURL.toString() %>" />
<style>
.grid-data-attributes {
font-weight: normal;
font-size: 13px;
text-align: center;
padding:0;
margin:0;
paddingLeft:0;
paddingRight:0;
headerWordWrap:true;
white-space:nowrap;
}
</style>
Publicado el día 22/05/15 8:34 en respuesta a Dave Weitzel.