在 GitHub 上檢視
用法
Bootstrap Table 可以通過數據屬性或 JavaScript 將數據以表格形式展示。
本頁目錄
通過數據屬性
<table data-toggle="table">
<thead>
<tr>
<th>項目 ID</th>
<th>項目名稱</th>
<th>項目價格</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>項目 1</td>
<td>$1</td>
</tr>
<tr>
<td>2</td>
<td>項目 2</td>
<td>$2</td>
</tr>
</tbody>
</table>
我們也可以通過在普通表格上設置 data-url="data1.json" 來使用遠程 URL 數據。
<table
data-toggle="table"
data-url="data1.json">
<thead>
<tr>
<th data-field="id">項目 ID</th>
<th data-field="name">項目名稱</th>
<th data-field="price">項目價格</th>
</tr>
</thead>
</table>
您還可以像下面的表格一樣,向表格添加 pagination、search 和 sortable。
<table
data-toggle="table"
data-url="data1.json"
data-pagination="true"
data-search="true">
<thead>
<tr>
<th data-sortable="true" data-field="id">項目 ID</th>
<th data-field="name">項目名稱</th>
<th data-field="price">項目價格</th>
</tr>
</thead>
</table>
通過 JavaScript
通過 JavaScript 調用 id 為 table 的 Bootstrap Table。
<table id="table"></table>
$('#table').bootstrapTable({
columns: [
{
field: 'id',
title: '項目 ID'
},
{
field: 'name',
title: '項目名稱'
},
{
field: 'price',
title: '項目價格'
}
],
data: [
{
id: 1,
name: '項目 1',
price: '$1'
},
{
id: 2,
name: '項目 2',
price: '$2'
}
]
})
我們也可以通過設置 url: 'data1.json' 來使用遠程 URL 數據。
$('#table').bootstrapTable({
url: 'data1.json',
columns: [
{
field: 'id',
title: '項目 ID'
},
{
field: 'name',
title: '項目名稱'
},
{
field: 'price',
title: '項目價格'
}
]
})
您還可以像下面的表格一樣,向表格添加 pagination、search 和 sortable。
$('#table').bootstrapTable({
url: 'data1.json',
pagination: true,
search: true,
columns: [
{
field: 'id',
title: '項目 ID',
sortable: true
},
{
field: 'name',
title: '項目名稱'
},
{
field: 'price',
title: '項目價格'
}
]
})