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 添付ファイル
68551 参照数
平均 (3 投票)
平均評価は4.33333333333333星中の5です。
コメント
コメント 作成者 日時
working code of pagination on... Jignesh Vachhani 2012/07/27 4:37
Thank you for this nice wiki. There is a... Hieu Vo 2013/01/21 23:58
thanks for the hint, been working on this issue... D Tran 2013/11/26 17:30
Take a look at my working example... Laura Liparulo 2013/01/29 3:45
But this variable "cur" isn't initialized in... Marko Perić 2014/12/10 5:29
Hi, My jsp never makes it to the action phase... Robert Rayas 2015/05/19 15:45

working code of pagination on http://www.liferaysolution.com/2012/07/pagination-in-liferay-61.html
投稿日時:12/07/27 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
投稿日時:13/01/21 23:58
Take a look at my working example
http://www.liferay.com/community/forums/-/message_boards/message/21031639­
投稿日時:13/01/29 3:45
thanks for the hint, been working on this issue for an hour emoticon
Hieu Voへのコメント。投稿日時:13/11/26 17:30
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?
投稿日時:14/12/10 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"
投稿日時:15/05/19 15:45