【发布时间】:2016-01-24 22:21:33
【问题描述】:
我正在使用这个bootstrap-table。我想将一张表嵌套到另一张表中。
$(document).ready(function() {
var t = [{
"id": "1",
"name": "john",
}, {
"id": "2",
"name": "ted"
}];
//TABLE 1
$('#summaryTable').bootstrapTable({
data: t,
detailFormatter: detailFormatter
});
function detailFormatter(index, row, element) {
$(element).html(row.name);
$(element).html(function() {
//I was thinking of putting the code here, but I don't know how to do it so it will work,
//except javascript, it needs to be put this html code <table id="detailTable"></table>
});
// return [
// ].join('');
}
//TABLE 2
var t2 = [{
"id": "1",
"shopping": "bike",
}, {
"id": "2",
"shopping": "car"
}];
$('#detailTable').bootstrapTable({
data: t2,
columns: [{
field: 'id',
title: 'id'
}, {
field: 'shopping',
title: 'Shopping detail'
}, {
field: 'status',
checkbox: true
}],
checkboxHeader: false
});
$('#detailTable').on('check.bs.table', function(e, row, $el) {
alert('check index: ' + row.shopping);
});
$('#detailTable').on('uncheck.bs.table', function(e, row, $el) {
alert('uncheck index: ' + row.shopping);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.0/bootstrap-table.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.10.0/bootstrap-table.min.js"></script>
<div class="container">
<p>TABLE 1</p>
<table id="summaryTable" data-detail-view="true">
<thead>
<tr>
<th data-field="id" data-align="center" data-width="20px">id</th>
<th data-field="name" data-align="center">Name</th>
</tr>
</thead>
</table>
<br>
<p>TABLE 2</p>
<table id="detailTable"></table>
</div>
这是我的演示链接:fiddle
有两张桌子。表 1 具有可扩展的行,我想将表 2 放入表 1 的每一行。有一个名为 detailFormatter 的函数可以执行此操作,您可以在其中返回字符串或渲染元素(我正在使用它,但我不能让它工作)。对于这个演示,表 1 是使用一些 html 代码创建的,而表 2 仅使用 javascript,但是有这个启动 div <table id="detailTable"></table>(对我来说,采用哪种方式并不重要)。 所以,问题是如何将表 2 放入表 1 中,并有可能使用其事件(如选中或取消选中复选框)作为 table2 的演示显示?稍后,我想使用此事件onExpandRow 所以当用户展开表 1 的行时,它将从数据库中获取数据并填写表 2。
【问题讨论】:
标签: javascript jquery twitter-bootstrap-3 bootstrap-table