首頁 web前端 Bootstrap教程 bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

Aug 20, 2019 pm 01:54 PM
bootstrap table

bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

  關於分頁,之前一直純手寫js程式碼來實現,最近又需要用到分頁,找了很多最終確定bootstrap-table,正好前端頁面用的是bootstrap。以下就來為大家介紹bootstrap-table如何實現分頁及自訂搜尋框。

推薦教學:bootstrap教學

bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

#先下載BootStrap-table的js與CSS

下載網址:https://github.com/wenzhixin/bootstrap-table.git

##下載完後解壓縮

bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

#把bootstrap-table.js、bootstrap-table-zh-CN.js、bootstrap-table.css複製到專案中

bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用

#在jsp中引入js和css

<link href="../css/bootstrap-table.css" rel="stylesheet">
<script src="../js/bootstrap-table.js"></script>
<script src="../js/bootstrap-table-zh-CN.js"></script>
登入後複製

其他bootstrap相關檔案和jQuery相關檔案自行引入即可

#先上一段jsp中程式碼

<div class="panel">
    <div class="panel-body" style="padding-bottom: 1px;">
        <form class="form-horizontal">
            <div class="form-group">
                <div class="col-sm-3">
                    <!-- 自定义搜索框 -->
                    <input type="text" name="searchString" id="searchString_id" class="form-control" placeholder="请输入卡号" onkeydown="javascript:if(event.keyCode==13) searchId();" />
                </div>
                <div class="col-sm-1">
                    <button type="button" class="btn btn-primary btn-w-m" id="queryBtn">
                        <span class="glyphicon glyphicon-search"></span> 搜索
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="ibox-content">
    <table id="myTable"></table>
</div>
登入後複製

 再看js程式碼

