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.

0 Attachments
57091 Views
Average (3 Votes)
The average rating is 4.33333333333333 stars out of 5.
Comments
Threaded Replies Author Date
working code of pagination on... Jignesh Vachhani July 27, 2012 4:37 AM
Thank you for this nice wiki. There is a... Hieu Vo January 21, 2013 11:58 PM
thanks for the hint, been working on this issue... D Tran November 26, 2013 5:30 PM
Take a look at my working example... Laura Liparulo January 29, 2013 3:45 AM
But this variable "cur" isn't initialized in... Marko Perić December 10, 2014 5:29 AM
Hi, My jsp never makes it to the action phase... Robert Rayas May 19, 2015 3:45 PM

working code of pagination on http://www.liferaysolution.com/2012/07/pagination-in-liferay-61.html
Posted on 7/27/12 4:37 AM.
Thank you for this nice wiki.

There is a mystery for me about where to set the 'cur' parameter though. The navigation does not work when I set cur value as an attribute in the view model (org.springframework.ui.Model)
model.addAttribute("cur", cur.toString());

However it will work when I set it as a render parameter
response.setRenderParameter("cur", cur.toString());

Is there anyone who understand this, please explain.

Thanks
Posted on 1/21/13 11:58 PM.
Take a look at my working example
http://www.liferay.com/community/forums/-/message_boards/message/21031639­
Posted on 1/29/13 3:45 AM.
thanks for the hint, been working on this issue for an hour emoticon
Posted on 11/26/13 5:30 PM in reply to Hieu Vo.
But this variable "cur" isn't initialized in Controller? What type (string?) and value I have to set to it so I can transfer it to jsp?
Posted on 12/10/14 5:29 AM.
Hi,
My jsp never makes it to the action phase to add the cur parameter. The rows per page and page 1 of 2 lists are clickable and work. When I click "Next", a warning is thrown and the portlet crashes. The warning is "This URL can only be invoked using POST"
Posted on 5/19/15 3:45 PM.