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個(gè)表格,其中名額分配一欄人數(shù)是可以編輯的,但是當(dāng)我修改第一個(gè)表格的數(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: '小學(xué)', userNumber: 60 },
{ sectionDeptName: '大學(xué)', 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>問題原因
你遇到的問題是因?yàn)樵谏啥鄠€(gè)表格時(shí),所有表格的數(shù)據(jù)都引用了同一個(gè)對象或數(shù)組,導(dǎo)致數(shù)據(jù)聯(lián)動現(xiàn)象。要解決這個(gè)問題,你需要確保每個(gè)表格的數(shù)據(jù)是獨(dú)立的副本,而不是引用同一個(gè)對象或數(shù)組。
解決方案
在生成表格數(shù)據(jù)時(shí),使用深拷貝來確保每個(gè)表格的數(shù)據(jù)是獨(dú)立的副本。


代碼如下
<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: '小學(xué)', userNumber: 60 },
{ sectionDeptName: '大學(xué)', 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>到此這篇關(guān)于vue中循環(huán)表格數(shù)據(jù),出現(xiàn)數(shù)據(jù)聯(lián)動現(xiàn)象。的文章就介紹到這了,更多相關(guān)vue表格數(shù)據(jù)聯(lián)動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談vue項(xiàng)目可以從哪些方面進(jìn)行優(yōu)化
本篇文章主要介紹了淺談vue項(xiàng)目可以從哪些方面進(jìn)行優(yōu)化,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
vue純前端使用exceljs導(dǎo)出excel文件方式
本文介紹了如何使用excel.js插件和Vue實(shí)現(xiàn)純前端Excel文件導(dǎo)出,并詳細(xì)步驟包括安裝插件、創(chuàng)建工作簿和工作表、定義列數(shù)據(jù)、設(shè)置樣式、行級操作、遍歷行和單元格、插入圖片和超鏈接2024-11-11
vue項(xiàng)目打包部署_nginx代理訪問方法詳解
今天小編就為大家分享一篇vue項(xiàng)目打包部署_nginx代理訪問方法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
使用 vue 實(shí)例更好的監(jiān)聽事件及vue實(shí)例的方法
這篇文章主要介紹了使用 vue 實(shí)例更好的監(jiān)聽事件及vue實(shí)例的方法,介紹了一種新增 vue 實(shí)例的方法,單獨(dú)監(jiān)聽事件,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
結(jié)合Vue控制字符和字節(jié)的顯示個(gè)數(shù)的示例
這篇文章主要介紹了結(jié)合Vue控制字符和字節(jié)的顯示個(gè)數(shù)的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
如何解決this.$refs.form.validate()不執(zhí)行的問題
這篇文章主要介紹了如何解決this.$refs.form.validate()不執(zhí)行的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
本文記錄作者在vue項(xiàng)目中使用到Google第三方登錄,查詢到的資料文檔也不詳細(xì),故此把自己所遇到的坑及問題詳細(xì)的記錄下來。2021-07-07

