vue中循環(huán)表格數(shù)據(jù)出現(xiàn)數(shù)據(jù)聯(lián)動現(xiàn)象(示例代碼)
vue中循環(huán)表格數(shù)據(jù),出現(xiàn)數(shù)據(jù)聯(lián)動現(xiàn)象。
問題描述:如圖

我輸入期數(shù)為4,會循環(huán)出來4個表格,其中名額分配一欄人數(shù)是可以編輯的,但是當我修改第一個表格的數(shù)據(jù)之后,后面的表格數(shù)據(jù)也跟著修改了。
源碼如下
<template>
<div class="content">
<div style="margin: 20px;">
<span>期數(shù):</span>
<el-input-number v-model="periods" :min="0" size="small" :precision="0" :controls="false" @change="handleChange"/>
</div>
<div v-for="(tableData, index) in listDatas" :key="index" style="margin-top: 20px;">
<div style="width: 100%; text-align: right; margin-bottom: 5px;">
<el-button type="primary" size="small" plain @click="rowDel(index)">刪除</el-button>
</div>
<vxe-table
:data="tableData"
border
stripe
size="mini"
>
<vxe-column align="center" field="sectionDeptName" title="站段" min-width="180"></vxe-column>
<vxe-column align="center" field="userNumber" title="分配名額" min-width="180">
<template #default="{ row }">
<el-input-number v-model="row.userNumber" :min="0" size="small" :precision="0" :controls="false"/>
</template>
</vxe-column>
</vxe-table>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
// 期數(shù)
const periods = ref(0);
// 表格數(shù)據(jù)列表
const listDatas = ref([]);
// 初始表格數(shù)據(jù)
const initialData = [
{ sectionDeptName: '初中', userNumber: 100 },
{ sectionDeptName: '高中', userNumber: 50 },
{ sectionDeptName: '小學', userNumber: 60 },
{ sectionDeptName: '大學', userNumber: 30 }
];
// 期數(shù)變化處理
const handleChange = (e) => {
if (listDatas.value.length === 0) {
for (let i = 0; i < e; i++) {
listDatas.value.push(initialData);
}
} else {
let i = listDatas.value.length;
for (i; i < e; i++) {
listDatas.value.push(initialData);
}
}
};
// 行刪除事件
const rowDel = (index) => {
ElMessageBox.confirm('確定將選擇數(shù)據(jù)刪除?', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
listDatas.value.splice(index, 1);
}).then(() => {
ElMessage({
type: 'success',
message: '操作成功!'
});
});
};
</script>
<style scoped>
.content {
padding: 20px;
}
</style>問題原因
你遇到的問題是因為在生成多個表格時,所有表格的數(shù)據(jù)都引用了同一個對象或數(shù)組,導致數(shù)據(jù)聯(lián)動現(xiàn)象。要解決這個問題,你需要確保每個表格的數(shù)據(jù)是獨立的副本,而不是引用同一個對象或數(shù)組。
解決方案
在生成表格數(shù)據(jù)時,使用深拷貝來確保每個表格的數(shù)據(jù)是獨立的副本。


代碼如下
<template>
<div class="content">
<div style="margin: 20px;">
<span>期數(shù):</span>
<el-input-number v-model="periods" :min="0" size="small" :precision="0" :controls="false" @change="handleChange"/>
</div>
<div v-for="(tableData, index) in listDatas" :key="index" style="margin-top: 20px;">
<div style="width: 100%; text-align: right; margin-bottom: 5px;">
<el-button type="primary" size="small" plain @click="rowDel(index)">刪除</el-button>
</div>
<vxe-table
:data="tableData"
border
stripe
size="mini"
>
<vxe-column align="center" field="sectionDeptName" title="站段" min-width="180"></vxe-column>
<vxe-column align="center" field="userNumber" title="分配名額" min-width="180">
<template #default="{ row }">
<el-input-number v-model="row.userNumber" :min="0" size="small" :precision="0" :controls="false"/>
</template>
</vxe-column>
</vxe-table>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
// 期數(shù)
const periods = ref(0);
// 表格數(shù)據(jù)列表
const listDatas = ref([]);
// 初始表格數(shù)據(jù)
const initialData = [
{ sectionDeptName: '初中', userNumber: 100 },
{ sectionDeptName: '高中', userNumber: 50 },
{ sectionDeptName: '小學', userNumber: 60 },
{ sectionDeptName: '大學', userNumber: 30 }
];
// 期數(shù)變化處理
const handleChange = (e) => {
if (listDatas.value.length === 0) {
for (let i = 0; i < e; i++) {
let tableData = initialData.map(item => ({ ...item})); // 使用深拷貝
listDatas.value.push(tableData);
}
} else {
let i = listDatas.value.length;
for (i; i < e; i++) {
let tableData = initialData.map(item => ({ ...item})); // 使用深拷貝
listDatas.value.push(tableData);
}
}
};
// 行刪除事件
const rowDel = (index) => {
ElMessageBox.confirm('確定將選擇數(shù)據(jù)刪除?', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
listDatas.value.splice(index, 1);
}).then(() => {
ElMessage({
type: 'success',
message: '操作成功!'
});
});
};
</script>
<style scoped>
.content {
padding: 20px;
}
</style>到此這篇關于vue中循環(huán)表格數(shù)據(jù),出現(xiàn)數(shù)據(jù)聯(lián)動現(xiàn)象。的文章就介紹到這了,更多相關vue表格數(shù)據(jù)聯(lián)動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue如何通過ref調(diào)用router-view子組件的方法
這篇文章主要介紹了vue?通過ref調(diào)用router-view子組件的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
VUE + UEditor 單圖片跨域上傳功能的實現(xiàn)方法
這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實現(xiàn)方法,需要的朋友參考下2018-02-02
Vue報錯:TypeError:Cannot create property '
這篇文章主要介紹了Vue報錯:TypeError:Cannot create property 'xxx' on string 'xxxx'問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
關于Element上傳組件beforeUpload上傳前限制失效問題
這篇文章主要介紹了Element上傳組件beforeUpload上傳前限制失效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

