vue中循環(huán)表格數(shù)據(jù)出現(xiàn)數(shù)據(jù)聯(lián)動(dòng)現(xiàn)象(示例代碼)
vue中循環(huán)表格數(shù)據(jù),出現(xiàn)數(shù)據(jù)聯(lián)動(dòng)現(xiàn)象。
問(wèn)題描述:如圖
我輸入期數(shù)為4,會(huì)循環(huán)出來(lái)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>
問(wèn)題原因
你遇到的問(wèn)題是因?yàn)樵谏啥鄠€(gè)表格時(shí),所有表格的數(shù)據(jù)都引用了同一個(gè)對(duì)象或數(shù)組,導(dǎo)致數(shù)據(jù)聯(lián)動(dòng)現(xiàn)象。要解決這個(gè)問(wèn)題,你需要確保每個(gè)表格的數(shù)據(jù)是獨(dú)立的副本,而不是引用同一個(gè)對(duì)象或數(shù)組。
解決方案
在生成表格數(shù)據(jù)時(shí),使用深拷貝來(lái)確保每個(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)動(dòng)現(xiàn)象。的文章就介紹到這了,更多相關(guān)vue表格數(shù)據(jù)聯(lián)動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue實(shí)現(xiàn)兩個(gè)列表之間的數(shù)據(jù)聯(lián)動(dòng)的代碼示例
- vue+antDesign實(shí)現(xiàn)樹形數(shù)據(jù)展示及勾選聯(lián)動(dòng)
- Vue動(dòng)態(tài)數(shù)據(jù)實(shí)現(xiàn)?el-select?多級(jí)聯(lián)動(dòng)、數(shù)據(jù)回顯方式
- Vue聯(lián)動(dòng)Echarts實(shí)現(xiàn)數(shù)據(jù)大屏展示
- vue watch深度監(jiān)聽(tīng)對(duì)象實(shí)現(xiàn)數(shù)據(jù)聯(lián)動(dòng)效果
相關(guān)文章
vue如何通過(guò)ref調(diào)用router-view子組件的方法
這篇文章主要介紹了vue?通過(guò)ref調(diào)用router-view子組件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法
這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法,需要的朋友參考下2018-02-02Vue報(bào)錯(cuò):TypeError:Cannot create property '
這篇文章主要介紹了Vue報(bào)錯(cuò):TypeError:Cannot create property 'xxx' on string 'xxxx'問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08關(guān)于Element上傳組件beforeUpload上傳前限制失效問(wèn)題
這篇文章主要介紹了Element上傳組件beforeUpload上傳前限制失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03