Add pagination for indicators

This commit is contained in:
Anton Romanov 2025-01-30 17:00:16 +04:00
parent 55ff385407
commit 04b3b311a9
4 changed files with 44 additions and 2 deletions

View File

@ -1,18 +1,26 @@
package ru.ulstu.aspirant.controller; package ru.ulstu.aspirant.controller;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.aspirant.model.Report; import ru.ulstu.aspirant.model.Report;
import ru.ulstu.aspirant.service.AspirantService; import ru.ulstu.aspirant.service.AspirantService;
import ru.ulstu.indicator.model.Indicator; import ru.ulstu.indicator.model.Indicator;
import ru.ulstu.model.OffsetablePageRequest;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@Controller @Controller
@RequestMapping("aspirant") @RequestMapping("aspirant")
public class AspirantController { public class AspirantController {
private static final Integer DEFAULT_PAGE_SIZE = 1;
private static final Integer DEFAULT_PAGE_NUMBER = 1;
private final AspirantService aspirantService; private final AspirantService aspirantService;
public AspirantController(AspirantService aspirantService) { public AspirantController(AspirantService aspirantService) {
@ -20,9 +28,22 @@ public class AspirantController {
} }
@GetMapping("aspirantReport") @GetMapping("aspirantReport")
public String createReport(Model model) { public String createReport(Model model,
List<Indicator> indicators = aspirantService.getIndicatorsByCourse(); @RequestParam Optional<Integer> page,
@RequestParam Optional<Integer> size) {
int currentPage = page.orElse(DEFAULT_PAGE_NUMBER);
int pageSize = size.orElse(DEFAULT_PAGE_SIZE);
Page<Indicator> indicators = aspirantService.getIndicatorsByCourse(new OffsetablePageRequest(currentPage - 1, pageSize));
model.addAttribute("indicators", indicators); model.addAttribute("indicators", indicators);
int totalPages = indicators.getTotalPages();
if (totalPages > 0) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed()
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
model.addAttribute("report", new Report()); model.addAttribute("report", new Report());
return "aspirant/editReport"; return "aspirant/editReport";
} }

View File

@ -1,5 +1,7 @@
package ru.ulstu.aspirant.service; package ru.ulstu.aspirant.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.admin.model.AspirantForm; import ru.ulstu.admin.model.AspirantForm;
import ru.ulstu.aspirant.model.Aspirant; import ru.ulstu.aspirant.model.Aspirant;
@ -48,4 +50,8 @@ public class AspirantService {
Aspirant aspirant = getAspirantByUser(UserUtils.getCurrentUserLogin()); Aspirant aspirant = getAspirantByUser(UserUtils.getCurrentUserLogin());
return indicatorService.getIndicatorsByCourse(aspirant.getCourse()); return indicatorService.getIndicatorsByCourse(aspirant.getCourse());
} }
public Page<Indicator> getIndicatorsByCourse(Pageable pageable) {
return indicatorService.getIndicatorsByCourse(pageable);
}
} }

View File

@ -1,5 +1,7 @@
package ru.ulstu.indicator.service; package ru.ulstu.indicator.service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.admin.model.IndicatorForm; import ru.ulstu.admin.model.IndicatorForm;
import ru.ulstu.indicator.model.Course; import ru.ulstu.indicator.model.Course;
@ -39,4 +41,8 @@ public class IndicatorService {
public List<Indicator> getIndicatorsByCourse(Course course) { public List<Indicator> getIndicatorsByCourse(Course course) {
return indicatorRepository.findAllByCourse(course); return indicatorRepository.findAllByCourse(course);
} }
public Page<Indicator> getIndicatorsByCourse(Pageable pageable) {
return indicatorRepository.findAll(pageable);
}
} }

View File

@ -99,6 +99,15 @@
</button> </button>
<a href="/aspirant/reports" class="btn btn-outline-dark">Отмена</a> <a href="/aspirant/reports" class="btn btn-outline-dark">Отмена</a>
</form> </form>
<div th:if="${indicators.totalPages > 0}" class="pagination">
<span style="float: left; padding: 5px 5px;">Страницы:</span>
</div>
<div th:if="${indicators.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}">
<a th:href="@{/aspirant/aspirantReport(size=${indicators.size}, page=${pageNumber})}"
th:text=${pageNumber}
th:class="${pageNumber == indicators.number+1} ? active"></a>
</div>
<link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/> <link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/>
<link rel="stylesheet" href="/webjars/bootstrap-glyphicons/bdd2cbfba0/css/bootstrap-glyphicons.css"/> <link rel="stylesheet" href="/webjars/bootstrap-glyphicons/bdd2cbfba0/css/bootstrap-glyphicons.css"/>
</div> </div>