欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中循環(huán)表格數(shù)據(jù)出現(xiàn)數(shù)據(jù)聯(lián)動(dòng)現(xiàn)象(示例代碼)

 更新時(shí)間:2024年11月08日 14:23:18   作者:jieyucx  
在Vue中循環(huán)生成表格數(shù)據(jù)時(shí),可能會(huì)遇到數(shù)據(jù)聯(lián)動(dòng)的現(xiàn)象,即修改一個(gè)表格中的數(shù)據(jù)后,其他表格的數(shù)據(jù)也會(huì)跟著變化,這種現(xiàn)象通常是因?yàn)樗斜砀竦臄?shù)據(jù)引用了同一個(gè)對(duì)象或數(shù)組導(dǎo)致的,本文介紹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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue中的v-if基本使用(最新推薦)

    vue中的v-if基本使用(最新推薦)

    v-if根據(jù)表達(dá)式的真假,切換元素的顯示和隱藏操作DOM元素,這篇文章主要介紹了vue中的v-if基本使用,需要的朋友可以參考下
    2022-12-12
  • vue3如何在setup中獲取DOM元素

    vue3如何在setup中獲取DOM元素

    這篇文章主要介紹了vue3如何在setup中獲取DOM元素問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue如何通過(guò)ref調(diào)用router-view子組件的方法

    vue如何通過(guò)ref調(diào)用router-view子組件的方法

    這篇文章主要介紹了vue?通過(guò)ref調(diào)用router-view子組件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • 在vue3.0中如何配置代理

    在vue3.0中如何配置代理

    這篇文章主要介紹了在vue3.0中如何配置代理問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue使用$emit傳遞參數(shù)方式

    vue使用$emit傳遞參數(shù)方式

    這篇文章主要介紹了vue使用$emit傳遞參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • vue-router 4使用實(shí)例詳解

    vue-router 4使用實(shí)例詳解

    雖然 vue-router 4 大多數(shù) API 保持不變,但是在 vue3 中以插件形式存在,所以在使用時(shí)有一定的變化。接下來(lái)就學(xué)習(xí)學(xué)習(xí)它是如何使用的
    2021-11-11
  • VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法

    VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法

    這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法,需要的朋友參考下
    2018-02-02
  • vue長(zhǎng)按事件touch示例詳解

    vue長(zhǎng)按事件touch示例詳解

    這篇文章主要介紹了vue長(zhǎng)按事件touch,文末給大家補(bǔ)充介紹了Vue長(zhǎng)按觸摸事件的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Vue報(bào)錯(cuò):TypeError:Cannot create property 'xxx' on string 'xxxx'問(wèn)題

    Vue報(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)題

    關(guān)于Element上傳組件beforeUpload上傳前限制失效問(wèn)題

    這篇文章主要介紹了Element上傳組件beforeUpload上傳前限制失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評(píng)論