Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Laura Liparulo
Search container with form parameter - pagination problem SOLVED
29 gennaio 2013 7.11
Risposta

Laura Liparulo

Punteggio: Junior Member

Messaggi: 38

Data di Iscrizione: 30 giugno 2012

Messaggi recenti

Hello, guys!
I've been struggling for a couple of days trying to make my seach container pagination works.
When you clicking on the next page and you need to use a form parameter value submitted to get the rows, you need to store it in the portlet preferences, otherwise you lose it in the page "refresh".

I haven't found a solution on the web so far, so I'm posting my code snippets... that WORKS lol:
but I'm using portlet preferences ... check it out!

action method in the controller:

 1        public void searchVolume(ActionRequest request, ActionResponse response)
 2            throws IOException, PortletException, PortalException,
 3            SystemException, NoSuchVolumeException {
 4
 5        String volumeIdentifier = request.getParameter("volumeId");
 6        long volumeId = (long) Integer.parseInt(volumeIdentifier);
 7        long idVol = 0;
 8
 9        boolean found = false;
10        boolean emptyList = false;
11
12        List<Volume> volume = new ArrayList<Volume>();
13        volume = VolumeLocalServiceUtil.getAllVolumes();
14        List<CaseArchive> caseArchive = new ArrayList<CaseArchive>();
15        caseArchive = CaseArchiveLocalServiceUtil
16                .getAllCasesbyVolumeId(volumeId);
17
18        if (caseArchive.size() == 0) {
19            emptyList = true;
20        }
21
22        for (Volume itemVolume : volume) {
23            if (itemVolume.getVolumeId() == volumeId)
24                found = true;
25        }
26
27        if (found && emptyList) {
28            SessionMessages.add(request, "no-cases-found");
29        } else if (found && !emptyList) {
30            Volume vol = DBUtil.getVolumefromRequest(request);
31            idVol = vol.getVolumeId();
32
33            if (idVol == 1) {
34                System.out.println("Volume id iniziale: " + volumeId);
35                SessionErrors.add(request, "error-volume");
36            } else if (SearchValidator.volumeNotNull(idVol) && !(idVol == 1))
37
38            {
39                System.out.println("Volume id action : " + vol.getVolumeId());
40
41                response.setRenderParameter("volId", volumeIdentifier);
42                System.out.println("search volume clicked");
43
44                PortletPreferences prefs = request.getPreferences();
45                String volumeIdent = request.getParameter("volumeId");
46                if (volumeIdent != null) {
47                    prefs.setValue("volumeIdPref", volumeIdent);
48                    prefs.store();
49                }
50
51                VolumeLocalServiceUtil.clearService();
52
53                SessionMessages.add(request, "search-volume");
54
55            }
56        } else
57            SessionErrors.add(request, "error-volume");
58
59        response.setRenderParameter("jspPage", viewDatabaseJSP);
60
61    }



