Using vue and bootstrap to write simple user information

Note:

1.v-model = "" for bi-directional data binding of input form logical layer and rendering layer

2.v-on:click='add() 'click method binding

3.v-for='(item,index) in myData' pass history group {{index}} {{item.name} {{item.age}}} is applicable to vue version 2.0

4.v-for='(item,index,key) in myData' traverses json {{index}} {{item}} {{key}} for vue version 2.0

5.v-on:click="currentUser=index" directly bind the data of click event change logic layer. currentUser here is the data of logic layer

6.v-show="myData.length!=0" v-show can directly use the data of logic layer to judge whether it is displayed or hidden according to the following Boolean values

7. < div class = "modal" role ='dialog 'id = "layer" > modal dialog is the mask ID used to contact trigger elements

8. Data toggle = 'modal' alternately displays the hidden mask frame. Data target = '(layer' determines the target modal box

9. Data dismission ='modal 'click to disappear the target element

Design sketch:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0" >
	<title>Document</title>
	<!-- Latest version of Bootstrap core CSS file -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.1.0/css/bootstrap.min.css">
	<script src='../jquery-3.2.1.min.js'></script>
	<script src='bootstrap.js'></script>
	<script src='vue.js'></script>
	<style>
		table td {vertical-align: middle !important;}
	</style>
</head>
<body>
<div class="container">

	<form action="" role='form' class="">
		<div class="form-group">
			<label for="username" class="">User name:</label>
			<input type="text" id="username" class="form-control" v-model="username" placeholder="enter one user name">
		</div>
		<div class="form-group">
			<label for="age">Age:</label>
			<input type="text" id="age" class="form-control" v-model="age" placeholder="Please enter age">
		</div>
		<div class="form-group">
			<input type="button" value="Add to" class="btn btn-primary" v-on:click='add()'>
			<input type="reset" value="Reset" class="btn btn-warning">
		</div>
	</form>
	<table class="table table-bordered table-hover">
	<caption class="h4 text-info text-center">User information table</caption>
		<tr class="text-danger">
			<th class="text-center">Serial number</th>
			<th class="text-center">Full name</th>
			<th class="text-center">Age</th>
			<th class="text-center">operation</th>
		</tr>
		<tr class="text-center" v-for='(item,index) in myData'>
			<td>{{index}}</td>
			<td>{{item.name}}</td>
			<td>{{item.age}}</td>
			<td>
				<button class="btn btn-danger btn-xs" data-toggle='modal' data-target='#Layer 'v-on: Click = "currentuser = index" > delete < / button >
			</td>
		</tr>
		<tr v-show="myData.length!=0">
			<td colspan="4" class="text-right">
				<button class="btn btn-danger btn-xs" v-on:click='currentUser="all"' data-toggle='modal' data-target="#Layer "> delete all < / button >
			</td>
		</tr>
		<tr v-show="myData.length==0">
			<td colspan="4" class="text-center">
				<p class="text-muted">No data...</p>
			</td>
		</tr>
	</table>
	<div class="modal fade bs-example-modal-sm" role='dialog' id="layer">
		<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">Are you sure to delete?</h4>
				</div>
				<div class="modal-body text-right">
					<button class="btn btn-primary btn-sm" data-dismiss='modal'>cancel</button>
					<button class="btn btn-danger btn-sm" data-dismiss='modal'  v-on:click="deleteuser()">confirm</button>
				</div>
			</div>
		</div>
	</div>

	
</div>
</body>
</html>
<script>
	var c = new Vue({
		el:'.container',
		data:{
			myData:[
				{name:"Zhang San",age:20},
				{name:"Li Si",age:20},
				{name:"Wang Wu",age:20},
			],
			username:"",
			age:"",
			currentUser :-100,
		},
		methods : {
			deleteuser :function(){
				if (this.currentUser == 'all') {
					this.myData = [];
				}else{
					this.myData.splice(this.currentUser,1);
				}
			},
			add : function(){
				if (this.username!=""&&this.age!=0) {
					this.myData.push({
						name:this.username,
						age:this.age
					})
				}
			},
		}
	})
</script>

 

Tags: Vue JSON JQuery

Posted on Fri, 10 Jan 2020 11:10:36 -0500 by chuspy