我在部门页面上有胸腺分页,效果很好,但是我有一个问题。
当我尝试更新部门名称时,此已重命名的部门从页面消失,并在最后一页显示为最后一行。
我该如何解决?我想更新部门名称,并将其放在同一位置,而不是最后。
这是我的代码。
服务:
public Page<Department> findPaginatedDepartments(final Pageable pageable) {
List<Department> departments = departmentRepository.findAll();
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
final List<Department> list;
if (departments.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, departments.size());
list = departments.subList(startItem, toIndex);
}
return new PageImpl<>(list, PageRequest.of(currentPage, pageSize), departments.size());
}
控制器:
@GetMapping()
public String getAllDepartments(
Model model,
@RequestParam("page") Optional<Integer> page,
@RequestParam("size") Optional<Integer> size) {
int currentPage = page.orElse(1);
int pageSize = size.orElse(10);
Page<Department> departmentPage = departmentService.findPaginatedDepartments(PageRequest.of(currentPage - 1, pageSize));
model.addAttribute("departmentPage", departmentPage);
int totalPages = departmentPage.getTotalPages();
if (totalPages > 0) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed()
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
return "/department/departments";
}
查看:
<div class="departments_wrapper main_menu_wrapper">
<div id="dep-grid" class="departments_table highlight">
<div class="head_departments">
<span class="head-left-grid" th:onclick="'javascript:sortDepartments()'">Name<i id="arrow-sort" class="tiny material-icons arrow-sort-button">expand_more</i></span>
<span class="head-right-grid">Edit</span>
</div>
<ul>
<div class="dep-body" th:each="department : ${departmentPage.content}">
<li id="dep-li" class="left-column" th:text="${department.name}"></li>
<li class="right-column">
<div class="dep_edit">
<a id="dep-modal-pic" class="edit_dep modal-trigger" href="#modal3"
th:onclick="'javascript:showFunctionModal(\'' + ${department.id} +'\' , \'' + ${department.name} +'\');'"><i
class="material-icons">more_horiz</i></a>
</div>
</li>
</div>
</ul>
</div>
<div class="pagination pagination-dep">
<ul>
<li class="disabled"><a href="#!"><i class="material-icons">chevron_left</i></a></li>
<li><a th:if="${departmentPage.totalPages > 0}" th:each="pageNumber : ${pageNumbers}"
th:href="@{/departments(size=${departmentPage.size}, page=${pageNumber})}" th:text="${pageNumber}"
th:class="${pageNumber==departmentPage.number + 1} ? active"></a></li>
<li class="disabled"><a href="#!"><i class="material-icons">chevron_right</i></a></li>
</ul>
</div>
</div>
更新部门名称
@Transactional
public void updateDepartment(final Long id, final Department department) {
final Department departmentFound = departmentRepository.getOne(id);
departmentFound.setName(department.getName());
departmentRepository.saveAndFlush(departmentFound);
}
答案 0 :(得分:0)
解决了!
经过多次搜索,我发现它是postgres "feature"
。
因此,我在 DepartmentService 中编写了比较器,该比较器将按ID对部门进行排序。
这解决了我的问题。
final Comparator<Department> cmp = Comparator.comparing(Department::getId);