Home > Article > Web Front-end > How to implement data addition, deletion, modification and query functions through vue and Element-plus
How to implement data addition, deletion, modification and query functions through Vue and Element Plus
Overview:
Vue is a progressive framework for building user interfaces. It is similar to Element Plus which is based on Vue 2.x The combination of UI component library can help us quickly build an interface with data addition, deletion, modification and query functions. This article will introduce how to use Vue and Element Plus to implement the data addition, deletion, modification and query functions, and give corresponding code examples.
First, open the command line tool and switch to the project directory. Execute the following command to install Element Plus:
npm install element-plus --save
Then, introduce Element Plus styles and components in main.js:
import { createApp } from 'vue' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' createApp(App).use(ElementPlus).mount('#app')
Create the components folder in the src directory, and create two files, List.vue and Form.vue respectively.
Among them, List.vue is used to display the data list, including table headers and data rows. Form.vue is a form used to add and edit data.
List.vue Sample code:
<template> <div> <el-table :data="data" border> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column label="操作"> <template #default="{row}"> <el-button type="text" @click="edit(row)">编辑</el-button> <el-button type="text" @click="del(row)">删除</el-button> </template> </el-table-column> </el-table> </div> </template> <script> export default { props: { data: { type: Array, default: () => [] } }, methods: { edit(row) { // 编辑逻辑 }, del(row) { // 删除逻辑 } } } </script>
Form.vue Sample code:
<template> <div> <el-form ref="form" :model="form" :rules="rules"> <el-form-item label="姓名" prop="name"> <el-input v-model="form.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="age"> <el-input v-model="form.age"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submit">提交</el-button> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { form: { name: '', age: '' }, rules: { name: [ { required: true, message: '请输入姓名', trigger: 'blur' } ], age: [ { required: true, message: '请输入年龄', trigger: 'blur' } ] } } }, methods: { submit() { // 提交逻辑 } } } </script>
<template> <div> <List :data="list" /> <Form /> </div> </template> <script> import List from './components/List.vue' import Form from './components/Form.vue' export default { data() { return { list: [ { id: 1, name: '张三', age: '20' }, { id: 2, name: '李四', age: '25' } ] } }, components: { List, Form } } </script>
In the parent component, we pass in the list array containing data to the data attribute of the List component, and then render the data list through the v-for loop in the List component.
edit(row) { // 编辑逻辑 this.editingData = { ...row } this.showForm = true }, del(row) { // 删除逻辑 const index = this.data.indexOf(row) this.data.splice(index, 1) }
In the methods of the Form component, we can implement the logic of adding and editing data. The specific implementation method is as follows:
submit() { // 提交逻辑 this.$refs.form.validate((valid) => { if (valid) { // 表单验证通过 if (this.editingData) { // 编辑数据 const index = this.data.findIndex(item => item.id === this.editingData.id) this.data.splice(index, 1, this.editingData) } else { // 新增数据 const id = Math.max(...this.data.map(item => item.id)) + 1 this.data.push({ ...this.form, id }) } this.resetForm() } else { // 表单验证未通过 return false } }) }, resetForm() { // 重置表单 this.editingData = null this.form = { name: '', age: '' } this.$refs.form.resetFields() }
The above code example demonstrates how to implement the data addition, deletion, modification and query functions through Vue and Element Plus. Use the List component to display the data list, and use the Form component to implement forms for adding and editing data. Using the communication between parent and child components, we can realize the transfer and update of data, thereby realizing the addition, deletion, modification and query operations of data.
The above is the detailed content of How to implement data addition, deletion, modification and query functions through vue and Element-plus. For more information, please follow other related articles on the PHP Chinese website!