![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9ce8e9aa4cc3485f641d85aab9287b9e.png#pic_center)
同学录介绍
随着社会发展,对于记忆的保存还是蛮重要的,毕竟也是曾经经历过的历史,也值得怀念,值得我们去回忆。
效果展示
这是代码完成实现的效果
可以填写里面的信息点击添加到下面表格里
代码实现
代码就不解释了,能看得懂就看,看不懂欢迎留言咨询,我定当给你解决哈哈哈!!!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue+Element</title>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<link rel="stylesheet" href="./style/index.css">
</head>
<body>
<div id="app">
<h1>同学录</h1>
<div class="head">
<el-row :gutter="20">
<el-col :span="6">
<el-input v-model="userInfo.name" placeholder="请输入姓名"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.gender" placeholder="请输入性别"></el-input>
</el-col>
<el-col :span="6">
<el-input v-model="userInfo.phoneNum" placeholder="请输入电话号码"></el-input>
</el-col>
<el-col :span="6">
<el-date-picker
v-model="userInfo.birthday"
type="date"
format="yyyy 年 MM 月 dd 日"
value-format="yyyy-MM-dd"
placeholder="选择日期">
</el-date-picker>
</el-row>
</el-col>
<el-button type="primary" @click="addUser" class="add-btn">添加信息</el-button>
</div>
<div class="body">
<template>
<el-table
:data="tableData"
style="width: 100%">
<el-table-column
prop="date"
label="序号"
width="180">
<template slot-scope="scope">
{{scope.$index+1}}
</template>
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="180">
</el-table-column>
<el-table-column
prop="gender"
label="性别">
</el-table-column>
<el-table-column
prop="phoneNum"
label="手机号码">
</el-table-column>
<el-table-column
prop="birthday"
label="生日">
</el-table-column>
<el-table-column
prop="birthday"
label="操作">
<template slot-scope="scope">
<el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row,scope.$index)" circle></el-button>
<el-button type="danger" icon="el-icon-delete" @click="delUser(scope.$index)" circle></el-button>
</template>
</el-table-column>
</el-table>
</template>
</div>
<el-dialog
title="修改信息"
:visible.sync="dialogVisible"
width="30%"
:before-close="handleClose">
<div>
<el-form ref="form" :model="editobj" label-width="80px">
<el-form-item label="姓名">
<el-input v-model="editobj.name"></el-input>
</el-form-item>
<el-form-item label="性别">
<el-input v-model="editobj.gender"></el-input>
</el-form-item>
<el-form-item label="手机号码">
<el-input v-model="editobj.phoneNum"></el-input>
</el-form-item>
<el-form-item label="生日">
<el-date-picker
v-model="userInfo.birthday"
type="date"
format="yyyy 年 MM 月 dd 日"
value-format="yyyy-MM-dd"
placeholder="选择日期">
</el-date-picker>
</el-form-item>
</el-form>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="confirm">确 定</el-button>
</span>
</el-dialog>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="./javascript/index.js"></script>
</body>
</html>
new Vue({
el:'#app',
data:function(){
return {
userInfo:{ //添加用户信息
name:'',
gender:'',
phoneNum:'',
birthday:'',
},
tableData: [{
name: '小明',
gender:'男',
phoneNum:'14562378910',
birthday:'2020-02-23'
}],
dialogVisible: false,
editobj:{
name:'',
gender:'',
phoneNum:'',
birthday:'',
},
userIndex:0,
}
},
methods:{
addUser(){
//添加用户信息
if(!this.userInfo.name){
this.$message({
message: '请输入姓名',
type: 'warning'
});
return;
}
if(!this.userInfo.gender){
this.$message({
message: '请输入性别',
type: 'warning'
});
return;
}
if(!/^1[3456789]\d{9}$/.test(this.userInfo.phoneNum)){
this.$message({
message: '请输入正确手机号码',
type: 'warning'
});
return;
}
if(!this.userInfo.birthday){
this.$message({
message: '请输入生日',
type: 'warning'
});
return;
}
this.tableData.push(this.userInfo);
this.userInfo = {
name:'',
gender:'',
phoneNum:'',
birthday:'',
};
},
//删除一组数据
delUser(idx){
//console.log(idx);
this.$confirm('确认关闭?')
.then(_ => {
this.tableData.splice(idx,1);
})
.catch(_ => {});
},
//编辑数据
editUser(item,idx){
this.userIndex =idx;
this.editobj={
name:item.name,
gender:item.gender,
phoneNum:item.phoneNum,
birthday:item.birthday,
};
this.dialogVisible = true;
},
handleClose(){
this.dialogVisible = false;
},
confirm() {
this.dialogVisible = false;
Vue.set(this.tableData,this.userIndex,this.editobj);
}
}
})
h1{
background: inherit;
border-bottom: 1px dashed #097;
color: #000099;
font: 60px Georgia,serif;
margin:0 0 10px;
padding: 0 0 35px;
text-align: center;
}
#app {
width: 1024px;
margin: 0 auto;
}
.add-btn {
margin-top: 20px;
width: 100%;
}
.body {
margin-top: 20px;
}
VQ小总结
有什么问题可以多多指教,初学小白一枚,还有待提高。