关键词:
使用bootstrap table可以很方便的开发后台表格,对数据进行异步更新,编辑。下面就来介绍一下bootstrap table的详细使用方法:
因为之前在官网也找了很久的教程,源码感觉隐藏的比较隐秘,其他扩展功能也很难找到,其实都在:http://issues.wenzhixin.net.cn/bootstrap-table/index.html这里面,点击上面的tab可以找到具体的功能实现(特别是extension和issues,之前以为issues不是教程,结果点进去才知道):
最简单的异步表单
1.html中加入如下代码:
<table id="table"></table>
- 1
2.javascript
/*bootstrap table*/
$(‘#table‘).bootstrapTable({
url:"/wadmin/permission/doRuleList",//请求数据url
queryParams: function (params) {
return {
offset: params.offset, //页码
limit: params.limit, //页面大小
search : params.search, //搜索
order : params.order, //排序
ordername : params.sort, //排序
};
},
showHeader : true,
showColumns : true,
showRefresh : true,
pagination: true,//分页
sidePagination : ‘server‘,//服务器端分页
pageNumber : 1,
pageList: [5, 10, 20, 50],//分页步进值
search: true,//显示搜索框
//表格的列
columns: [
{
field: ‘id‘,//域值
title: ‘规则ID‘,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%‘,
},
{
field: ‘name‘,//域值
title: ‘唯一英文标识‘,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘30%‘,
editable:true,
},
{
field: ‘title‘,//域值
title: ‘中文描述‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘35%‘,
editable:true,
},
{
field: ‘status‘,//域值
title: ‘状态‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘20%‘,
formatter : function (value, row, index) {
if (row[‘status‘] === 1) {
return ‘正常‘;
}
if (row[‘status‘] === 0) {
return ‘禁用‘;
}
return value;
}
}
]
});
上面代码中,除了url和下面的column需要动态变之外,其他的基本上都是固定的,可以复制粘贴到处使用。因为规则的固定,所以后台的规则也是比较固定的。
3.thinkPHP5.0实现后台(主要是返回total和rows)
controller
public function doRoleRuleList() {
$id = input("id");
if (!$id) return "";
$limit = input("limit");
$offset = input("offset");
$search = input("search");
$order = input("order", "desc");
$ordername = input("ordername");
if (!$ordername) {
$ordername = ‘id‘;
$order = ‘desc‘;
}
$perModel = new PermissionModel();
$rs = $perModel->doRoleRuleList($id, $offset, $limit, $search, $order, $ordername);
return json($rs);
}
model
public function doRoleRuleList($id, $offset, $limit, $search, $order, $ordername) {
$total = Db::connect("dbUser")->table("auth_rule")->where([‘status‘=>[‘<>‘, 0]])->count();
$auth_group = Db::connect("dbUser")->table("auth_group")->field("id,rules")->where("id", $id)->find();
$rules ="-1";
if ($auth_group && $auth_group[‘rules‘]) $rules = $auth_group[‘rules‘];
$rows = Db::connect("dbUser")->table("auth_rule")
->field([‘id‘,‘name‘,‘title‘,"if(id in ({$rules}), 1, 0)"=>‘selected‘])
->where([
‘status‘=>[‘<>‘, 0],
// ‘name|title‘ => [‘like‘, "%{$search}%"]
])
->order($ordername." ".$order)
->select();
return [‘total‘=>$total,‘rows‘=>$rows];
}
说明图:
复杂表格(行内编辑(编辑文字,下拉选择),样式改变,自定义图标,文件上传),因为js比较复杂,这里先列出代码功能的说明:
1.显示详情使用如下代码:
表格参数中添加代码
detailView : true,
detailFormatter : function (index, row) {
var image = ‘<div class="photos">‘
+‘<a target="_blank" href="‘+row[‘jumpUrl‘]+‘"><img alt="image" class="feed-photo" src="‘+row[‘picUrl‘]+‘"></a>‘
+‘</div>‘;
return image;
}
可以在detailFormatter进行代码的格式化,以字符串的形式返回即可,实现效果如下:
2.表格列内容的格式化
列参数中添加代码
formatter : function (value, row, index) {
return "<img style=‘width: 50px;height: 50px;‘ src=‘"+value+"‘ alt=‘‘>"
}
效果如下:
3 . 表格的样式自定义
列参数中添加带代码
cellStyle : function (value, row, index) {
return {
css: {
"max-width": "300px",
"word-wrap": "break-word",
"word-break": "normal"
}
};
}
4 . 新增数据
在js文件中添加如下代码:
$("#btn_add").click(function () {
$.ajax({
type : "POST",
url : "/wadmin/ad/addAd",
dataType : ‘JSON‘,
success : function (data) {
if (data.result != 0) {
toastr.info("info", data.message);
return ;
}
toastr.success("success", ‘标签‘);
$("#table").bootstrapTable(‘insertRow‘, {index:0, row:data.data});
}
});
});
绑定的按钮是toolbar里面的,点击如果进行ajax请求,再根据请求的结果自行判断添加
5.表格中列格式化成图标并监听事件
6.文件上传(基于5)
列参数中添加如下代码
{
field: ‘operate‘,
title: ‘操作‘,
align: ‘center‘,
events: operateEvents,
width : ‘25%‘,
formatter: operateFormatter
}
operateFormatter(这里注意需要添加a标签包住图标,并添加class)
function operateFormatter(value, row, index) {
return [
‘<a class="using" href="javascript:void(0)" title="Remove">‘,
‘<i class="fa fa-check"></i>‘,
‘</a> ‘,
‘<a class="upload" style="cursor: pointer" href="javascript:void(0)" title="Upload">‘,
‘<label style="cursor: pointer" for="‘+row[‘adId‘]+‘">‘,
‘<i class="fa fa-upload"></i>‘,
‘<input type="file" name="adUpload" style="display: none;" class="adUpload" id="‘+row[‘adId‘]+‘" >‘,
‘</label>‘,
‘</a> ‘,
‘<a class="remove" href="javascript:void(0)" title="Using">‘,
‘<i class="fa fa-times"></i>‘,
‘</a>‘,
].join(‘‘);
}
operateEvents(监听事件,注意这里是window.operateEvents)
window.operateEvents = {
‘click .remove‘: function (e, value, row, index) {
$.ajax({
type : "POST",
url : "/wadmin/ad/deleteAd",
data : {
adId : row[‘adId‘]
},
dataType : ‘JSON‘,
success : function (data) {
if (data.result != 0) {
toastr.info("info", data.message);
return ;
}
toastr.success("success", ‘删除成功‘);
$("#table").bootstrapTable(‘remove‘, {
field: ‘adId‘,
values: [row[‘adId‘]]
});
}
});
return false;
},
‘click .using‘: function (e, value, row, index) {
$.ajax({
type : "POST",
url : "/wadmin/ad/usingAd",
data : {
adId : row[‘adId‘]
},
dataType : ‘JSON‘,
success : function (data) {
if (data.result != 0) {
toastr.info("info", data.message);
return ;
}
toastr.success("success", ‘使用该广告‘);
$("#table").bootstrapTable(‘refresh‘);
}
});
return false;
},
‘click .upload‘: function (e, value, row, index) {
$(‘.adUpload‘).fileupload({
url : ‘/wadmin/ad/adUpload/adId/‘+row[‘adId‘],
dataType: ‘json‘,
add: function (e, data) {
data.submit();
},
done: function (e, data) {
var response = data.result;
if (response.result != 0) {
toastr.error(response.message);
return false;
}
toastr.success("上传成功");
$("#table").bootstrapTable(‘refresh‘);
}
});
return false;
}
};
实现的效果如下:
7.行内编辑
普通的编辑只需要在列参数中设置即可:
列参数中添加代码
editable : true,
- 1
需要下拉编辑的,使用如下代码:
editable: {
type: ‘select‘,
source: [ //0->无广告,1->静态不可点击,2->静态可点击,3->动态不可点击,4->动态可点击
{value: 0, text: ‘无广告‘},
{value: 1, text: ‘静态不可点击‘},
{value: 2, text: ‘静态可点击‘},
{value: 3, text: ‘动态不可点击‘},
{value: 4, text: ‘动态可点击‘},
]
}
这里还有一些行内编辑的样式是需要导入第三方lib的,比如说如果要实现行内时间的编辑,需要下载导入combodate.js
,然后添加如下代码(这些js文件后面会给出的,也可以去官网下载最新版):
editable: {
type: ‘combodate‘,
viewformat: ‘YYYY-MM-DD HH:mm:ss‘,
template: ‘YYYY-MM-DD HH:mm:ss‘,
format: ‘YYYY-MM-DD HH:mm:ss‘,
combodate: {
minuteStep: 1,
secondStep: 1,
maxYear: 5000,
minYear: 2016,
}
}
然后再表格参数中添加如下代码监听事件:
onEditableSave: function (field, row, oldValue, $el) {
$.ajax({
type: "post",
url: "/wadmin/ad/updateAdInfo",
dataType : ‘json‘,
data: row,
success: function (data, status) {
if (status == "success" && data.result == 0) {
toastr.success(‘更新成功‘);
if (field == ‘jumpUrl‘) {
$(‘#table‘).bootstrapTable("refresh");
}
return true;
} else {
toastr.info(data.message);
$(‘#table‘).bootstrapTable("refresh");
}
},
error: function () {
alert("Error");
},
complete: function () {
}
});
}
实现的效果如下:
其中,调用bootstraptable的一些方法可以动态更新表格(增删改等),用法如下:
$("#table").bootstrapTable(‘insertRow‘, {index:0, row:data.data});
$("#table").bootstrapTable(‘remove‘, {
field: ‘adId‘,
values: [row[‘adId‘]]
});
$("#table").bootstrapTable(‘refresh‘);
$("#table").bootstrapTable(‘updateCell‘, {
index : index,
field: ‘status‘,
value: row[‘status‘] ? 0 : 1
});
因为引入的js和css库有点多,而且需要实现行内编辑的话需要需要引入的js文件比较难找,下面给出下载地址(密码:jjdk):
http://pan.baidu.com/s/1bpiRObt
完整代码
html
<div id="toolbar" class="btn-group">
<button id="btn_add" type="button" class="btn btn-default">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增广告
</button>
</div>
<table id="table"></table>
javascript
/**
* Created by raid on 2016/12/28.
*/
$(function () {
window.operateEvents = {
‘click .remove‘: function (e, value, row, index) {
$.ajax({
type : "POST",
url : "/wadmin/ad/deleteAd",
data : {
adId : row[‘adId‘]
},
dataType : ‘JSON‘,
success : function (data) {
if (data.result != 0) {
toastr.info("info", data.message);
return ;
}
toastr.success("success", ‘删除成功‘);
$("#table").bootstrapTable(‘remove‘, {
field: ‘adId‘,
values: [row[‘adId‘]]
});
}
});
return false;
},
‘click .using‘: function (e, value, row, index) {
$.ajax({
type : "POST",
url : "/wadmin/ad/usingAd",
data : {
adId : row[‘adId‘]
},
dataType : ‘JSON‘,
success : function (data) {
if (data.result != 0) {
toastr.info("info", data.message);
return ;
}
toastr.success("success", ‘使用该广告‘);
$("#table").bootstrapTable(‘refresh‘);
}
});
return false;
},
‘click .upload‘: function (e, value, row, index) {
$(‘.adUpload‘).fileupload({
url : ‘/wadmin/ad/adUpload/adId/‘+row[‘adId‘],
dataType: ‘json‘,
add: function (e, data) {
data.submit();
},
done: function (e, data) {
var response = data.result;
if (response.result != 0) {
toastr.error(response.message);
return false;
}
toastr.success("上传成功");
$("#table").bootstrapTable(‘refresh‘);
}
});
return false;
}
};
/*bootstrap table*/
$(‘#table‘).bootstrapTable({
url:"/wadmin/ad/doAdList",//请求数据url
toolbar : "#toolbar",
// toolbarAlign : "right",
queryParams: function (params) {
return {
offset: params.offset, //页码
limit: params.limit, //页面大小
search : params.search, //搜索
order : params.order, //排序
ordername : params.sort, //排序
};
},
detailView : true,
detailFormatter : function (index, row) {
var image = ‘<div class="photos">‘
+‘<a target="_blank" href="‘+row[‘jumpUrl‘]+‘"><img alt="image" class="feed-photo" src="‘+row[‘picUrl‘]+‘"></a>‘
+‘</div>‘;
return image;
},
showHeader : true,
showColumns : true,
showRefresh : true,
pagination: true,//分页
sidePagination : ‘server‘,//服务器端分页
pageNumber : 1,
pageList: [5, 10, 20, 50],//分页步进值
search: true,//显示搜索框
//表格的列
columns: [
{
field: ‘adId‘,//域值
title: ‘广告ID‘,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%‘,
},
{
field: ‘picUrl‘,//域值
title: ‘图片‘,//标题
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘15%‘,
formatter : function (value, row, index) {
return "<img style=‘width: 50px;height: 50px;‘ src=‘"+value+"‘ alt=‘‘>"
}
},
{
field: ‘jumpUrl‘,//域值
title: ‘跳转链接‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%‘,
editable : true,
cellStyle : function (value, row, index) {
return {
css: {
"max-width": "300px",
"word-wrap": "break-word",
"word-break": "normal"
}
};
}
},
{
field: ‘adDesc‘,//域值
title: ‘描述‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%‘,
editable : true,
},
{
field: ‘displayType‘,//域值
title: ‘表现形式‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%‘,
editable: {
type: ‘select‘,
source: [ //0->无广告,1->静态不可点击,2->静态可点击,3->动态不可点击,4->动态可点击
{value: 0, text: ‘无广告‘},
{value: 1, text: ‘静态不可点击‘},
{value: 2, text: ‘静态可点击‘},
{value: 3, text: ‘动态不可点击‘},
{value: 4, text: ‘动态可点击‘},
]
}
},
{
field: ‘displaySeconds‘,//域值
title: ‘时间‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%‘,
editable : true,
},
{
field: ‘scope‘,//域值
title: ‘影响范围‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%‘,
editable: {
type: ‘select‘,
source: [ //0->全国
{value: 0, text: ‘全国‘},
]
}
},
{
field: ‘userType‘,//域值
title: ‘影响群体‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘5%‘,
editable: {
type: ‘select‘,
source: [ //0->全部
{value: 0, text: ‘全部‘},
]
}
},
{
field: ‘status‘,//域值
title: ‘状态‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%‘,
formatter : function (value, row, index) {
return value==1||value==‘1‘ ? ‘正在使用‘ : ‘没有使用‘;
}
},
{
field: ‘addTime‘,//域值
title: ‘时间‘,//内容
visible: true,//false表示不显示
sortable: true,//启用排序
width : ‘10%‘,
}
,{
field: ‘operate‘,
title: ‘操作‘,
align: ‘center‘,
events: operateEvents,
width : ‘25%‘,
formatter: operateFormatter
}
],
onEditableSave: function (field, row, oldValue, $el) {
$.ajax({
type: "post",
url: "/wadmin/ad/updateAdInfo",
dataType : ‘json‘,
data: row,
success: function (data, status) {
if (status == "success" && data.result == 0) {
toastr.success(‘更新成功‘);
if (field == ‘jumpUrl‘) {
$(‘#table‘).bootstrapTable("refresh");
}
return true;
} else {
toastr.info(data.message);
$(‘#table‘).bootstrapTable("refresh");
}
},
error: function () {
alert("Error");
},
complete: function () {
}
});
}
});
$("#btn_add").click(function () {
$.ajax({
type : "POST",
url : "/wadmin/ad/addAd",
dataType : ‘JSON‘,
success : function (data) {
if (data.result != 0) {
toastr.info("info", data.message);
return ;
}
toastr.success("success", ‘标签‘);
$("#table").bootstrapTable(‘insertRow‘, {index:0, row:data.data});
}
});
});
function operateFormatter(value, row, index) {
return [
‘<a class="using" href="javascript:void(0)" title="Remove">‘,
‘<i class="fa fa-check"></i>‘,
‘</a> ‘,
‘<a class="upload" style="cursor: pointer" href="javascript:void(0)" title="Upload">‘,
‘<label style="cursor: pointer" for="‘+row[‘adId‘]+‘">‘,
‘<i class="fa fa-upload"></i>‘,
‘<input type="file" name="adUpload" style="display: none;" class="adUpload" id="‘+row[‘adId‘]+‘" >‘,
‘</label>‘,
‘</a> ‘,
‘<a class="remove" href="javascript:void(0)" title="Using">‘,
‘<i class="fa fa-times"></i>‘,
‘</a>‘,
].join(‘‘);
}
});
bootstraptable使用心得总结
...的表格控件,查询到:http://bootstrap-table.wenzhixin.net.cn的BootstrapTable感觉挺不错,但是由于官方的文档不是怎么的完善,导致自己的网络数据请求一直没有通过。今天终于调试通过,在这里与大家分享一下。一、相关... 查看详情
bootstraptable的使用总结(代码片段)
...ples.bootstrap-table.com/#动态表头(function(document,window,$)//ExampleBootstrapTableLargeColumns//-------------------------------------buildTable($('#exampleTableLargeColumns'),50,50);functionbuildTable($el,cells,rows)vari,j,row,columns=[],data=[];for(i=0;i<cells;i+... 查看详情
bootstraptable使用总结之前端样式设计(代码片段)
...按钮。在实现的时候也是找了很多的资料,后头在参考了bootstraptable怎么实现自定义按钮列的操作的时候,发现了可以return一个HTML代码块到相应的table里面,就参照这个思路实现了我的想法。一:设定bootstrap样式知识点1的参考代... 查看详情
bootstraptable的使用小结
...一下,毕竟有些时候没有使用到这个方式很有用,在使用bootstraptable的时候,选择当前已经选择的节点的事件中的ID的值 当前rows中有很多的数据,但是我只需要id这一个值,这个时候进行操作就非常的简单了。$.map(data,function(... 查看详情
bootstraptable的使用小结
...一下,毕竟有些时候没有使用到这个方式很有用,在使用bootstraptable的时候,选择当前已经选择的节点的事件中的ID的值 当前rows中有很多的数据,但是我只需要id这一个值,这个时候进行操作就非常的简单了。$.map(data,function(... 查看详情
bootstraptable如何使用
最近在使用bootStrapTable的表格功能有一些自己的理解写下来分享一下主要用的是一个bootStrapTable和jquery的混合开发具体怎样引入bootStrapTable我就不一一详解了直接上代码htm代码<tableid="table0"></table>js代码var$table=$(‘#table0‘)... 查看详情
bootstraptable使用小记
varTableInit=function(){varoTableInit=newObject();//初始化TableoTableInit.Init=function(){$(‘#tb_rolelist‘).bootstrapTable({url:‘/Role/GetList‘,//请求后台的URL(*)method:‘get‘,//请求方式(*)toolbar:‘#toolbar‘,//工具按 查看详情
bootstraptable使用实例
一、bootstrapTable简单使用:<linkrel="stylesheet"href="./static/libs/bootstrap/css/bootstrap.css"><linkrel="stylesheet"href="./static/libs/bootstrap-table-master/bootstrap-table.css"><scriptsr 查看详情
bootstraptable使用实例
一、bootstrapTable简单使用:<linkrel="stylesheet"href="./static/libs/bootstrap/css/bootstrap.css"><linkrel="stylesheet"href="./static/libs/bootstrap-table-master/bootstrap-table.css"><scriptsr 查看详情
bootstraptable的使用
前言:BootstrapTable基于Bootstrap,Bootstrap基于jquery,所以需要引入jquery后再引入bootstrap。<linkhref="${ctx}/assets/plugins/bootstrap/css/bootstrap.min.css"rel="stylesheet"><linkrel="stylesheet"href="${ctx}/as 查看详情
bootstraptable列宽拖动的方法
在之前做过的一个web项目中,前端表格是基于jQuery和BootstrapTable做的,客户要求能利用拖动改变列宽,为了总结和备忘,现将实现的过程记录如下: 1、BootstrapTable可拖动,需要用到它的Resizable扩展插件,具体可见bootstrap-... 查看详情
bootstraptable使用心得
序号显示带分页信息的连续编号,在序号列添加以下格式化代码即可。{field:‘number‘,title:‘序号‘,align:‘center‘,width:45,formatter:function(value,row,index){varpageSize=$(‘#logList‘).bootstrapTable(‘getOptions‘).pageSize,pageNum=$(‘#logLi 查看详情
bootstraptable使用
bootstraptablegitaddresshttps://github.com/wenzhixin/bootstrap-table 引入文件<linkrel="stylesheet"href="../bower_components/bootstrap/dist/css/bootstrap.min.css"/><linkrel="stylesheet"href=" 查看详情
使用 BootstrapTable 添加行时使用 CellStyle
】使用BootstrapTable添加行时使用CellStyle【英文标题】:UsingCellStylewhenaddingrowswithBootstrapTable【发布时间】:2018-06-2005:19:54【问题描述】:我有一个带有按钮的引导表,用于添加新的数据行。我想在插入时为每个新行的某一列(text2... 查看详情
bootstraptable错误
异常:Cannotreadproperty‘field‘ofundefined场景:使用BootStrapTable展示数据时,控制台报错解决:给table加上thead和tbody标签 异常:使用tablednd插件时,onDrap方法不调用解决:给tr标签加id属性 查看详情
bootstraptable使用及遇到的问题
本人前端菜鸟一枚,最近使用bootstraptable实现表格,记录一下以便日后翻阅,废话不多说,先看效果图:1、首先说下要实现该效果需要添加的css样式及所需的js文件,具体下载地址就不粘贴了(因为太懒)<linkrel="stylesheet"href="css/b... 查看详情
bootstraptable使用示例及代码
http://issues.wenzhixin.net.cn/bootstrap-table/ <!DOCTYPEhtml><html><head><title>BootstrapTableExamples</title><linkrel="stylesheet"href="assets/bootstrap/css/boo 查看详情
如何让bootstraptable语言包使用
参考技术A设置如下样式是可以使表格内容居中的,没有居中的原因可能是你还设置了其他的样式(把这个样式覆盖了):.tableth,.tabletdtext-align:center;height:38px;你可以新建一个单独的html文件复制如下代码来测试一下:BootstrapTable 查看详情