Skip to content

Commit d057ce0

Browse files
admin: zones pager
1 parent 2f7ff1d commit d057ce0

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

src/main/java/org/woehlke/greenshop/admin/web/taxes/ZoneController.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.woehlke.greenshop.admin.web.taxes;
22

3+
import org.springframework.data.domain.Page;
4+
import org.springframework.data.domain.PageRequest;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.domain.Sort;
37
import org.springframework.stereotype.Controller;
48
import org.springframework.ui.Model;
59
import org.springframework.web.bind.annotation.PathVariable;
610
import org.springframework.web.bind.annotation.RequestMapping;
711
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.RequestParam;
813
import org.woehlke.greenshop.admin.AdminMenuCategory;
914
import org.woehlke.greenshop.customer.service.ZoneService;
1015
import org.woehlke.greenshop.customer.entities.Zone;
@@ -21,27 +26,35 @@ public class ZoneController {
2126
@Inject
2227
private ZoneService zoneService;
2328

29+
30+
private final static int PAGE_SIZE = 20;
31+
32+
private final static String FIRST_PAGE = "0";
33+
2434
@RequestMapping(value = "/admin/zones", method = RequestMethod.GET)
25-
public String zones(Model model){
35+
public String zones(@RequestParam(value="page",defaultValue=FIRST_PAGE) int page, Model model){
2636
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();
2737
model.addAttribute("menuCategory",menuCategory);
28-
List<Zone> zones = zoneService.findAllZones();
38+
Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "country.name");
39+
Page<Zone> zones = zoneService.findAllZones(pageRequest);
2940
model.addAttribute("zones",zones);
3041
Zone thisZone = null;
31-
if(zones.size()>0){
42+
if(zones.getContent().size()>0){
3243
thisZone = zones.iterator().next();
3344
}
3445
model.addAttribute("thisZone",thisZone);
3546
return "admin/zones";
3647
}
3748

3849
@RequestMapping(value = "/admin/zones/{zoneId}", method = RequestMethod.GET)
39-
public String zoneId(@PathVariable long zoneId, Model model){
50+
public String zoneId(@RequestParam(value="page",defaultValue=FIRST_PAGE) int page,
51+
@PathVariable long zoneId, Model model){
4052
int menuCategory = AdminMenuCategory.LOCATION_TAXES.ordinal();
4153
model.addAttribute("menuCategory",menuCategory);
4254
Zone thisZone = zoneService.findZoneById(zoneId);
4355
model.addAttribute("thisZone",thisZone);
44-
List<Zone> zones = zoneService.findAllZones();
56+
Pageable pageRequest = new PageRequest(page,PAGE_SIZE, Sort.Direction.ASC, "country.name");
57+
Page<Zone> zones = zoneService.findAllZones(pageRequest);
4558
model.addAttribute("zones",zones);
4659
return "admin/zones";
4760
}

src/main/java/org/woehlke/greenshop/customer/service/ZoneService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.woehlke.greenshop.customer.service;
22

3+
import org.springframework.data.domain.Page;
4+
import org.springframework.data.domain.Pageable;
35
import org.woehlke.greenshop.customer.entities.Zone;
46

57
import java.util.List;
@@ -10,7 +12,7 @@
1012
*/
1113
public interface ZoneService {
1214

13-
List<Zone> findAllZones();
15+
Page<Zone> findAllZones(Pageable pageRequest);
1416

1517
Zone findZoneById(long zoneId);
1618

src/main/java/org/woehlke/greenshop/customer/service/ZoneServiceImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.woehlke.greenshop.customer.service;
22

3+
import org.springframework.data.domain.Page;
4+
import org.springframework.data.domain.Pageable;
35
import org.springframework.transaction.annotation.Propagation;
46
import org.springframework.transaction.annotation.Transactional;
57
import org.woehlke.greenshop.customer.entities.Zone;
@@ -23,8 +25,8 @@ public class ZoneServiceImpl implements ZoneService {
2325
private ZoneRepository zoneRepository;
2426

2527
@Override
26-
public List<Zone> findAllZones() {
27-
return zoneRepository.findAll();
28+
public Page<Zone> findAllZones(Pageable pageRequest) {
29+
return zoneRepository.findAll(pageRequest);
2830
}
2931

3032
@Override

src/main/webapp/WEB-INF/jsp/admin/zones.jsp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<td class="dataTableHeadingContent" align="center">Code</td>
2020
<td class="dataTableHeadingContent" align="right">Action&nbsp;</td>
2121
</tr>
22-
<c:forEach var="zone" items="${zones}">
22+
<c:forEach var="zone" items="${zones.content}">
2323
<c:if test="${zone.id == thisZone.id}">
2424
<tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://localhost/oscommerce2/admin/zones.php?page=1&cID=102&action=edit'">
2525
<td class="dataTableContent">${zone.country.name}</td>
@@ -40,8 +40,27 @@
4040
<tr>
4141
<td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
4242
<tr>
43-
<td class="smallText" valign="top">Displaying <strong>1</strong> to <strong>20</strong> (of <strong>181</strong> zones)</td>
44-
<td class="smallText" align="right"><form name="pages" action="http://localhost/oscommerce2/admin/zones.php" method="get">&lt;&lt;&nbsp;&nbsp;Page <select name="page" onchange="this.form.submit();"><option value="1" selected="selected">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select> of 10&nbsp;&nbsp;<a href="http://localhost/oscommerce2/admin/zones.php?page=2" class="splitPageLink">&gt;&gt;</a></form></td>
43+
<td class="smallText" valign="top">Displaying <strong>${zones.number*20+1}</strong> to <strong>${zones.number*20+zones.numberOfElements}</strong> (of <strong>${zones.totalElements}</strong> zones)</td>
44+
<td class="smallText" align="right">
45+
<form name="pages" action="<c:url value="/admin/zones"/>" method="get"><c:choose>
46+
<c:when test="${zones.hasPreviousPage()}"><a href="<c:url value="/admin/zones?page=${zones.previousPageable().pageNumber}"/>" class="splitPageLink">&lt;&lt;</a></c:when>
47+
<c:otherwise>&lt;&lt;</c:otherwise>
48+
</c:choose>&nbsp;&nbsp;Page <select name="page" onchange="this.form.submit();">
49+
<c:forEach begin="0" end="${zones.totalPages-1}" varStatus="status">
50+
<c:choose>
51+
<c:when test="${status.index == zones.number}">
52+
<option selected="selected" value="${status.index}">${status.index +1}</option>
53+
</c:when>
54+
<c:otherwise>
55+
<option value="${status.index}">${status.index +1}</option>
56+
</c:otherwise>
57+
</c:choose>
58+
</c:forEach>
59+
</select> of ${zones.totalPages}&nbsp;&nbsp;<c:choose>
60+
<c:when test="${zones.hasNextPage()}"><a href="<c:url value="/admin/zones?page=${zones.nextPageable().pageNumber}"/>" class="splitPageLink">&gt;&gt;</a></c:when>
61+
<c:otherwise>&gt;&gt;</c:otherwise>
62+
</c:choose></form>
63+
</td>
4564
</tr>
4665
<tr>
4766
<td class="smallText" colspan="2" align="right"><span class="tdbLink"><a id="tdb1" href="http://localhost/oscommerce2/admin/zones.php?page=1&action=new">New Zone</a></span><script type="text/javascript">$("#tdb1").button({icons:{primary:"ui-icon-plus"}}).addClass("ui-priority-secondary").parent().removeClass("tdbLink");</script></td>

0 commit comments

Comments
 (0)