Pagination in Liferay
There are two ways of implementing pagination in liferay. [These implementation was done in plugin portlet that consumes webservices].
A. Via liferay-ui:search-container. #
Following is the code snippet;
<%
Model[] hospitalresults = (Model[]) request.getAttribute("hospitalresults");
List<Names> list = new ArrayList<Names>();
Integer count = (Integer)request.getAttribute("count");
Integer delta = (Integer)request.getAttribute("delta");
Integer cur = (Integer)request.getAttribute("cur");
if(cur == null){
cur = 1;
}
if(delta == null){
delta = 20;
}
if(count == null){
count = 0;
}
if(hospitalresults != null){
for(Model hospital : hospitalresults){
list.add((Names)hospital);
}
}
String search = (String) request.getAttribute("search");
if(search == null){
search = "";
}
System.out.println(search);
PortletURL portletURL = renderResponse.createActionURL();
portletURL.setParameter("action", "search");
portletURL.setParameter("entity", "hospital");
portletURL.setParameter("search", search);
%>
<liferay-ui:search-container iteratorURL="<%= portletURL %>" delta="<%= delta %>" emptyResultsMessage="No Hospitals Found.">
<liferay-ui:search-container-results total="<%= count %>" results="<%= list %>" />
<liferay-ui:search-container-row modelVar="hospital" className="com.intuitive.surgeonlocator.models.Names" >
<% String name = hospital.getName(); %>
<liferay-ui:search-container-column-text name="HOSPITAL">
<a href="<portlet:actionURL>
<portlet:param name="action" value="edit" />
<portlet:param name="entity" value="hospital" />
<portlet:param name="hospitalid" value="<%= String.valueOf(hospital.getId()) %>" />
</portlet:actionURL>"><%= name %></a>
</liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-text name="DOCTORS">
<a href="<portlet:actionURL>
<portlet:param name="action" value="showsurgeons" />
<portlet:param name="entity" value="hospital" />
<portlet:param name="hospitalid" value="<%= String.valueOf(hospital.getId()) %>" />
</portlet:actionURL>">VIEW DOCTORS</a>
</liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-text name="DELETE">
<a href="<portlet:actionURL>
<portlet:param name="action" value="delete"/>
<portlet:param name="entity" value="hospital"/>
<portlet:param name="hospitalid" value="<%= String.valueOf(hospital.getId()) %>" />
<portlet:param name="search" value="<%= search %>" />
</portlet:actionURL>">DELETE</a>
</liferay-ui:search-container-column-text>
</liferay-ui:search-container-row>
<% portletURL.setParameter("cur", String.valueOf(cur)); searchContainer.setIteratorURL(portletURL); %>
<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
</liferay-ui:search-container>
B. Via creating SearchContainer object. #
Following is the code snippet
<%
PortletURL portletURL = renderResponse.createRenderURL();
SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, 20, portletURL, null, null);
Model[] hospitalresults = (Model[]) request.getAttribute("hospitalresults");
Integer count = (Integer)request.getAttribute("count");
String keywords = (String)request.getAttribute("keywords");
List<Names> list = new ArrayList<Names>();
if(hospitalresults != null){
for(Model hospital : hospitalresults){
list.add((Names)hospital);
}
}
searchContainer.setResults(list);
if(count != null)
searchContainer.setTotal(count);
List<String> headerNames = new ArrayList<String>();
headerNames.add("HOSPITAL");
headerNames.add("DOCTORS");
headerNames.add("DELETE");
searchContainer.setHeaderNames(headerNames);
List resultRows = searchContainer.getResultRows();
for(int i=0; i<list.size(); i++){
Names hospital = (Names)list.get(i);
ResultRow row = new ResultRow(hospital, hospital.getName(), i);
row.addText(hospital.getName());
row.addText("VIEW DOCTORS");
row.addText("DELETE");
resultRows.add(row);
}
%>
<c:if test="${hospitalresults != null}" >
<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" paginate="true" />
</c:if>
NOTE: TO MAKE PAGINATION ON CURRENT PAGE CLICK IMPLEMENT ADD FOLLOWING LINE IN YOUR CONTROLLER
actionResponse.setRenderParameter("cur", cur);
cur is the parameter which is to be updated on every request when user clicks on first, previous, next and last and even page selection dropdown.
16384 Views