$(document).ready(function () {
  //调用函数,初始化表格
    initTable();
  //当点击查询按钮的时候执行,bootstrap-table前端分页是不能使用搜索功能,所以可以提取出来自定义搜索。后台代码,在后面给出
    $("#queryBtn").bind("click", initTable);
});
function initTable() {
    //先销毁表格
    $(&#39;#myTable&#39;).bootstrapTable(&#39;destroy&#39;);
    $(&#39;#myTable&#39;).bootstrapTable({
        url: "showConsumeRecordlList",//请求后台的URL(*)
        method: &#39;get&#39;,
        dataType: "json",
        dataField: &#39;rows&#39;,
        striped: true,//设置为 true 会有隔行变色效果
        undefinedText: "空",//当数据为 undefined 时显示的字符
        pagination: true, //设置为 true 会在表格底部显示分页条。
        showToggle: "true",//是否显示 切换试图(table/card)按钮
        showColumns: "true",//是否显示 内容列下拉框
        pageNumber: 1,//初始化加载第一页,默认第一页
        pageSize: 10,//每页的记录行数(*)
        pageList: [10, 20, 30, 40],//可供选择的每页的行数(*),当记录条数大于最小可选择条数时才会出现
        paginationPreText: &#39;上一页&#39;,
        paginationNextText: &#39;下一页&#39;,
        search: false, //是否显示表格搜索,bootstrap-table服务器分页不能使用搜索功能,可以自定义搜索框,上面jsp中已经给出,操作方法也已经给出
        striped : true,//隔行变色
        showColumns: false,//是否显示 内容列下拉框
        showToggle: false, //是否显示详细视图和列表视图的切换按钮
        clickToSelect: true,  //是否启用点击选中行
        data_local: "zh-US",//表格汉化
        sidePagination: "server", //服务端处理分页
        queryParamsType : "limit",//设置为 ‘limit’ 则会发送符合 RESTFul 格式的参数.
        queryParams: function (params) {//自定义参数,这里的参数是传给后台的,我这是是分页用的
//            请求服务器数据时,你可以通过重写参数的方式添加一些额外的参数,例如 toolbar 中的参数 如果
//       queryParamsType = &#39;limit&#39; ,返回参数必须包含limit, offset, search, sort, order 
//            queryParamsType = &#39;undefined&#39;, 返回参数必须包含: pageSize, pageNumber, searchText, sortName, sortOrder. 
//            返回false将会终止请求。
            return {//这里的params是table提供的
                offset: params.offset,//从数据库第几条记录开始
                limit: params.limit,//找多少条
                memberId: $("#searchString_id").val() //这个就是搜索框中的内容,可以自动传到后台,搜索实现在xml中体现
            };
        },
        responseHandler: function (res) {
      //如果后台返回的json格式不是{rows:[{...},{...}],total:100},可以在这块处理成这样的格式
      return res;
        },
        columns: [{
            field: &#39;xuhao&#39;,
            title: &#39;序号&#39;,
            formatter: idFormatter
        }, {
            field: &#39;memberId&#39;,
            title: &#39;会员卡号&#39;,
        }, {
            field: &#39;name&#39;,
            title: &#39;会员姓名&#39;
        }, {
            field: &#39;payTime&#39;,
            title: &#39;缴费时间&#39;,
            formatter: timeFormatter
        }, {
            field: &#39;payNo&#39;,
            title: &#39;缴费单号&#39;
        }, {
            field: &#39;payEntry&#39;,
            title: &#39;收款条目&#39;,
            formatter: payEntryFormatter
        }, {
            field: &#39;cardType&#39;,
            title: &#39;卡种&#39;,
            formatter: cardTypeFormatter
        }, {
            field: &#39;payMoney&#39;,
            title: &#39;缴费金额&#39;
        }, {
            field: &#39;operate&#39;,
            title: &#39;缴费详情&#39;,
            formatter: operateFormatter
        } ],
        onLoadSuccess: function () {
        },
        onLoadError: function () {
            showTips("数据加载失败!");
        }
    });
}
function idFormatter(value, row, index){
    return index+1;
}
function timeFormatter(value, row, index) {
    if (value != null) {
        var date = new Date(dateTime);
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
    }
}
function payEntryFormatter(value, row, index){
    switch(row.payEntry){
    case &#39;1&#39;:
        value=&#39;缴费种类1&#39;;
        break;
    case &#39;2&#39;:
        value=&#39;缴费种类2&#39;;
        break;
    case &#39;3&#39;:
        value=&#39;缴费种类3&#39;;
        break;
    default:
        value=&#39;其他&#39;;
        break;
    }
    return value;
}
function cardTypeFormatter(value, row, index) {
    switch(row.cardType){
    case &#39;1&#39;:
        value=&#39;卡种1&#39;;
        break;
    case &#39;2&#39;:
        value=&#39;卡种2&#39;;
        break;
    case &#39;3&#39;:
        value=&#39;卡种3&#39;;
        break;
    default:
        value=&#39;其他&#39;;
        break;
    }
    return value;
}
function operateFormatter(value, row, index) {
      return &#39;<button  type="button" onClick="showConsumeRecord(&#39;+id+&#39;)"  class="btn btn-xs btn-primary" data-toggle="modal" data-target="#consumeModal">查看</button>&#39;;
}
登入後複製

前段準備就緒,開始伺服器程式碼

準備分頁實體

package com.gym.utils;

public class Page {
    // 每页显示数量
    private int limit;
    // 页码
    private int page;
    // sql语句起始索引
    private int offset;
    // setter and getter....
}
登入後複製

#準備展示實體

import java.util.Date;
import com.gym.utils.Page;

public class ConsumeRecord extends Page {
    private Integer id;
    private Integer memberId;
    private String months;
    private Long payMoney;
    private Date payTime;
    private String payStatus;
    private String payEntry;
    private String remark;
    private String name;
    private String cardType;
    private Date endTime;
    private Date registerTime;
    private String payNo;
    // setter and getter...
}
登入後複製

再來一個分頁幫助類別

import java.util.ArrayList;
import java.util.List;

public class PageHelper<T> {
    // 注意:这两个属性名称不能改变,是定死的
    // 实体类集合
    private List<T> rows = new ArrayList<T>();
    // 数据总条数
    private int total;
    // setter and getter...
}
登入後複製

寫Controller

