赞
踩
JHipster利用了Spring Boot的Pageable分页机制,前端排序是针对数据库数据,而非当前页面数据,充分满足了实际需求。下面分前端和后端两部分分别介绍一下它的实现方式。
- public sort(): any {
- const result = [this.propOrder + ',' + (this.reverse ? 'desc' : 'asc')];
- if (this.propOrder !== 'id') {
- result.push('id');
- }
- return result;
- }
buildPaginationQueryOpts函数是组装了后端Pageable所需要的属性。
- function buildPaginationQueryOpts(paginationQuery) {
- if (paginationQuery) {
- let sorts = '';
- for (const idx of Object.keys(paginationQuery.sort)) {
- if (sorts.length > 0) {
- sorts += '&';
- }
- sorts += 'sort=' + paginationQuery.sort[idx];
- }
- return `${sorts}&page=${paginationQuery.page}&size=${paginationQuery.size}`;
- }
- return '';
- }
- public retrieve(req?: any): Promise<any> {
- return axios.get(`api/admin/users?${buildPaginationQueryOpts(req)}`);
- }
例如:每页显示3行,显示第一页,以login升序排列,请求URL为:
http://localhost:9000/api/admin/users?sort=login,asc&sort=id&page=1&size=3
- @GetMapping("/users")
- @PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.ADMIN + "\")")
- public ResponseEntity<List<AdminUserDTO>> getAllUsers(Pageable pageable) {
- log.debug("REST request to get all User for an admin");
- if (!onlyContainsAllowedProperties(pageable)) {
- return ResponseEntity.badRequest().build();
- }
-
- final Page<AdminUserDTO> page = userService.getAllManagedUsers(pageable);
- HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
- return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
- }
- public Page<AdminUserDTO> getAllManagedUsers(Pageable pageable) {
- return userRepository.findAll(pageable).map(AdminUserDTO::new);
- }
- @Repository
- public interface UserRepository extends MongoRepository<User, String> {
- Optional<User> findOneByActivationKey(String activationKey);
-
- List<User> findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(Instant dateTime);
-
- Page<User> findAll(Pageable pageable);
- }
Good Luck,
Cheers!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。