Spring boot + Thymeleaf + Semantic UI implement a simple pagination
博客专区 > RealPari 的博客 > 博客详情
Spring boot + Thymeleaf + Semantic UI implement a simple pagination
RealPari 发表于9个月前
Spring boot + Thymeleaf + Semantic UI implement a simple pagination
  • 发表于 9个月前
  • 阅读 94
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: Spring boot + Thymeleaf + Semantic UI分页

Controller:

    @RequestMapping(value="/home", method=RequestMethod.GET)
	public String getHomePage(Model model, 
			@RequestParam(value="page", defaultValue = "0") Integer pageIndex,
			@RequestParam(value="pageSize", defaultValue = "10") Integer pageSize){
		
		Sort sort = new Sort(Direction.ASC, "id");
		Pageable pageable = new PageRequest(pageIndex, pageSize, sort);
		Page<User> pageUser = userRepository.findAll(pageable);
		model.addAttribute("users", pageUser.getContent());
		model.addAttribute("pageIndex", pageable.getPageNumber());
		model.addAttribute("pageSize", pageable.getPageSize());
		model.addAttribute("isFirst", pageUser.isFirst());
		model.addAttribute("isLast", pageUser.isLast());
		model.addAttribute("hasPrevious", pageUser.hasPrevious());
		model.addAttribute("hasNext", pageUser.hasNext());
		model.addAttribute("totalPages", pageUser.getTotalPages());
		return "home";		
	}

View page:

<nav>
	<div class="ui pagination stackable menu pagination-centered">
		<a th:class="'item ' + (${isFirst} ? 'disabled' : '')" href="#" th:href="@{/home(page=0)}">First</a> 
		<a th:class="'item ' + (${hasPrevious} ? '' : 'disabled')" href="#" th:href="@{/home(page=(${pageIndex}-1 lt 0 ? 0 : ${pageIndex-1}))}"> Previous </a>
		<div class="item" th:text="${pageIndex+1}+'/'+${totalPages}">1</div>
		<a th:class="'item ' + (${hasNext} ? '' : 'disabled')" href="#" th:href="@{/home(page=(${pageIndex+1} gt ${totalPages} ? ${totalPages} : ${pageIndex+1}))}"> Next </a>
		<a th:class="'item '+ (${isLast} ? 'disabled' : '')" href="#" th:href="@{/home(page=${totalPages-1})}">Last</a>
	</div>
</nav>

Result:

This is a simple method to implement a pagination function. Just using 'Pageable' and 'Page' of Spring, not need to integrate with other templates.

If you have any issues, please add a comment, thanks. Good luck!

共有 人打赏支持
粉丝 2
博文 49
码字总数 15143
×
RealPari
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: