Bootstrap Table使用心得总结


Posted in Javascript onNovember 29, 2016

之前一直在调研我们的管理后台使用的表格控件,查询到 : http://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: "http://ydjr.dev.chengyiwm.com/goldman-mgr/listProduct",
 sortName: "prodId", //排序列 
 striped: true, //?l?行 
 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配置完成后给大家看看我们的显示效果:

Bootstrap Table使用心得总结

如果大家还想深入学习,可以点击这里进行学习,再为大家附3个精彩的专题:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
在标题栏显示新消息提示,很多公司项目中用到这个方法
Nov 04 Javascript
深入了解javascript中的prototype与继承
Apr 14 Javascript
JavaScript加入收藏夹功能(兼容IE、firefox、chrome)
May 05 Javascript
基于js里调用函数时,函数名带括号和不带括号的区别
Jul 28 Javascript
微信小程序-getUserInfo回调的实例详解
Oct 27 Javascript
浅析Visual Studio Code断点调试Vue
Feb 27 Javascript
vue 中swiper的使用教程
May 22 Javascript
jQuery实现简单复制json对象和json对象集合操作示例
Jul 09 jQuery
基于D3.js实现时钟效果
Jul 17 Javascript
vue在路由中验证token是否存在的简单实现
Nov 11 Javascript
vscode 配置vue+vetur+eslint+prettier自动格式化功能
Mar 23 Javascript
小谈angular ng deploy的实现
Apr 07 Javascript
jQuery将表单序列化成一个Object对象的实例
Nov 29 #Javascript
利用CSS、JavaScript及Ajax实现图片预加载的方法
Nov 29 #Javascript
jQuery序列化表单成对象的简单实现
Nov 29 #Javascript
JS判断输入的字符串是否是数字的方法(正则表达式)
Nov 29 #Javascript
JS访问DOM节点方法详解
Nov 29 #Javascript
基于Node.js + WebSocket打造即时聊天程序嗨聊
Nov 29 #Javascript
浅谈js函数的多种定义方法与区别
Nov 29 #Javascript
You might like
多php服务器实现多session并发运行
2006/10/09 PHP
基于Snoopy的PHP近似完美获取网站编码的代码
2011/10/23 PHP
浅析PHP安装扩展mcrypt以及相关依赖项(PHP安装PECL扩展的方法)
2013/07/05 PHP
ThinkPHP使用Smarty第三方插件方法小结
2016/03/19 PHP
golang实现php里的serialize()和unserialize()序列和反序列方法详解
2018/10/30 PHP
jQuery 相关控件的事件操作分解
2009/08/03 Javascript
jquery 模式对话框终极版实现代码
2009/09/28 Javascript
jquery 操作日期、星期、元素的追加的实现代码
2012/02/07 Javascript
Js 获取Gridview选中行的内容操作步骤
2013/02/05 Javascript
jQuery如何实现点击页面获得当前点击元素的id或其他信息
2014/01/09 Javascript
JS 打印功能代码可实现打印预览、打印设置等
2014/10/31 Javascript
详解js私有作用域中创建特权方法
2016/01/25 Javascript
微信小程序 触控事件详细介绍
2016/10/17 Javascript
微信小程序实现图片放大预览功能
2020/10/22 Javascript
vue-cli项目代理proxyTable配置exclude的方法
2018/09/20 Javascript
JavaScript使用类似break机制中断forEach循环的方法
2018/11/13 Javascript
简单两步使用node发送qq邮件的方法
2019/03/01 Javascript
Vue Echarts实现可视化世界地图代码实例
2019/05/07 Javascript
Vue中多元素过渡特效的解决方案
2020/02/05 Javascript
[35:26]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第三局
2016/02/26 DOTA
[01:07:17]EG vs Optic Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
[00:52]玛尔斯技能全介绍
2019/03/06 DOTA
Python subprocess模块学习总结
2014/03/13 Python
深入理解Python中命名空间的查找规则LEGB
2015/08/06 Python
教你学会使用Python正则表达式
2017/09/07 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
2018/12/07 Python
利用PyTorch实现VGG16教程
2020/06/24 Python
Python实现对word文档添加密码去除密码的示例代码
2020/12/29 Python
UGG澳洲官网:UGG Australia
2018/04/26 全球购物
三星法国官方网站:Samsung法国
2019/10/31 全球购物
网络工程师面试(三木通信技术有限公司)
2013/06/05 面试题
教研处工作方案
2014/05/26 职场文书
个人承诺书格式
2014/06/03 职场文书
先进学校事迹材料
2014/12/30 职场文书
2015年高二班主任工作总结
2015/05/25 职场文书
Python opencv缺陷检测的实现及问题解决
2021/04/24 Python