搜尋
首頁web前端Bootstrap教程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
將引導程序集成到React:實用指南將引導程序集成到React:實用指南Apr 25, 2025 am 12:04 AM

將Bootstrap集成到React項目中的步驟包括:1.安裝Bootstrap包,2.導入CSS文件,3.使用Bootstrap類名樣式化元素,4.使用React-Bootstrap或reactstrap庫來使用Bootstrap的JavaScript組件。這種集成利用React的組件化和Bootstrap的樣式系統,實現高效的UI開發。

Bootstrap是用什麼?一個實用的解釋Bootstrap是用什麼?一個實用的解釋Apr 24, 2025 am 12:16 AM

bootstrapisapowerfulflameworkthatsimplifiesCreatingingResponsive,移動 - firstwebsites.itoffers.itoffers:1)AgridSystemforadaptableBableLayouts,2)2)pre-styledlementslikeButtonslikeButtonSandForms和3)JavaScriptCompriptcomponcomponentsSuchcaroSelSuselforEnhanceSuch forenhanceTinteractivity。

引導程序:從佈局到組件引導程序:從佈局到組件Apr 23, 2025 am 12:06 AM

Bootstrap是一個由Twitter開發的前端框架,集成了HTML、CSS和JavaScript,幫助開發者快速構建響應式網站。其核心功能包括:柵格系統與佈局:基於12列的設計,使用flexbox佈局,支持不同設備尺寸的響應式頁面。組件與樣式:提供豐富的組件庫,如按鈕、模態框等,通過添加類名即可實現美觀效果。工作原理:依賴CSS和JavaScript,CSS使用LESS或SASS預處理器,JavaScript依賴jQuery,實現交互和動態效果。通過這些功能,Bootstrap大大提升了開發

什麼是bootstrap?初學者的介紹什麼是bootstrap?初學者的介紹Apr 22, 2025 am 12:07 AM

BootstrapisafreeCSSframeworkthatsimplifieswebdevelopmentbyprovidingpre-styledcomponentsandJavaScriptplugins.It'sidealforcreatingresponsive,mobile-firstwebsites,offeringaflexiblegridsystemforlayoutsandasupportivecommunityforlearningandcustomization.

Bootstrap Demystified:一個簡單的解釋Bootstrap Demystified:一個簡單的解釋Apr 21, 2025 am 12:13 AM

Bootstrapisafree,open-sourceCSSframeworkthathelpscreateresponsive,mobile-firstwebsites.1)Itoffersagridsystemforlayoutflexibility,2)includespre-styledcomponentsforquickdesign,and3)ishighlycustomizabletoavoidgenericlooks,butrequiresunderstandingCSStoop

引導與反應:選擇正確的方法引導與反應:選擇正確的方法Apr 20, 2025 am 12:09 AM

Bootstrap適合快速搭建和小型項目,而React適合複雜的、交互性強的應用。 1)Bootstrap提供預定義的CSS和JavaScript組件,簡化響應式界面開發。 2)React通過組件化開發和虛擬DOM,提升性能和交互性。

Bootstrap的目的:建立一致且有吸引力的網站Bootstrap的目的:建立一致且有吸引力的網站Apr 19, 2025 am 12:07 AM

Bootstrap的主要用途是幫助開發者快速構建響應式、移動優先的網站。其核心功能包括:1.響應式設計,通過網格系統實現不同設備的佈局調整;2.預定義組件,如導航欄和模態框,確保美觀和跨瀏覽器兼容性;3.支持自定義和擴展,使用Sass變量和mixins調整樣式。

Bootstrap與其他框架:比較概述Bootstrap與其他框架:比較概述Apr 18, 2025 am 12:06 AM

Bootstrap優於TailwindCSS、Foundation和Bulma,因為它易用且快速開發響應式網站。 1.Bootstrap提供豐富的預定義樣式和組件庫。 2.其CSS和JavaScript庫支持響應式設計和交互功能。 3.適合快速開發,但自定義樣式可能較複雜。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1

記事本++7.3.1

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