JSP page:
  1
  2
  3<%@include file="/init.jsp"%>
  4<portlet:defineObjects />
  5
  6<%
  7    CaseArchiveLocalServiceUtil.clearCache();
  8    VolumeLocalServiceUtil.clearCache();
  9
 10    //RoiLocalServiceUtil.clearCache();
 11    //ImageDBLocalServiceUtil.clearCache();
 12    //DicomLocalServiceUtil.clearCache();
 13    ImageTypeLocalServiceUtil.clearCache();
 14
 15    List<Volume> volumes = VolumeLocalServiceUtil.getAllVolumes();
 16    List<CaseArchive> cases = CaseArchiveLocalServiceUtil.getAllCases();
 17    Long volumeIdentifier = 1L;
 18
 19    Collections.sort(volumes, new Comparator<Volume>() {
 20        public int compare(Volume o1, Volume o2) {
 21            Volume p1 = (Volume) o1;
 22            Volume p2 = (Volume) o2;
 23            return p1.getVolumeName().compareToIgnoreCase(
 24                    p2.getVolumeName());
 25        }
 26    });
 27
 28    PortletURL portletURL = renderResponse.createRenderURL();
 29
 30    portletURL.setParameter("jspPage", "/html/admin/viewDatabase.jsp");
 31
 32    int selected = 0;
 33    String volSel = null;
 34
 35    PortletPreferences prefs = renderRequest.getPreferences();
 36    String volumeId = (String) prefs.getValue("volumeIdPref", "1");
 37%>
 38
 39<portlet:renderURL var="backUpURL">
 40    <portlet:param name="jspPage" value="/html/admin/backUp.jsp" />
 41</portlet:renderURL>
 42
 43<portlet:renderURL var="cancelURL">
 44    <portlet:param name="jspPage" value="/html/admin/view.jsp" />
 45</portlet:renderURL>
 46
 47<portlet:actionURL var="searchVolumeURL" name="searchVolume">
 48
 49</portlet:actionURL>
 50
 51<liferay-ui:success key="no-cases-found" message="no-cases" />
 52<liferay-ui:success key="search-volume" message="search-ok" />
 53<liferay-ui:error key="error-volume" message="volume-name-required" />
 54
 55
 56<aui:form name="fm" action="<%=searchVolumeURL.toString()%>"
 57    method="post">
 58    <aui:fieldset>
 59        <aui:select name="volumeId" label="Volume">
 60            <aui:option value="1">
 61                <liferay-ui:message key="Choose volume" />
 62            </aui:option>
 63            <%
 64                for (Volume volume : volumes) {
 65            %>
 66            <aui:option value="<%=volume.getVolumeId()%>">
 67                <%=volume.getVolumeName()%>
 68            </aui:option>
 69            <%
 70                }
 71            %>
 72        </aui:select>
 73
 74        <aui:button-row>
 75            <aui:button type="submit" value="Search" />
 76        </aui:button-row>
 77
 78    </aui:fieldset>
 79</aui:form>
 80
 81<liferay-ui:search-container delta="10" iteratorURL="<%=portletURL%>">
 82
 83    <liferay-ui:search-container-results>
 84        <%
 85            if (request.getParameter("volId") != null) {
 86                        volumeIdentifier = (long) Integer.parseInt(volumeId);
 87                        System.out.println("volId "
 88                                + request.getParameter("volId"));
 89
 90                        System.out.println("volumeId: from pref "
 91                                + volumeIdentifier);
 92
 93                    } else if (searchContainer.getCur() > 1) {
 94                        volumeIdentifier = (long) Integer.parseInt(volumeId);
 95
 96                    }
 97
 98                    List<CaseArchive> tempResults = DBUtil
 99                            .getAllCasesOk(volumeIdentifier);
100
101                    results = ListUtil.subList(tempResults,
102                            searchContainer.getStart(),
103                            searchContainer.getEnd());
104
105                    total = tempResults.size();
106
107                    pageContext.setAttribute("results", results);
108                    pageContext.setAttribute("total", total);
109
110                    portletURL.setParameter("cur",
111                            searchContainer.getCurParam());
112                    System.out.println("Cur PRINT:" + searchContainer.getCur());
113        %>
114
115    </liferay-ui:search-container-results>
116
117    <liferay-ui:search-container-row className="it.dicom.model.CaseArchive"
118        keyProperty="caseId" modelVar="caseArchive">
119
120        <liferay-ui:search-container-column-text name="Case Name"
121            property="caseName" />
122        <liferay-ui:search-container-column-jsp path="/html/admin/backUp.jsp"
123            align="right" />
124        <liferay-ui:search-container-column-button align="right"
125            href="<%=backUpURL%>" name='view Case' />
126
127    </liferay-ui:search-container-row>
128
129    <liferay-ui:search-iterator />
130
131</liferay-ui:search-container>
132
133<p>
134    <br /> <br /> <a href="<%=cancelURL%>">&larr; Back to Menu</a>
135</p>


In the example above, volumeId is the form action parameter (passed by the select option), while volumeIdPref is the portlet preferences parameter, which keeps the value while consulting the pages.
In the action method, that is invoked when submitting the form value, i've set a response parameter called volId which is used in the jsp to set the variable value when invoking the search-container result page "1". The render parameter is null when visiting the other pages, but it can be retrieved by the portlet preferences value.

I hope this helps. Let me know if you have questions or suggestions.
Regards
Laura
Ruchir Chaturvedi
RE: Search container with form parameter - pagination problem SOLVED
9 aprile 2013 9.53
Risposta

Ruchir Chaturvedi

Punteggio: New Member

Messaggi: 1

Data di Iscrizione: 19 luglio 2012

Messaggi recenti

Nice post, Thanks.
Jacques Traore
RE: Search container with form parameter - pagination problem SOLVED
15 maggio 2013 11.17
Risposta

Jacques Traore

Punteggio: Junior Member

Messaggi: 28

Data di Iscrizione: 21 gennaio 2013

Messaggi recenti

Very useful.
Thanks.
vijay pandey
RE: Search container with form parameter - pagination problem SOLVED
15 maggio 2013 23.46
Risposta

vijay pandey

Punteggio: Junior Member

Messaggi: 44

Data di Iscrizione: 1 settembre 2010

Messaggi recenti

informative information show in blog post

Cheers
Vijay Pandey