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


<html>

	<head>
		<meta charset="utf-8" />
		<title>我的VueDemotitle>
		<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>

你可能感兴趣的:(学习笔记)