使用vue實現用戶管理 添加及刪除功能


簡單的管理系統-增刪改查

添加及刪除功能

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>用戶管理</title>
		<script src="../js/vue-2.4.0.js"></script>
		<link rel="stylesheet" type="text/css" href="../bootstrap-3.3.7-dist/css/bootstrap.css"/>
	</head>
	<body>
		<div id="app">
			<div class="panel panel-primary">
			  <div class="panel-heading">
			    <h3 class="panel-title">用戶管理</h3>
			  </div>
			  <div class="panel-body">
			   <form class="form-inline">
				  <div class="form-group">
				    <label for="exampleInputName2">Id:</label>
				    <input type="text" class="form-control" id="exampleInputName2" v-model="id">
				  </div>
				  <div class="form-group">
				    <label for="exampleInputEmail2">Name:</label>
				    <input type="text" class="form-control" id="exampleInputEmail2" v-model="name">
				  </div>
				  <button type="button" class="btn btn-primary" @click="add()">添加</button>
				  <div class="form-group">
				    <label for="exampleInputName3">搜索名稱關鍵字:</label>
				    <input type="text" class="form-control" id="exampleInputName3" >
				  </div>
				</form>
			  </div>
			</div>
			<table class="table table-bordered table-striped table-hover">
				<tr>
					<th>Id</th>
					<th>Name</th>
					<th>CTime</th>
					<th>Operation</th>
				</tr>
				<tr v-for="item in userList">
					<td>{{item.id}}</td>
					<td>{{item.name}}</td>
					<td>{{item.ctime}}</td>
					<td><a href="#" class="btn btn-primary" @click.prevent="del(item.id)">刪除</a></td>
				</tr>
			</table>
		</div>
		
		<script type="text/javascript">
			let vm = new Vue({
				el: "#app",
				data: {
					userList:[
						{id:1,name:"劉備",ctime: new Date()},
						{id:2,name:"關羽",ctime: new Date()},
						{id:3,name:"張飛",ctime: new Date()},
					],
					id:'',
					name:''
				},
				methods: {
					add() {
						let user = {id:this.id,name:this.name,ctime: new Date()};
						this.userList.push(user);
						this.id = this.name = '';
					},
					del(userId) {
						
						let index = this.userList.findIndex(item => item.id==userId);
						this.userList.splice(index, 1);
					}
				}
			});
		</script>
	</body>
</html>

優化:增加了根據名字搜索

<html>
	<head>
		<meta charset="UTF-8">
		<title>用戶管理</title>
		<script src="../js/vue-2.4.0.js"></script>
		<link rel="stylesheet" type="text/css" href="../bootstrap-3.3.7-dist/css/bootstrap.css"/>
	</head>
	<body>
		<div id="app">
			<div class="panel panel-primary">
			  <div class="panel-heading">
			    <h3 class="panel-title">用戶管理</h3>
			  </div>
			  <div class="panel-body">
			   <form class="form-inline">
				  <div class="form-group">
				    <label for="exampleInputName2">Id:</label>
				    <input type="text" class="form-control" id="exampleInputName2" v-model="id">
				  </div>
				  <div class="form-group">
				    <label for="exampleInputEmail2">Name:</label>
				    <input type="text" class="form-control" 
				    	id="exampleInputEmail2" v-model="name" @keydown.enter="add()">
				  </div>
				  <button type="button" class="btn btn-primary" @click="add()">添加</button>
				  <div class="form-group">
				    <label for="exampleInputName3">搜索名稱關鍵字:</label>
				    <input type="text" class="form-control"
				    	 id="exampleInputName3" v-model="keyWord" v-focus>
				  </div>
				</form>
			  </div>
			</div>
			<table class="table table-bordered table-striped table-hover">
				<tr>
					<th>Id</th>
					<th>Name</th>
					<th>CTime</th>
					<th>Operation</th>
				</tr>
				<tr v-for="item in search(keyWord)">
					<td>{{item.id}}</td>
					<td>{{item.name}}</td>
					<td>{{item.ctime | formatDate('yyyy-mm-dd hh:mm:ss')}}</td>
					<td><a href="#" class="btn btn-primary" @click.prevent="del(item.id)">刪除</a></td>
				</tr>
			</table>
		</div>
		
		<script type="text/javascript"> 
			//自定義鍵盤碼
			//Vue.config.keyCodes.f2 = 113
			
			//全局指令
			Vue.directive("focus",{
				inserted: function(el) {
					el.focus();
				}
			})
			
			Vue.filter("formatDate",function(data, pattern) {
				let year = data.getFullYear();
				let month = (data.getMonth() + 1).toString().padStart(2,'0');
				let day = data.getDate().toString().padStart(2,'0');
				if(pattern==null || pattern==''){
					return `${year}年${month}月${day}日`
				}else {
					let h = data.getHours().toString().padStart(2,'0');
					let m = data.getMinutes().toString().padStart(2,'0');
					let s = data.getSeconds().toString().padStart(2,'0');
					
					return `${year}年${month}月${day}日  ${h}:${m}:${s}`
				}
				
			})
			
			let vm = new Vue({
				el: "#app",
				data: {
					userList:[
						{id:1,name:"劉備",ctime: new Date()},
						{id:2,name:"關羽",ctime: new Date()},
						{id:3,name:"張飛",ctime: new Date()},
					],
					id:'',
					name:'',
					keyWord:''
				},
				methods: {
					add() {
						let user = {id:this.id,name:this.name,ctime: new Date()};
						this.userList.push(user);
						this.id = this.name = '';
					},
					del(userId) {
						
						let index = this.userList.findIndex(item => item.id==userId);
						this.userList.splice(index, 1);
					},
					search(keyWord) {
						return this.userList.filter(item => item.name.includes(keyWord))
					}
				}
			});
		</script>
	</body>
</html>

功能還有很多很多的缺點,這只是在學習過程中的一個小練習,復習相關知識


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM