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 Allegati
68535 Visualizzazioni
Media (3 Voti)
La media del punteggio è 4.33333333333333 stelle su 5.
Commenti
Commenti Autore Data
working code of pagination on... Jignesh Vachhani 27 luglio 2012 4.37
Thank you for this nice wiki. There is a... Hieu Vo 21 gennaio 2013 23.58
thanks for the hint, been working on this issue... D Tran 26 novembre 2013 17.30
Take a look at my working example... Laura Liparulo 29 gennaio 2013 3.45
But this variable "cur" isn't initialized in... Marko Perić 10 dicembre 2014 5.29
Hi, My jsp never makes it to the action phase... Robert Rayas 19 maggio 2015 15.45

working code of pagination on http://www.liferaysolution.com/2012/07/pagination-in-liferay-61.html
Inviato il 27/07/12 4.37.
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
Inviato il 21/01/13 23.58.
Take a look at my working example
http://www.liferay.com/community/forums/-/message_boards/message/21031639­
Inviato il 29/01/13 3.45.
thanks for the hint, been working on this issue for an hour emoticon
Inviato il 26/11/13 17.30 in risposta a 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?
Inviato il 10/12/14 5.29.
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"
Inviato il 19/05/15 15.45.