使用Vue.js搭建简单的表格页面

本文介绍了一个使用 Vue.js 构建的简单应用案例,该应用实现了通过输入姓名、编号和等级来添加成员,并能从表格中删除已添加的成员。表格展示了所有成员的信息,包括姓名、编号和等级等字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>我的VueDemo</title>
		<script src="https://cdn.bootcss.com/vue/2.4.4/vue.js"></script>
		<link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet">
		<style type="text/css">
			.table {
				width: 500px;
			}
		</style>
	</head>

	<body>
		<div id="app">
			<div class="form-group">
				<label for="group">姓名</label>
				<input type="text" v-model="person1.name">
			</div>
			<div class="form-group">
				<label for="author">编号</label>
				<input type="text" v-model="person1.num">
			</div>
			<div class="form-group">
				<label for="price">等级</label>
				<input type="text" v-model="person1.score">
			</div>
			<button class="btn btn-success" v-on:click="addPerson()">添加</button>
			<table class="table table-bordered" class="table">
				<thead>
					<tr>
						<th>姓名</th>
						<th>编号</th>
						<th>等级</th>
						<th>删除</th>
					</tr>
				</thead>
				<tbody>
					<tr v-for="person in people">
						<td>{{person.name}}</td>
						<td>{{person.num}}</td>
						<td>{{person.score}}</td>
						<td><button class="btn btn-warning" @click="delPerson(person)">删除</button></td>
					</tr>
				</tbody>
			</table>
		</div>
	</body>
	<script type="text/javascript">
		var vm = new Vue({
			el: '#app',
			data: {
				person1: {
					name: '',
					num: '',
					score: ''
				},
				people: [{
						name: 'AAAAA',
						num: '000001',
						score: '01'
					},
					{
						name: 'BBBBB',
						num: '000002',
						score: '02'
					},
					{
						name: 'CCCCC',
						num: '000003',
						score: '03'
					},
				]
			},
			//在methods中定义方法
			methods: {
				//新增成员信息
				addPerson: function() {
					this.person1.id = this.people.length + 1;
					this.people.push(this.person1);
					this.person1 = {};
				},
				//删除成员信息
				delPerson: function(person) {
					this.people.splice(this.people.indexOf(person), 1);
				}
			}
		})
	</script>

</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值