Bootstrap table (当前版本 v1.11.1) 可以有几种快速入门的方法,每种适合不同技能等级的人使用,往下看哪种适合你。
源码
包含了 css,JavaScript,多语言和扩展,以及文档。
克隆或者 Fork 通过 GitHub
CDN
CDNJS 或者 bootcss 提供了 CDN 来支持 Bootstrap table 的 CSS 和 JavaScript 文件链接。
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
<!-- Latest compiled and minified Locales -->
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>
Bower
通过 Bower 来安装和管理 Bootstrap table 的 CSS,JavaScript, 多语言和扩展。
$ bower install bootstrap-table
包含什么Edit on GitHub
下载的 Bootstrap table 源码包含了未压缩的 CSS,JavaScript,语言文件以及扩展,并且提供了压缩扰乱的 min 文件,当然也提供了我们的文档。更具体地说,主要包含了以下的文件:
bootstrap-table/
├── dist/
│ ├── extensions/
│ ├── locale/
│ ├── bootstrap-table.min.css
│ └── bootstrap-table.min.js
├── docs/
└── src/
├── extensions/
├── locale/
├── bootstrap-table.css
└── bootstrap-table.js
src/
,locale/
和 extensions/
是我们的 CSS,JavaScript 的源码。dist/
文件夹包含了所有src/
下压缩并扰乱的文件。docs/
文件夹包含了我们文档的源码。另外,我们提供了包信息,License 信息,和其他的信息。
编译 CSS 和 JavaScriptEdit on GitHub
Bootstrap table 使用 Grunt 来作为编译系统,可以用十分方便的方法来编译我们代码,或者文档等等。
安装 Grunt
安装 Grunt,你必须先 下载和安装 node.js (包含 npm)。npm 是 node 的包模块 管理工具。
然后,我们使用命令行:
- 安装全局的
grunt-cli
,使用npm install -g grunt-cli
命令即可。 - 进入到
/bootstrap-table/
的根目录,然后运行npm install
。npm 将通过查找package.json
文件并自动安装这里所需要的依赖。
完成之后,你就可以运行一下的命令来编译的代码了。
可以用的 Grunt 命令
grunt dist
(编译 CSS 和 JavaScript)
这里我们会生成 /dist/
文件夹。 As a Bootstrap user, this is normally the command you want.
grunt test
(运行 tests)
运行 JSHint 来测试我们的代码。
grunt docs
(编译和测试文档)
编译和测试 CSS,JavaScript,本地我们可以通过 jekyll serve
来运行我们的文档。
grunt
(编译所有并运行测试)
压缩和扰乱 CSS 和 JavaScript,测试,编译文档等等。依赖 Jekyll。
遇到问题
如果你安装或运行依赖是遇到问题,首先删除 /node_modules/
npm 生成的文件夹。然后,再运行一次 npm install
即可。
引入 Bootstrap 库(假如你的项目还没有使用)和 bootstrap-table.css
到 head 标签下。
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="bootstrap-table.css">
引入 jQuery 库,bootstrap 库(假如你的项目还没有使用)和 bootstrap-table.js
到 head 标签下或者在 body 标签关闭之前(一般建议这么做)。
<script src="jquery.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="bootstrap-table.js"></script>
<-- put your locale files after bootstrap-table.js -->
<script src="bootstrap-table-zh-CN.js"></script>
通过 data 属性或者 JavaScript 来启用 Bootstrap Table 插件,显示丰富的功能。
通过 data 属性的方式
无需编写 JavaScript 启用 bootstrap table,我们对普通的 table 设置 data-toggle="table"
即可。
<table data-toggle="table">
<thead>
<tr>
<th>Item ID</th>
<th>Item Name</th>
<th>Item Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Item 1</td>
<td>$1</td>
</tr>
<tr>
<td>2</td>
<td>Item 2</td>
<td>$2</td>
</tr>
</tbody>
</table>
我们也可以通过设置远程的 url 如 data-url="data1.json"
来加载数据。
<table data-toggle="table" data-url="data1.json">
<thead>
<tr>
<th data-field="id">Item ID</th>
<th data-field="name">Item Name</th>
<th data-field="price">Item Price</th>
</tr>
</thead>
</table>
通过 JavaScript 的方式
通过表格 id 来启用 bootstrap table。
<table id="table"></table>
$('#table').bootstrapTable({
columns: [{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}],
data: [{
id: 1,
name: 'Item 1',
price: '$1'
}, {
id: 2,
name: 'Item 2',
price: '$2'
}]
});
我们也可以通过设置远程的 url 如 url: 'data1.json'
来加载数据。
$('#table').bootstrapTable({
url: 'data1.json',
columns: [{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}, ]
});