vue3?element-plus?實現(xiàn)表格數(shù)據(jù)更改功能詳細步驟
在 vue3 中使用 element-plus 實現(xiàn)表格數(shù)據(jù)更改功能,可以通過以下步驟實現(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>- 在表格中添加一個“編輯”按鈕,點擊該按鈕會彈出一個對話框,用于修改表格行的數(shù)據(jù)。
- 在對話框中添加一個表單,用于輸入修改后的數(shù)據(jù)。
- 在對話框的“保存”按鈕上綁定一個 submitForm 方法,用于提交表單數(shù)據(jù)。在 submitForm 方法中,可以先對輸入的數(shù)據(jù)進行驗證,如果驗證通過,則將修改后的數(shù)據(jù)更新到表格中,同時關(guān)閉對話框。
以上就是使用 element-plus 實現(xiàn)表格數(shù)據(jù)更改功能的全部步驟。
到此這篇關(guān)于vue3 element-plus 實現(xiàn)表格數(shù)據(jù)更改功能的文章就介紹到這了,更多相關(guān)vue3 element-plus 表格數(shù)據(jù)更改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3實現(xiàn)Element Plus表格的多選功能與條件操作
- 基于Vue3和Element Plus實現(xiàn)可擴展的表格組件
- vue3基于elementplus 簡單實現(xiàn)表格二次封裝過程
- Vue3?使用Element?Plus表格單選帶checkbox功能
- vue3+element?Plus實現(xiàn)表格前端分頁完整示例
- element-plus+Vue3實現(xiàn)表格數(shù)據(jù)動態(tài)渲染
- Vue3中的element-plus表格實現(xiàn)代碼
- vue3 + ElementPlus 封裝列表表格組件包含分頁
- Vue3+Element Plus 通用表格組件封裝與使用實踐
相關(guān)文章
Vue.js中Props數(shù)據(jù)未響應(yīng)式傳遞的問題及解決過程
Vue.js中Props數(shù)據(jù)未響應(yīng)式傳遞的原因及解決方法,避免直接修改Props數(shù)據(jù),正確使用計算屬性,正確處理嵌套Props數(shù)據(jù),使用清晰的事件命名2025-11-11
vue 動態(tài)改變靜態(tài)圖片以及請求網(wǎng)絡(luò)圖片的實現(xiàn)方法
下面小編就為大家分享一篇vue 動態(tài)改變靜態(tài)圖片以及請求網(wǎng)絡(luò)圖片的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Vue使用NProgress實現(xiàn)頁面頂部的進度條顯示效果
這篇文章主要介紹了vue Nprogress頁面頂部進度條功能實現(xiàn),NProgress是頁面跳轉(zhuǎn)是出現(xiàn)在瀏覽器頂部的進度條,本文通過實例代碼給大家講解,需要的朋友可以參考下2022-12-12

