需求效果

數據格式
list:[ { 'name':'項目1', items:[ { 'zysx':'n','ks':'科室1'}, { 'zysx':'n','ks':'科室1'}, { 'zysx':'n','ks':'科室1'} ] }, { 'name':'項目2', items:[ { 'zysx':'n2','ks':'科室2'}, { 'zysx':'n2','ks':'科室2'}, { 'zysx':'n2','ks':'科室2'} ] },{ 'name':'項目3', items:[ { 'zysx':'n3','ks':'科室3'}, { 'zysx':'n3','ks':'科室3'}, { 'zysx':'n3','ks':'科室3'} ] } ]
首先要明白什么循環時{{index}}與{{!index}},前者輸出序號,后者輸出的是boolean
關鍵代碼:
<template>
<div class="sss">
<a>准備:先外層循環,查看數據格式及{{!index}}和{{index}}的區別</a>
<table border="1px solid red;" width="600px">
<tr><th>項目</th><th>注意事項</th><th>科室</th></tr>
<tbody>
<tr v-for="(item1,index) in list">
<td>{{index}}</td><td>{{!index}}</td><td>{{item1.items}}</td>
</tr>
</tbody>
</table>
<a>第1步:簡單的循環外層數據到tr</a>
<table border="1px solid red;" width="600px">
<tr><th>項目</th><th>注意事項</th><th>科室</th></tr>
<tbody>
<tr v-for="(item1) in list">
<td>{{item1.name}}</td><td>{{item1.items}}</td><td>{{item1.items}}</td>
</tr>
</tbody>
</table>
<a>第2步:里面的列表數據想要單個顯示需再在第1步內部加個循環</a>
<table border="1px solid red;" width="600px">
<tr><th>項目</th><th>注意事項</th><th>科室</th></tr>
<tbody v-for="item1 in list">
<tr v-for="(item2,index) in item1.items">
<td >{{item1.name}}</td><td>{{item2}}</td><td>{{item2}}</td>
</tr>
</tbody>
</table>
<a>第3步:將第2步中的外層數據合並行。此時用到{{!index}},從准備中可以看到,所引為0時返回true,因此合並行時,false的td都不生成,true的跨行</a>
<table border="1px solid red;" width="600px">
<tr><th>項目</th><th>注意事項</th><th>科室</th></tr>
<tbody v-for="(item1,index) in list">
<tr v-for="(item2,index2) in item1.items">
<td v-if="!index2" :rowspan="item1.items.length">{{item1.name}}</td><td>{{item2}}</td><td>{{item2}}</td>
</tr>
</tbody>
</table>
<a>第4步:將第4步中的內層循環出來的對象優化展示</a>
<table border="1px solid red;" width="600px">
<tr><th>項目</th><th>注意事項</th><th>科室</th></tr>
<tbody v-for="item1 in list">
<tr v-for="(item2,index) in item1.items">
<td v-if="!index" :rowspan="item1.items.length">{{item1.name}}</td><td>{{item2.zysx}}</td><td>{{item2.ks}}</td>
</tr>
</tbody>
</table>
<br/>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
list:[
{
'name':'項目1',
items:[
{ 'zysx':'n','ks':'科室1'},
{ 'zysx':'n','ks':'科室1'},
{ 'zysx':'n','ks':'科室1'}
]
},
{
'name':'項目2',
items:[
{ 'zysx':'n2','ks':'科室2'},
{ 'zysx':'n2','ks':'科室2'},
{ 'zysx':'n2','ks':'科室2'}
]
},{
'name':'項目3',
items:[
{ 'zysx':'n3','ks':'科室3'},
{ 'zysx':'n3','ks':'科室3'},
{ 'zysx':'n3','ks':'科室3'}
]
}
]
}
},
methods: {
},
mounted() {
}
}
</script>
<style>
</style>
