分頁案例

北方有夢_hsp發表於2020-12-31

pom檔案

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hsp</groupId>
    <artifactId>bill-manager-tk</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <java.version>11</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
        </dependency>
    </dependencies>

</project>

前端

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>userList</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
    <script type="text/javascript" th:src="@{/js/My97DatePicker/WdatePicker.js}"></script>
    <script type="text/javascript" th:src="@{/js/jquery/jquery-1.10.2.min.js}"></script>
</head>
<body class="container">
<br/>
<h1>賬單列表</h1>
<br/><br/>
<div class="with:80%">
    <form class="form-inline" id="qf" th:action="@{/bill/list-page}" method="post">
        <input type="hidden" name="pageNum" id="pageNum" th:value="${page.pageNum }" />
        <input type="hidden" name="pageSize" id="pageSize" th:value="${page.pageSize }" />
        <div class="form-group">
            <label for="typeId" class="control-label">型別</label>
            <select name="typeId" id="typeId" class="form-control">
                <option value="0">全部</option>
                <option th:each="t:${types}" th:value="${t.id}" th:text="${t.name}"
                        th:selected="(${bill.typeId} == ${t.id})" ></option>
            </select>
        </div>
        <div class="form-group">
            <label for="date1" class="control-label" >開始時間</label>
            <input type="text" class="form-control" name="date1" id="date1" placeholder="開始時間" th:value="${bill.date1} ? ${#dates.format(bill.date1, 'yyyy-MM-dd')}"
                   onclick="WdatePicker()"/>
        </div>
        <div class="form-group">
            <label for="date2" class="control-label">結束時間</label>
            <input type="text" class="form-control" name="date2" id="date2" placeholder="結束時間" th:value="${bill.date2} ? ${#dates.format(bill.date2, 'yyyy-MM-dd')}"
                   onclick="WdatePicker()"/>
        </div>
        <div class="form-group">
            <input type="submit" value="查詢" class="btn btn-info" />
            &nbsp; &nbsp;
            <input type="reset" value="重置" class="btn btn-info" />
            &nbsp; &nbsp;
            <a href="/bill/toAdd" th:href="@{/bill/toAdd}" class="btn btn-info">新增</a>
        </div>
    </form>
</div>
<br/>
<div class="with:80%">
    <table class="table table-striped table-bordered">
        <thead>
        <tr>
            <th>id</th>
            <th>標題</th>
            <th>時間</th>
            <th>金額</th>
            <th>類別</th>
            <th>說明</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="b, bstatus : ${page.list}" th:style="${bstatus.odd} ? 'background-color:#A3C6C8'">
            <th scope="row" th:text="${b.id}">id</th>
            <td th:text="${b.title}">name</td>
            <td th:text="${b.billTime} ? ${#dates.format(b.billTime, 'yyyy-MM-dd')}">time</td>
            <td th:text="${b.price}">price</td>
            <td th:text="${b.typeName}">typeName</td>
            <td th:text="${b.explains}">explain</td>
            <td><a th:href="|/bill/toUpdate/${b.id}|">修改</a>
                <a th:href="|/bill/delete/${b.id}|">刪除</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<ul class="pagination">
    <li><button class="btn btn-default" id="first">第一頁</button></li>
    <li><button class="btn btn-default" id="prev">上一頁</button></li>
    <li th:each="p:${page.navigatepageNums}">
        <button class="btn btn-default" name="pn" th:text="${p}" th:disabled="(${p} ==${page.pageNum})"></button>
    </li>
    <li><button class="btn btn-default" id="next">下一頁</button></li>
    <li><button class="btn btn-default" id="last">最後頁</button></li>
</ul>
<script th:inline="javascript">
    /*<![CDATA[*/
    $(function(){
//初始化變數
        var pageNum = [[${page.pageNum}]]; //當前頁
        var pageCount = [[${page.pages}]];//最後頁
        var hasNextPage = [[${page.hasNextPage}]];//還有下一頁
        var hasPreviousPage = [[${page.hasPreviousPage}]];//還有上一頁
        $("#next").click(function(){
            $("#pageNum").val(pageNum + 1);
            $("#qf").submit();
        });
        $("#prev").click(function(){
            $("#pageNum").val(pageNum - 1);
            $("#qf").submit();
        });
        $("#first").click(function(){
            $("#pageNum").val(1);
            $("#qf").submit();
        });
        $("#last").click(function(){
            $("#pageNum").val(pageCount);
            $("#qf").submit();
        });
//沒有上一頁
        if (!hasPreviousPage) {
            $("#prev").prop("disabled", true);
            $("#first").prop("disabled", true);
        };
        //沒有下一頁
        if (!hasNextPage) {
            $("#next").prop("disabled", true);
            $("#last").prop("disabled", true);
        };
        $("button[name='pn']").click(function(){
            $("#pageNum").val($(this).html());
            $("#qf").submit();
        });
    });
    /*]]>*/
</script>
</body>
</html>

後端

@RequestMapping("/list-page")
    public String listPage(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "3") int pageSize, bills b, ModelMap map){
        List<billType> types = typeService.list();
        map.addAttribute("types", types);
        PageInfo<bills> pageInfo = billsService.listPage(b, pageNum, pageSize);
        map.addAttribute("page", pageInfo);
        map.addAttribute("bill", b);
        return "/bill/list-page";
}

相關文章