/**
     * 展示缴费详情列表
     * 
     * @param modelMap
     * @return
     */
    @RequestMapping("/showConsumeRecordlListA")
    @ResponseBody
    public String showConsumeRecordlListA(ConsumeRecord consumeRecord, HttpServletRequest request) {
        PageHelper<ConsumeRecord> pageHelper = new PageHelper<ConsumeRecord>();
        // 统计总记录数
        Integer total = consumerRecordService.getTotal(consumeRecord);
        pageHelper.setTotal(total);

        // 查询当前页实体对象
        List<ConsumeRecord> list = consumerRecordService.getConsumerRecordListPage(consumeRecord);
        pageHelper.setRows(list);

        return new GsonBuilder().serializeNulls().create().toJson(pageHelper);
    }
登入後複製

經過Service層,這塊就不貼上了,直接到達mapper

import java.util.List;
import com.entity.ConsumeRecord;

public interface ConsumeRecordMapper {
    ...
    ...
    /**
     * 获取消费记录条数
     * 
     * @param consumeRecord
     * @return
     */
    Integer getTotal(ConsumeRecord consumeRecord);

    /**
     * 分页查询消费记录集合
     * 
     * @param consumeRecord
     * @return
     */
    List<ConsumeRecord> getConsumerRecordListPage(ConsumeRecord consumeRecord);
}
登入後複製

然後mapper .xml

<!-- 查询符合条件的缴费总条数 -->
    <select id="getTotal" parameterType="com.entity.ConsumeRecord" resultType="int">
        SELECT count(1) FROM consume_record where 1=1  
        <if test="memberId != null and memberId != &#39;&#39;">
            and member_id=#{memberId}
          </if> 
    </select>
    <!-- 查询符合条件的缴费信息集合 -->
    <select id="getConsumerRecordListPage" parameterType="com.entity.ConsumeRecord" resultMap="BaseResultMap">
        SELECT * FROM consume_record  where 1=1  
        <if test="memberId != null and memberId != &#39;&#39;">
            and member_id=#{memberId}
          </if> 
          ORDER BY pay_time DESC
        LIMIT #{offset},#{limit}
    </select>
登入後複製

這是bootstrap-table官方文檔,主要解釋參數的意思,可根據文檔依照自己的需求更改程式碼

以上是bootStrap-table伺服器端後台分頁及自訂搜尋框的實作的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

bootstrap搜索欄怎麼獲取 bootstrap搜索欄怎麼獲取 Apr 07, 2025 pm 03:33 PM

如何使用 Bootstrap 獲取搜索欄的值:確定搜索欄的 ID 或名稱。使用 JavaScript 獲取 DOM 元素。獲取元素的值。執行所需的操作。

vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

bootstrap怎麼寫分割線 bootstrap怎麼寫分割線 Apr 07, 2025 pm 03:12 PM

創建 Bootstrap 分割線有兩種方法:使用 標籤,可創建水平分割線。使用 CSS border 屬性,可創建自定義樣式的分割線。

bootstrap垂直居中怎麼弄 bootstrap垂直居中怎麼弄 Apr 07, 2025 pm 03:21 PM

使用 Bootstrap 實現垂直居中:flexbox 法:使用 d-flex、justify-content-center 和 align-items-center 類,將元素置於 flexbox 容器內。 align-items-center 類法:對於不支持 flexbox 的瀏覽器,使用 align-items-center 類,前提是父元素具有已定義的高度。

bootstrap怎麼調整大小 bootstrap怎麼調整大小 Apr 07, 2025 pm 03:18 PM

要調整 Bootstrap 中元素大小,可以使用尺寸類,具體包括:調整寬度:.col-、.w-、.mw-調整高度:.h-、.min-h-、.max-h-

bootstrap怎麼設置框架 bootstrap怎麼設置框架 Apr 07, 2025 pm 03:27 PM

要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。

bootstrap怎麼插入圖片 bootstrap怎麼插入圖片 Apr 07, 2025 pm 03:30 PM

在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

Bootstrap Table使用AJAX獲取數據出現亂碼怎麼辦 Bootstrap Table使用AJAX獲取數據出現亂碼怎麼辦 Apr 07, 2025 am 11:54 AM

使用AJAX從服務器獲取數據時Bootstrap Table出現亂碼的解決方法:1. 設置服務器端代碼的正確字符編碼(如UTF-8)。 2. 在AJAX請求中設置請求頭,指定接受的字符編碼(Accept-Charset)。 3. 使用Bootstrap Table的"unescape"轉換器將已轉義的HTML實體解碼為原始字符。

See all articles