Spring + Thymeleaf - how to implement pagination for a list

前端 未结 3 1615
说谎
说谎 2020-12-08 22:49

I am developing a simple application using Spring + Thymeleaf. On one of the pages I have a list of items which needs to be paginated.

Ideally I would like to only s

相关标签:
3条回答
  • 2020-12-08 23:07

    Another option would be Ben Thurley's solution. We have implemented it and it's working smoothly: http://bthurley.wordpress.com/2012/07/18/spring-mvc-with-restful-datatables/

    It lacks couples of items, like the filter argument for the search, but you can easily add via the PagingCriteria object and make sure to add it on the TableParamArgumentResolver.

    public class TableParamArgumentResolver implements WebArgumentResolver {
    
        private static final String S_ECHO           = "sEcho";
        private static final String I_DISPLAY_START  = "iDisplayStart";
        private static final String I_DISPLAY_LENGTH = "iDisplayLength";
        private static final String I_SORTING_COLS   = "iSortingCols";
    
        private static final String I_SORT_COLS      = "iSortCol_";
        private static final String S_SORT_DIR       = "sSortDir_";
        private static final String S_DATA_PROP      = "mDataProp_";
        private static final String I_DATA_SEARCH    = "sSearch";
    
        public Object resolveArgument(MethodParameter param, NativeWebRequest request)
                throws Exception {
            TableParam tableParamAnnotation = param.getParameterAnnotation(TableParam.class);
    
            if (tableParamAnnotation != null) {
                HttpServletRequest httpRequest = (HttpServletRequest) request.getNativeRequest();
    
                String sEcho = httpRequest.getParameter(S_ECHO);
                String sDisplayStart = httpRequest.getParameter(I_DISPLAY_START);
                String sDisplayLength = httpRequest.getParameter(I_DISPLAY_LENGTH);
                String sSortingCols = httpRequest.getParameter(I_SORTING_COLS);
                String sSearch = httpRequest.getParameter(I_DATA_SEARCH);
    
                Integer iEcho = Integer.parseInt(sEcho);
                Integer iDisplayStart = Integer.parseInt(sDisplayStart);
                Integer iDisplayLength = Integer.parseInt(sDisplayLength);
                Integer iSortingCols = Integer.parseInt(sSortingCols);
    
                List<SortField> sortFields = new ArrayList<SortField>();
                for (int colCount = 0; colCount < iSortingCols; colCount++) {
                    String sSortCol = httpRequest.getParameter(I_SORT_COLS + colCount);
                    String sSortDir = httpRequest.getParameter(S_SORT_DIR + colCount);
                    String sColName = httpRequest.getParameter(S_DATA_PROP + sSortCol);
                    sortFields.add(new SortField(sColName, sSortDir));
                }
    
                PagingCriteria pc = new PagingCriteria(iDisplayStart, iDisplayLength, iEcho, sortFields, sSearch);
    
                return pc;
            }
    
            return WebArgumentResolver.UNRESOLVED;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 23:07

    I have this almost ready, hope it helps....

    <div class="tag-box tag-box-v7 text-justify">
        <div class="text-center">
            <ul class="pagination" th:with="elementsperpage=2, blocksize=10, pages=${page2th.Number}/${elementsperpage}, wholepages=${format.format(pages)},
    whole=(${page2th.Number}/${blocksize})+1, wholex=${format.format(whole)}, startnlockpage=${wholepages}*${blocksize+1}, endblockpage=${wholepages}*${blocksize+1}, 
    startpage=${wholex-1}*${blocksize}, endpage=(${wholex}*${blocksize})+1">
    
                <li>
                    <a th:if="${startpage gt 0}" th:href="@{${'/viewannouncements?p='}+${startpage}}">&lt;&lt;</a>
                    <a th:if="${startpage eq 0}" href="javascript:void(0);">&lt;&lt;</a>
                </li>
    
                <li th:each="pageNo : ${#numbers.sequence(endpage-11, (endpage lt page2th.TotalPages)? endpage-2 : page2th.TotalPages-1)}" 
                th:class="${page2th.Number eq pageNo}? 'active' : ''">
                        <a th:if="${page2th.Number eq pageNo}" href="javascript:void(0);">
                            <span th:text="${pageNo + 1}"></span>
                        </a>
                        <a th:if="${not (page2th.Number  eq pageNo)}" th:href="@{${'/viewannouncements?p='}+${pageNo+1}}">
                            <span th:text="${pageNo + 1}"></span>
                        </a>
                </li>
    
                <li>
                    <a th:if="${(endpage*elementsperpage) le (page2th.TotalElements)}" th:href="@{${'/viewannouncements?p='}+${endpage}}">&gt;&gt;</a>
                    <a th:if="${(endpage*elementsperpage) le (page2th.TotalElements)}" href="javascript:void(0);"></a>
                </li>
    
    
    
            </ul>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-08 23:12

    Similar to solution described in http://www.javacodegeeks.com/2013/03/implement-bootstrap-pagination-with-spring-data-and-thymeleaf.html

    but without using wrapper around Spring Pageable

    <div class="table-pagination">
        <ul class="pagination">
            <li th:class="${contactsPage.number eq 0} ? 'disabled' : ''">
                <a th:if="${not contactsPage.firstPage}" th:href="@{${'/contacts'}(page=${contactsPage.number-1},size=${contactsPage.size})}">Previous</a>
                <a th:if="${contactsPage.firstPage}" href="javascript:void(0);">Previous</a>
            </li>
    
            <li th:each="pageNo : ${#numbers.sequence(0, contactsPage.totalPages - 1)}" th:class="${contactsPage.number eq pageNo}? 'active' : ''">
                <a th:if="${contactsPage.number  eq pageNo}" href="javascript:void(0);">
                    <span th:text="${pageNo + 1}"></span>
                </a>
                <a th:if="${not (contactsPage.number  eq pageNo)}" th:href="@{${'/contacts'}(page=${pageNo},size=${contactsPage.size})}">
                    <span th:text="${pageNo + 1}"></span>
                </a>
    
            </li>
            <li th:class="${contactsPage.number + 1 ge contactsPage.totalPages} ? 'disabled' : ''">
                <a th:if="${not contactsPage.lastPage}" th:href="@{${'/contacts'}(page=${contactsPage.number+1},size=${contactsPage.size})}">Next</a>
                <a th:if="${contactsPage.lastPage}" href="javascript:void(0);">Next</a>
            </li>
        </ul>
    </div>
    
    0 讨论(0)
提交回复
热议问题