先前一直在調研管理後台要用的表格控制項,查到 https://bootstrap-table.wenzhixin.net.cn 的 Bootstrap Table,感覺相當不錯。不過由於官方文件不夠完善,導致自己的網路資料請求一直無法通過。
今天終於調試成功,在這裡與大家分享一下。
一、相關設定檔案的引入
<!-- jQuery文件。務必在bootstrap.min.js 之前引入 --> <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <!-- bootstrap table --> <link href="//cdn.bootcss.com/bootstrap-table/1.11.0/bootstrap-table.min.css" rel="stylesheet"> <script src="//cdn.bootcss.com/bootstrap-table/1.11.0/bootstrap-table.min.js"></script> <script src="//cdn.bootcss.com/bootstrap-table/1.11.0/bootstrap-table-locale-all.js"></script> <script src="//cdn.bootcss.com/bootstrap-table/1.11.0/extensions/export/bootstrap-table-export.min.js"></script> <!-- bootstrap table 包含excel導出,pdf導出 --> <script src="https://rawgit.com/hhurz/tableExport.jquery.plugin/master/tableExport.js"></script> <script src="//cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
注意!!!!! 這裡的 tableExport.js 並不是 bootcdn 上的 tableExport,使用時要留意作者,否則會導致無法導出 excel。
二、編寫表頭與工具列
其實整個表頭的編寫非常簡單,只需幾個設定即可。
注意,把每個 bean 的屬性寫在 th 中;並注意綁定工具列。
可參考如下設定:
<!-- 工具欄的按鈕,可以自定義事件 --> <div id="toolbar" class="btn-group"> <button type="button" class="btn btn-default"> <i class="glyphicon glyphicon-plus"></i> </button> <button type="button" class="btn btn-default"> <i class="glyphicon glyphicon-heart"></i> </button> <button type="button" class="btn btn-default"> <i class="glyphicon glyphicon-trash"></i> </button> </div> <table id="demo" class="table table-striped table-hover table-bordered" data-toolbar="#toolbar" // 這裡必須綁定工具欄,不然布局會錯亂 data-search="true" data-show-refresh="true" data-show-columns="true" data-show-export="true" data-export-types="['excel']" data-export-options='{ // 導出的文件名 "fileName": "products", "worksheetName": "products" }' > <thead> <tr> <th width="3%" data-field="prodId">產品Id</th> <th width="10%" data-field="nameOfProduct">產品名稱</th> <th width="4%" data-field="categoryId">產品類別</th> <th width="5%" data-field="domicileOfCapital">資本類型</th> <th width="8%" data-field="underwriter">發行機構</th> <th width="6%" data-field="managementInstitution">基金公司</th> <th width="5%" data-field="managementInstitution2">管理機構</th> <th width="3%" data-field="flag">角標</th> <th width="7%" data-field="beginTime">上線時間</th> <th width="7%" data-field="endTime">下線時間</th> <th width="4%" data-field="status">發布狀態</th> <th width="4%" data-field="fundRaisingStatus">募集狀態</th> <th width="3%" data-field="totalScore">打分</th> <th width="3%" data-field="modesOfGuaranteeScore">擔保</th> <th width="3%" data-field="invsetmentTargetScore">投資</th> <th width="3%" data-field="underwriterScore">發行</th> <th width="3%" data-field="sourceOfPaymentScore">還款</th> <th width="3%" data-field="issuerDescriptionScore">融資</th> <th width="10%">操作</th> </tr> </thead> </table> 三、綁定後端邏輯
因為 Bootstrap Table 預設採用 form 表單方式提交,其分頁參數與查詢參數和我們的後端邏輯協議不一致(官方正好缺少這部分的文件),所以我們需要依其協議做一個自訂設定。
$(function() { $("#demo").bootstrapTable({ url: "https://ydjr.dev.chengyiwm.com/goldman-mgr/listProduct", sortName: "prodId", //排序列 striped: true, //條紋行 sidePagination: "server", //服務器分頁 clickToSelect: true, //選擇行即選擇checkbox singleSelect: true, //僅允許單選 searchOnEnterKey: true, //ENTER鍵搜索 pagination: true, //啟用分頁 escape: true, //過濾危險字符 queryParams: getParams, //攜帶參數 method: "post", //請求格式 responseHandler: responseHandler, }); }); /** * 默認加載時攜帶參數 * * 將自帶的param參數轉化到cy的請求邏輯協議 */ function getParams(params) { var query = $("#searchKey").val(); console.log(JSON.stringify(params)); return { head: { userId: "11154", skey: "6FC19FCE5D8DCF130954D8AE2CADB30A", platform: "pc", imei: "", appVersion: "", cityId: "", platformVersion: "", deviceId: "", channel: "", protoVersion: 1, isPreview: 2 }, body: { 'query': params.search, // 搜索參數 'start': params.offset, // 分頁開始位置 'pageSize': params.limit, //每頁多少條 } } } /** * 獲取返回的數據的時候做相應處理,讓bootstrap table認識我們的返回格式 * @param {Object} res */ function responseHandler(res) { return { "rows": res.body.listProduct, // 具體每一個bean的列表 "total": res.body.totalCount // 總共有多少條返回數據 } } Ok,設定完成後給大家看看我們的顯示效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。