在进行项目时,用到了bootstrap table的父子表,现在记录下使用过程中遇到的坑。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BootStrap Table使用</title>
<!-- 1、Jquery组件引用 -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- 2、bootstrap组件引用 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<!-- 3、bootstrap table组件以及中文包的引用 -->
<script src="js/bootstrap-table.js"></script>
<link href="css/bootstrap-table.css" rel="stylesheet" />
<script src="js/bootstrap-table-zh-CN.js"></script>
<!-- 导出 -->
<script src="js/bootstrap-table-export.min.js"></script>
<script src="js/tableExport.js"></script>
</head>
<body>
<div class="panel-body" style="padding-bottom:0px;">
<div class="panel panel-default">
<div class="panel-heading">查询条件</div>
<div class="panel-body">
<form id="formSearch" class="form-horizontal">
<div class="form-group" style="margin-top:15px">
<label class="control-label col-sm-1" for="txt_search_departmentname">部门名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="txt_search_departmentname">
</div>
<label class="control-label col-sm-1" for="txt_search_statu">状态</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="txt_search_statu">
</div>
<div class="col-sm-4" style="text-align:left;">
<button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary">查询</button>
</div>
</div>
</form>
</div>
</div>
<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>
<button id="btn_edit" type="button" class="btn btn-default">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
</button>
<button id="btn_delete" type="button" class="btn btn-default">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
</button>
</div>
<table id="tb_departments"></table>
</div>
</body>
<script>
$(function () {
//1.初始化Table
var oTable = new TableInit();
oTable.Init();
//2.初始化Button的点击事件
var oButtonInit = new ButtonInit();
oButtonInit.Init();
});
var TableInit = function () {
var oTableInit = new Object();
//初始化Table
oTableInit.Init = function () {
$('#tb_departments').bootstrapTable({
url: '/zph/getZphInfo', //请求后台的URL(*)
method: 'get', //请求方式(*)
toolbar: '#toolbar', //工具按钮用哪个容器
striped: true, //是否显示行间隔色
cache: false, //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
pagination: true, //是否显示分页(*)
sortable: false, //是否启用排序
sortOrder: "asc", //排序方式
//queryParams: oTableInit.queryParams,//传递参数(*)
sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*)
pageNumber:1, //初始化加载第一页,默认第一页
pageSize: 10, //每页的记录行数(*)
pageList: [10, 25, 50, 100], //可供选择的每页的行数(*)
search: true, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
strictSearch: true,
showColumns: true, //是否显示所有的列
showRefresh: true, //是否显示刷新按钮
minimumCountColumns: 2, //最少允许的列数
clickToSelect: true, //是否启用点击选中行
//height: 500, //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
uniqueId: "id", //每一行的唯一标识,一般为主键列
showToggle:true, //是否显示详细视图和列表视图的切换按钮
cardView: false, //是否显示详细视图
detailView: true, //是否显示父子表
showExport: true, //是否显示导出
exportDataType: "basic", //basic', 'all', 'selected'.
columns: [{
checkbox: true
}, {
field: 'id',
title: '部门名称'
}, {
field: 'title',
title: '上级部门'
}, {
field: 'time',
title: '部门级别'
} ],
//注册加载子表的事件。注意下这里的三个参数!
onExpandRow: function (index, row, $detail) {
oTableInit.InitSubTable(index, row, $detail);
}
});
};
//初始化子表格(无线循环)
oTableInit.InitSubTable = function (index, row, $detail) {
var parentid = row.id;
var cur_table = $detail.html('<table></table>').find('table');
$(cur_table).bootstrapTable({
url: '/zph/getZphInfobyid',
method: 'get',
contentType: 'application/json;charset=UTF-8',//这里我就加了个utf-8
dataType: 'json',
queryParams: { id: parentid },
ajaxOptions: { id: parentid },
clickToSelect: true,
//height: 500,
detailView: false,//父子表
uniqueId: "id",
pageSize: 10,
pageList: [10, 25],
columns: [{
checkbox: true
}, {
field: 'id',
title: '菜单名称'
}, {
field: 'title',
title: '菜单URL'
}, {
field: 'time',
title: '父级菜单'
} ],
//无线循环取子表,直到子表里面没有记录
onExpandRow: function (index, row, $Subdetail) {
oInit.InitSubTable(index, row, $Subdetail);
}
});
};
//得到查询的参数
/* oTableInit.queryParams = function (params) {
var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
limit: params.limit, //页面大小
offset: params.offset, //页码
departmentname: $("#txt_search_departmentname").val(),
statu: $("#txt_search_statu").val()
};
return temp;
}; */
return oTableInit;
};
var ButtonInit = function () {
var oInit = new Object();
var postdata = {};
oInit.Init = function () {
//初始化页面上面的按钮事件
};
return oInit;
};
</script>
</html>
在进行url后台取值的时候,记住返回值必须得是:json数组!json数组!json数组!(重要的事情说三遍!)
后台部分代码:
@RequestMapping("getZphInfo")
@ResponseBody
public List<ZphInfo> getZphInfo(Model model) {
List<ZphInfo> zphinfolist = new ArrayList<ZphInfo>();
zphinfolist = zphservice.getZphInfoList();
model.addAttribute("zphinfolist", zphinfolist);
for (ZphInfo zphInfo : zphinfolist) {
logger.info(zphInfo.toString());
}
return zphinfolist;
}
@RequestMapping("getZphInfobyid")
@ResponseBody
public List<ZphInfo> getZphInfoById(int id) {
//int zphid = Integer.parseInt(id);
ZphInfo zphinfo = new ZphInfo();
if(null == zphservice.getZphInfoById(id)) {
logger.info("未查找到信息!");
return null;
}
List<ZphInfo> zpList = new ArrayList<ZphInfo>();
zphinfo = zphservice.getZphInfoById(id);
zpList.add(zphinfo);
return zpList;
}
效果图: