【Vue】demo增删改查列表
<! DOCTYPE html> < html lang="en"> < head > < meta charset="UTF-8"> < meta name="viewport" content="width=device-width, initial-scale=1.0"> < link rel="stylesheet" href="./style/element-ui-index.css"> < link rel="stylesheet" href="./style/index.css"> < title >入门</ title > </ 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" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></ el-date-picker > </ el-col > </ el-row > < el-button type="primary" @click="addUser">添加信息</ 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 > <!--{{scope.row.name}}获取当前列的姓名--> </ 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 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="editObj.birthday" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></ 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 > </ body > <!-- import Vue before Element --> < script src="./javascript/vue.min.js"></ script > <!-- import JavaScript --> < script src="./javascript/element-ui-index.js"></ script > < script src="./javascript/index.js"></ script > </ html > |
JS
new Vue({ el:"#app", data(){ return{ userInfo:{//添加用户信息 name:'', gender:'', phoneNum:'', birthday:'' }, tableData: [{ name: '王小虎1', gender: '男', phoneNum: '13311112222', birthday:'2020-12-09' }, { name: '王小虎2', gender: '男', phoneNum: '15511112222', birthday:'2021-12-09' }, { name: '王小虎3', gender: '男', phoneNum: '16611112222', birthday:'2022-12-09' }, { name: '王小虎4', gender: '男', phoneNum: '17711112222', birthday:'2023-12-09' }], dialogVisible: false,//弹框的显示 editObj:{ name:'', gender:'', phoneNum:'', birthday:'' }, userIndex:0, } }, methods:{ //添加用户 addUser(){ if(this.userInfo.name==""){ this.$message({ message: '姓名不能为空!', type: 'warning' }); return; }; if(!/^1[3578]\d{9}$/.test(this.userInfo.phoneNum)){ this.$message({ message: '请输入正确的电话号码!', type: 'warning' }); return; }; this.tableData.push(this.userInfo); this.userInfo={//添加完之后清空userInfo name:'', gender:'', phoneNum:'', birthday:'' }}, //删除数据 delUser(index){ //alert(index); this.$confirm('确认删除?') .then(_ => { this.tableData.splice(index,1); }).catch(_ => {}); }, editUser(item,index){ //编辑用户 this.userIndex=index; 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; //this.tableData[this.userIndex]=this.editObj;//这种方式不能渲染页面 Vue.set(this.tableData,this.userIndex,this.editObj); } } }) |
CSS
#app{ width: 1024px; margin: 0 auto; } |
分类:
前端 / Vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律