一、說明
隨者技術進步,網頁表現在更愈趨複雜多變,尤其是手機及平板普及帶來的顯示需求變化(如RWD),讓網頁前端開發難度大為提升。因此,利用前端框架輔助開發更顯重要。
在此使用Bootstrap4
二、安裝
2.1 使用NPM安裝
- 在vscode終端機上輸入指令
1
$ npm install bootstrap jquery popper.js
- 安裝完成後,資料會放在node_modules之下
- 推送至Git
三、使用Bootstrap
3.1 建立第一個bootstrap頁面
新增Controller
application/controllers/Hello_bootstrap.php寫入內容
1
2
3
4
5
6
7
8
9
10
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello_bootstrap extends CI_Controller {
public function index()
{
$this->load->view('helloBootstrap');
}
}新增View:
application/views/helloBootstrap.php寫入內容
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
defined('BASEPATH') or exit('No direct script access allowed');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap 4 Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
<script src="/node_modules/jquery/dist/jquery.min.js"></script>
<script src="/node_modules/popper.js"></script>
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Hello Bootstrap !</h1>
<p>This is some text.</p>
</div>
</body>
</html>查看結果
前往網址:https://crud-training.dev.idv/hello_bootstrap上面的hello_bootstrap,是Controller名稱Hello_bootstrap字首轉小寫而來 (只有字首會轉小寫)
3.2 常用Bootstrap元件
- Grid
- Colors
- Forms
- Modal
- Tooltip
- Popover
- Tables
- Buttons
- Dropdowns
- Navs, Navsbar
- Pagination
四、練習範例
新增Controller
application/controllers/Bootstrap_example.php寫入內容
1
2
3
4
5
6
7
8
9
10
defined('BASEPATH') OR exit('No direct script access allowed');
class Bootstrap_example extends CI_Controller {
public function index()
{
$this->load->view('bootstrapExample');
}
}新增View:
application/views/bootstrapExample.php寫入內容
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
defined('BASEPATH') or exit('No direct script access allowed');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap 4 Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
<script src="/node_modules/jquery/dist/jquery.min.js"></script>
<script src="/node_modules/popper.js"></script>
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Font Awesome Icon -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" />
</head>
<body>
<div class="container">
<!-- Title -->
<div class="row">
<div class="col-sm-12">
<h1>帳號管理</h1>
</div>
</div>
<!-- Controll Form -->
<div class="row">
<div class="col-sm-4 form-group">
<input type="password" class="form-control" id="pwd">
</div>
<div class="col-sm-4"><button class="btn btn-primary">搜尋</button></div>
<div class="col-sm-4"><button class="btn btn-warning float-right" data-toggle="modal" data-target="#myModal">新增</button></div>
</div>
<!-- Table -->
<div class="row">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th> </th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td><i class="fas fa-pen color_green ml-3"></i> <i class="fas fa-trash text-danger ml-3"></i></td>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
</tr>
<tr>
<td><i class="fas fa-pen color_green ml-3"></i> <i class="fas fa-trash text-danger ml-3"></i></td>
<td>Mary</td>
<td>Moe</td>
<td>mary@example.com</td>
</tr>
<tr>
<td><i class="fas fa-pen color_green ml-3"></i> <i class="fas fa-trash text-danger ml-3"></i></td>
<td>July</td>
<td>Dooley</td>
<td>july@example.com</td>
</tr>
</tbody>
</table>
</div>
<!-- Pagination -->
<div class="row float-right">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
</div>
</div>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>請自行練習Bootstrap元件使用
查看結果
前往網址:https://crud-training.dev.idv/bootstrap_example上面的bootstrap_example,是Controller名稱Bootstrap_example字首轉小寫而來 (只有字首會轉小寫)
練習完畢後,將程式碼推送至Git上