Vue3学习(二)集成ElementPlus实现增删改查

一、增加功能

        表格

<div style="padding: 10px">
<el-button type="primary" @click="handleAdd">新增数据</el-button>

<div style="margin: 10px 0">
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column prop="date" label="日期" width="180"/>
                <el-table-column prop="name" label="姓名" width="180"/>
                <el-table-column prop="address" label="地址"/>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="handleEdit(scope.row,scope.$index)">编辑
                        </el-button>
                        <el-button link type="danger" size="small" @click.prevent="remove(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>



const tableData = [
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
]

        新增弹窗

 <!--弹窗-->
        <el-dialog v-model="dialogFormVisible" title="信息" width="40%">
            <el-form :model="form" label-width="100px" style="padding-right:30px ">
                <el-form-item label="日期">
                    <el-input v-model="form.date" autocomplete="off"/>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="form.name" autocomplete="off"/>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address" autocomplete="off"/>
                </el-form-item>
            </el-form>
            <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </span>
            </template>
        </el-dialog>

导入包装数据所需的组件 

import {ref, reactive} from "vue";

 定义变量

let dialogFormVisible = ref(false)
    let form = reactive({})

点击新增数据时功能实现

//新增数据 设置新的空的绑值对象 打开弹窗
    const handleAdd = () => {
        form = reactive({})
        //打开弹窗
        dialogFormVisible.value = true
    }


//保存数据,把数据插入到tableData中,并刷新页面,弹窗关闭
    const save = () => {
        
            //新增
            tableData.push(form)
 

        dialogFormVisible.value = false
    }

二、编辑功能

        编辑按钮

<el-table-column label="操作">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="handleEdit(scope.row,scope.$index)">编辑
                        </el-button>
                        <el-button link type="danger" size="small" @click.prevent="remove(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
//全局保存编辑的行号
    const globalIndex = ref(-1)

        编辑功能

 //编辑数据 先赋值到表单再弹窗
    const handleEdit = (row, index) => {
        const newObj = Object.assign({}, row)
        form = reactive(newObj)
        //把当前编辑的行号赋值给全局保存的行号
        globalIndex.value = index
        console.log(globalIndex.value)
        dialogFormVisible.value = true
    }

        保存数据

 //保存数据,把数据插入到tableData中,并刷新页面,弹窗关闭
    const save = () => {
        if (globalIndex.value >= 0) {
            //表示编辑
            tableData.value[globalIndex.value] = form
            //还原回去
            globalIndex.value = -1
        } else {
            //新增
            tableData.value.push(form)
        }

        dialogFormVisible.value = false
    }

三、删除功能

        删除按钮

 <el-table-column label="操作">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="handleEdit(scope.row,scope.$index)">编辑
                        </el-button>
                        <el-button link type="danger" size="small" @click.prevent="remove(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>

        删除功能

//删除数据 从index位置开始,删除一行即可
    const remove = (index) => {
        tableData.value.splice(index, 1)
    }

四、查询数据

        查询按钮

<el-input style="width: 300px" placeholder="请输入名称..." v-model="name" clearable></el-input>
        <el-button type="primary" @click="search" style="margin-left: 5px">查询数据</el-button>

        查询功能 

//查询数据
    const search = () =>{
        tableData.value = tableData.value.filter(v =>v.name.includes(name.value))
    }

        完整代码

<template>
    <div style="padding: 10px">

        <el-input style="width: 300px" placeholder="请输入名称..." v-model="name" clearable></el-input>
        <el-button type="primary" @click="search" style="margin-left: 5px">查询数据</el-button>

        <el-button type="primary" @click="handleAdd">新增数据</el-button>

        <div style="margin: 10px 0">
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column prop="date" label="日期" width="180"/>
                <el-table-column prop="name" label="姓名" width="180"/>
                <el-table-column prop="address" label="地址"/>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="handleEdit(scope.row,scope.$index)">编辑
                        </el-button>
                        <el-button link type="danger" size="small" @click.prevent="remove(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--弹窗-->
        <el-dialog v-model="dialogFormVisible" title="信息" width="40%">
            <el-form :model="form" label-width="100px" style="padding-right:30px ">
                <el-form-item label="日期">
                    <el-input v-model="form.date" autocomplete="off"/>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="form.name" autocomplete="off"/>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address" autocomplete="off"/>
                </el-form-item>
            </el-form>
            <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </span>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>

    import {reactive, ref} from "vue";

    let tableData = ref([
        {
            date: '2016-05-03',
            name: 'Jarry',
            address: 'No. 189, Grove St, Los Angeles',
        },
        {
            date: '2016-05-02',
            name: 'Tom',
            address: 'No. 189, Grove St, Los Angeles',
        },
    ])

    let dialogFormVisible = ref(false)
    let form = reactive({})
    //全局保存编辑的行号
    const globalIndex = ref(-1)
    const name = ref('')

    //新增数据 设置新的空的绑值对象 打开弹窗
    const handleAdd = () => {
        form = reactive({})
        //打开弹窗
        dialogFormVisible.value = true
    }

    //保存数据,把数据插入到tableData中,并刷新页面,弹窗关闭
    const save = () => {
        if (globalIndex.value >= 0) {
            //表示编辑
            tableData.value[globalIndex.value] = form
            //还原回去
            globalIndex.value = -1
        } else {
            //新增
            tableData.value.push(form)
        }

        dialogFormVisible.value = false
    }

    //编辑数据 先赋值到表单再弹窗
    const handleEdit = (row, index) => {
        const newObj = Object.assign({}, row)
        form = reactive(newObj)
        //把当前编辑的行号赋值给全局保存的行号
        globalIndex.value = index
        console.log(globalIndex.value)
        dialogFormVisible.value = true
    }

    //删除数据 从index位置开始,删除一行即可
    const remove = (index) => {
        tableData.value.splice(index, 1)
    }

    //查询数据
    const search = () =>{
        tableData.value = tableData.value.filter(v =>v.name.includes(name.value))
    }


</script>

  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3可以通过组合API和Composition API来实现增删改查功能。下面是一个简单的例子: 1. 首先在组件中定义数据和方法: ``` <template> <div> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item, index) in list" :key="index"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td> <button @click="edit(index)">编辑</button> <button @click="del(index)">删除</button> </td> </tr> </tbody> </table> <form @submit.prevent="submit"> <input type="text" v-model="formData.name" placeholder="姓名"> <input type="text" v-model="formData.age" placeholder="年龄"> <button type="submit">提交</button> </form> </div> </template> <script> import { reactive } from 'vue' export default { setup() { const list = reactive([ { name: '张三', age: 18 }, { name: '李四', age: 20 }, { name: '王五', age: 22 } ]) const formData = reactive({ name: '', age: '' }) const editIndex = reactive(-1) const submit = () => { if (editIndex.value === -1) { list.push(formData) } else { list[editIndex.value] = formData editIndex.value = -1 } formData.name = '' formData.age = '' } const edit = (index) => { formData.name = list[index].name formData.age = list[index].age editIndex.value = index } const del = (index) => { list.splice(index, 1) } return { list, formData, submit, edit, del } } } ``` 2. 在模板中使用数据和方法: ``` <template> <div> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item, index) in list" :key="index"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td> <button @click="edit(index)">编辑</button> <button @click="del(index)">删除</button> </td> </tr> </tbody> </table> <form @submit.prevent="submit"> <input type="text" v-model="formData.name" placeholder="姓名"> <input type="text" v-model="formData.age" placeholder="年龄"> <button type="submit">提交</button> </form> </div> </template> ``` 3. 最后,你可以在组件中使用这些方法和数据来实现增删改查功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值