liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 使用 []({{ site.repo }}/blob/master/docs/_i18n/{{ site.lang }}/getting-started/usage.md)
 
---
 
引入 Bootstrap 库(假如你的项目还没有使用)和 `bootstrap-table.css` 到 head 标签下。
 
```html
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="bootstrap-table.css">
```
 
引入 jQuery 库,bootstrap 库(假如你的项目还没有使用)和 `bootstrap-table.js` 到 head 标签下或者在 body 标签关闭之前(一般建议这么做)。
 
```html
<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"` 即可。
 
```html
<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"` 来加载数据。
 
```html
<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。
 
```html
<table id="table"></table>
```
 
```js
$('#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'` 来加载数据。
 
```js
$('#table').bootstrapTable({
    url: 'data1.json',
    columns: [{
        field: 'id',
        title: 'Item ID'
    }, {
        field: 'name',
        title: 'Item Name'
    }, {
        field: 'price',
        title: 'Item Price'
    }, ]
});
```