vue3?element-plus?實(shí)現(xiàn)表格數(shù)據(jù)更改功能詳細(xì)步驟
在 vue3 中使用 element-plus 實(shí)現(xiàn)表格數(shù)據(jù)更改功能,可以通過(guò)以下步驟實(shí)現(xiàn):
1.導(dǎo)入 element-plus 的 Table、Form 和 Input 組件,并在組件中引入表格數(shù)據(jù):
<template> <div> <el-table :data="tableData"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> <el-table-column> <template #default="{row}"> <el-button @click="editRow(row)">Edit</el-button> </template> </el-table-column> </el-table> <el-dialog :visible.sync="dialogVisible"> <el-form ref="form" :model="form" :rules="rules" label-width="80px"> <el-form-item label="Name" prop="name"> <el-input v-model="form.name"></el-input> </el-form-item> <el-form-item label="Age" prop="age"> <el-input v-model.number="form.age"></el-input> </el-form-item> <el-form-item label="Address" prop="address"> <el-input v-model="form.address"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">Cancel</el-button> <el-button type="primary" @click="submitForm">Save</el-button> </div> </el-dialog> </div> </template> <script> import { ref } from 'vue'; import { ElTable, ElTableColumn, ElButton, ElDialog, ElForm, ElFormItem, ElInput } from 'element-plus'; export default { components: { ElTable, ElTableColumn, ElButton, ElDialog, ElForm, ElFormItem, ElInput, }, setup() { const tableData = ref([ { name: 'John', age: 30, address: 'New York', }, { name: 'Jane', age: 25, address: 'San Francisco', }, { name: 'Bob', age: 40, address: 'Dallas', }, ]); const form = ref({}); const dialogVisible = ref(false); const rules = { name: [ { required: true, message: 'Please input name', trigger: 'blur' }, ], age: [ { required: true, message: 'Please input age', trigger: 'blur' }, { type: 'number', message: 'Age must be a number', trigger: 'blur' }, ], address: [ { required: true, message: 'Please input address', trigger: 'blur' }, ], }; const editRow = (row) => { form.value = { ...row }; dialogVisible.value = true; }; const submitForm = () => { const formRef = this.$refs.form; formRef.validate((valid) => { if (valid) { const dataIndex = tableData.value.indexOf(form.value); const tableDataCopy = [...tableData.value]; tableDataCopy.splice(dataIndex, 1, form.value); tableData.value = tableDataCopy; dialogVisible.value = false; } }); }; return { tableData, form, dialogVisible, rules, editRow, submitForm, }; }, }; </script>
- 在表格中添加一個(gè)“編輯”按鈕,點(diǎn)擊該按鈕會(huì)彈出一個(gè)對(duì)話框,用于修改表格行的數(shù)據(jù)。
- 在對(duì)話框中添加一個(gè)表單,用于輸入修改后的數(shù)據(jù)。
- 在對(duì)話框的“保存”按鈕上綁定一個(gè) submitForm 方法,用于提交表單數(shù)據(jù)。在 submitForm 方法中,可以先對(duì)輸入的數(shù)據(jù)進(jìn)行驗(yàn)證,如果驗(yàn)證通過(guò),則將修改后的數(shù)據(jù)更新到表格中,同時(shí)關(guān)閉對(duì)話框。
以上就是使用 element-plus 實(shí)現(xiàn)表格數(shù)據(jù)更改功能的全部步驟。
到此這篇關(guān)于vue3 element-plus 實(shí)現(xiàn)表格數(shù)據(jù)更改功能的文章就介紹到這了,更多相關(guān)vue3 element-plus 表格數(shù)據(jù)更改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中使用js-xlsx導(dǎo)出excel的實(shí)現(xiàn)方法
本文主要介紹了vue中使用js-xlsx導(dǎo)出excel的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02VUE中filters過(guò)濾器的兩種用法實(shí)例
vue中過(guò)濾器的作用可被用于一些常見(jiàn)的文本格式化,也就是修飾文本,但是文本內(nèi)容不會(huì)改變,下面這篇文章主要給大家介紹了關(guān)于VUE中filters過(guò)濾器的兩種用法,需要的朋友可以參考下2022-04-04vue2中組件互相調(diào)用實(shí)例methods中的方法實(shí)現(xiàn)詳解
vue在同一個(gè)組件內(nèi),方法之間經(jīng)常需要互相調(diào)用,下面這篇文章主要給大家介紹了關(guān)于vue2中組件互相調(diào)用實(shí)例methods中的方法實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08應(yīng)用provide與inject刷新Vue頁(yè)面方法
這篇文章主要介紹了應(yīng)用provide與inject刷新Vue頁(yè)面的兩種方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,多多進(jìn)步,祝大家早日升職加薪2021-09-09ant desing vue table 實(shí)現(xiàn)可伸縮列的完整例子
最近在使用ant-design-vue做表格時(shí),遇到要做一個(gè)可伸縮列表格的需求,在網(wǎng)上一直沒(méi)有找到好的方法,于是小編動(dòng)手自己寫個(gè)可以此功能,下面小編把a(bǔ)nt desing vue table 可伸縮列的實(shí)現(xiàn)代碼分享到腳本之家平臺(tái)供大家參考2021-05-05vue實(shí)現(xiàn)圖片轉(zhuǎn)pdf的示例代碼
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)圖片轉(zhuǎn)pdf的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以跟隨小編一起了解一下2023-08-08vue.js 實(shí)現(xiàn)點(diǎn)擊展開(kāi)收起動(dòng)畫效果
這篇文章主要介紹了vue.js 實(shí)現(xiàn)點(diǎn)擊展開(kāi)收起動(dòng)畫效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Vue按回車鍵進(jìn)行搜索的實(shí)現(xiàn)方式
這篇文章主要介紹了Vue按回車鍵進(jìn)行搜索的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01