« Retour - 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.

Moyenne (17 Voter)
L'estimation moyenne est de 3.2941176470588234 étoiles sur 5.
Commentaires
Réponses Auteur Données
Hi , How do we get the RowChecker to work in... Gavin Meyers 22 janvier 2010 04:17
> Hopefully, this gives you a jump start on how... asdf asdf 30 janvier 2011 11:28
I agree with Jeff, and unfortunately is typical... Dave Weitzel 24 mars 2011 13:26
I'm not sure if it will help everyone, however... Nathan Bragg 31 mars 2011 07:21
The search-container is not working for me in... Prakash Khanchandani 21 avril 2011 03:17
Can we use two tables to display one... Meenu charathu 6 septembre 2011 03:51
I hope arrive at time, I had the same error, I... gas -- 25 janvier 2012 09:48
Hello Dear friends Could anyone tell me how to... Mahdi Lashkari 23 juin 2012 21:47
pagination working code on :... Jignesh Vachhani 27 juillet 2012 04:37
So anyone knows how to user... Alex Curtui 14 novembre 2012 02:34
@Alex Best implementation for search-toggle,... Akash B Gajjar 25 novembre 2012 19:45
Thank you, I've found it then, but now I just... Alex Curtui 25 janvier 2013 02:27
I want liferay to sh0w my data in a tabular... mehdi sunasara 19 juillet 2013 02:17
How to implement List class... Mittal Patoliya 31 juillet 2013 03:56
Does Anyone know how to change the column... Asif Billa 21 mai 2015 05:31
easiest t o use cssClass="<your class name>" in... Dave Weitzel 21 mai 2015 10:29
Thanks Dave, I have tried your alternate... Asif Billa 22 mai 2015 08:34
Does Anyone know how to change the column... Asif Billa 21 mai 2015 05:32

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

It isn't working for me.

Thanks
Gavin
Publié le 22/01/10 04: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.
Publié le 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.
Publié le 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>
Publié le 31/03/11 07: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.
Publié le 21/04/11 03:17.
Can we use two tables to display one search-container-row?
is it possible...if possible can anybody please help me...
Publié le 06/09/11 03:51 en réponse à 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>
Publié le 25/01/12 09:48 en réponse à 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((
Publié le 23/06/12 21:47.
pagination working code on : http://www.liferaysolution.com/2012/07/pagination-in-liferay-61.html
Publié le 27/07/12 04:37.
So anyone knows how to user "liferay-ui:search-toggle" tag?

Regards,
Alex
Publié le 14/11/12 02:34 en réponse à 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.
Publié le 25/11/12 19:45 en réponse à 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".
Publié le 25/01/13 02:27 en réponse à 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
Publié le 19/07/13 02: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.
Publié le 31/07/13 03: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>
Publié le 21/05/15 05:31 en réponse à 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>
Publié le 21/05/15 05: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;}
Publié le 21/05/15 10:29 en réponse à 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>
Publié le 22/05/15 08:34 en réponse à Dave Weitzel.