我试图做一个包含国家GDP的表格。当任何人单击州行时,将显示子表(地区 GDP 表)。它工作正常,但子表在重复。我制作了一个切换功能来控制子表的隐藏/显示。谁能找出为什么子表重复?解决方案将不胜感激。
运行代码段以正确理解问题。
var app = new Vue({
el: "#app",
data(){
return {
opened:[],
stateGDP: [
{ State: "Rajasthan", "1999-00": "2547", "2000-01": "3679",Id:"23" },
{ State: "Orissa", "1999-00": "38714", "2000-01": "38814",Id:"24" }
],
DistrictGDP: [
{
State: "Rajasthan",
District: "Jaipur",
"1999-00": "2547",
"2000-01": "3679",
Id:"23"
},
{
State: "Rajasthan",
District: "Jodhpur",
"1999-00": "2557",
"2000-01": "3639",
Id:"23"
},
{
State: "Orissa",
District: "Bhubaneswar",
"1999-00": "1983",
"2000-01": "2068",
Id:"24"
},
{
State: "Orissa",
District: "Puri",
"1999-00": "1923",
"2000-01": "2008",
Id:"24"
}
]
}
},
methods:{
toggle:function(Id) {
const index = this.opened.indexOf(Id);
if (index > -1) {
this.opened.splice(index, 1)
} else {
this.opened.push(Id)
}
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="table">
<table class="table-fill">
<thead>
<tr>
<th class="text-center">State/UT</th>
<th class="text-center">1999-00</th>
<th class="text-center">2000-01</th>
</tr>
</thead>
<template v-for="row in stateGDP">
<tr @click="toggle(row.Id)" :class="{ opened: opened.includes(row.Id) }">
<td>{{ row.State }}</td>
<td>{{ row['1999-00'] }}</td>
<td>{{ row['2000-01'] }}</td>
</tr>
<template v-for="(xy, indexStop) in DistrictGDP" v-if="opened.includes(row.Id) && row.Id==xy.Id">
<thead>
<tr>
紫衣仙女
相关分类