項目中碰到的一個小需求:分頁請求數據,一次請求60條,需要將后台返回的數組每10條分成一組渲染一個表格
實現邏輯: var chunk = 10; var len = res.data.content.length; var result = []; for (let i = 0; i < len; i += chunk) { result.push(res.data.content.slice(i, i + chunk)) // 每10項分成一組 }
result = result.map((item, index) => { //遍歷,每組加上表頭和data
return {
columns: [{
title: '列號',
dataIndex: 'index',
align: 'center',
width: 60
},
{
title: '特征',
dataIndex: 'field',
align: 'center',
width: 110
},
{
title: '分類',
dataIndex: 'type',
align: 'center',
width: 110
}],
data: item
}
})
this.tableData = result
//表格渲染部分
<a-table
v-for="(item,index) in tableData"
:key="index"
:columns="item.columns"
:dataSource="item.data"
bordered
:rowKey="record=>record.id"
>
<template slot="title">
組{{index+1}}
</template>
</a-table>