(精華2020年5月4日更新) vue教程篇 簡單小結(1)-使用者管理

2b勿擾發表於2020-05-04
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>練習:使用者管理</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
	<!-- Bootstrap -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
	<!-- jQuery (Bootstrap 的所有 JavaScript 外掛都依賴 jQuery,所以必須放在前邊) -->
	<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
	<!-- 載入 Bootstrap 的所有 JavaScript 外掛。你也可以根據需要只載入單個外掛。 -->
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
	<script>
		window.onload = function () {
			let vm = new Vue({
				el: '.container',
				data: {
					users: [{
							name: 'tom',
							age: 24,
							email: 'tom@itany.com'
						},
						{
							name: 'jack',
							age: 23,
							email: 'jack@sina.com'
						}
					],
					user: {},
					nowIndex: -1 //當前要刪除項的索引
				},
				methods: {
					addUser() {
						this.users.push(this.user);
						this.user = {};
					},
					deleteUser() {
						if (this.nowIndex == -1) {
							//刪除所有
							this.users = [];
							// this.users.length=0;
						} else {
							//從指定索引的位置開始刪除
							this.users.splice(this.nowIndex, 1)
						}
					}
				}
			});
		}
	</script>
</head>

<body>
	<div class="container">
		<h2 class="text-center">新增使用者</h2>
		<form class="form-horizontal">
			<div class="form-group">
				<label for="name" class="control-label col-sm-2 col-sm-offset-2">姓 名:</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="name" v-model="user.name" placeholder="請輸入姓名">
				</div>
			</div>
			<div class="form-group">
				<label for="age" class="control-label col-sm-2 col-sm-offset-2">年 齡:</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="age" v-model="user.age" placeholder="請輸入年齡">
				</div>
			</div>
			<div class="form-group">
				<label for="email" class="control-label col-sm-2 col-sm-offset-2">郵 箱:</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="email" v-model="user.email" placeholder="請輸入郵箱">
				</div>
			</div>
			<div class="form-group text-center">
				<input type="button" value="添  加" class="btn btn-primary" @click="addUser">
				<input type="reset" value="重  置" class="btn btn-primary">
			</div>
		</form>
		<hr>

		<table class="table table-bordered table-hover">
			<caption class="h3 text-center text-info">使用者列表</caption>
			<thead>
				<tr>
					<th class="text-center">序號</th>
					<th class="text-center">姓名</th>
					<th class="text-center">年齡</th>
					<th class="text-center">郵箱</th>
					<th class="text-center">操作</th>
				</tr>
			</thead>
			<tbody>
				<tr v-for="(user,index) in users" :key="index" class="text-center">
					<td>{{index+1}}</td>
					<td>{{user.name}}</td>
					<td>{{user.age}}</td>
					<td>{{user.email}}</td>
					<td>
						<button class="btn btn-danger btn-sm" data-toggle="modal" data-target="#del"
							v-on:click="nowIndex=index">刪除</button>
					</td>
				</tr>
				<tr>
					<td colspan="5" class="text-right">
						<button class="btn btn-danger btn-sm" data-toggle="modal" data-target="#del"
							v-on:click="nowIndex=-1">刪除所有</button>
					</td>
				</tr>
			</tbody>
		</table>
		<!-- 模態框,彈出框 -->
		<div class="modal fade" id="del">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button class="close" data-dismiss="modal">
							<span>&times;</span>
						</button>
						<h4 class="modal-title" v-show="nowIndex!==-1">
							確認要刪除使用者:{{users[nowIndex]?users[nowIndex].name:''}} 嗎?</h4>
						<h4 class="modal-title" v-show="nowIndex===-1">確認要刪除所有使用者嗎?</h4>
					</div>
					<div class="modal-body text-center">
						<button class="btn btn-primary" data-dismiss="modal">取消</button>
						<button class="btn btn-primary" data-dismiss="modal" v-on:click="deleteUser">確認</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>

</html>

相關文章