我的实现方式是通过html中双层for循环做的
首先我们把数组数据处理一下,因为我们是每5个元素分成一个div
看下数据格式:
data() {
return {
examList: [
{
title: '正题',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题1',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
},
{
title: '正题2',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题3',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题4',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题5',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
]
};
},
<div class="fl" v-for="start in splitItemEveryFive(exam)">
.....
</div>
splitItemEveryFive(exam) {
return Math.floor(exam.listData.length / 5) + 1;
},
上面的代码我们先把他的父亲div写出来了, 例如我们有13个元素。那么我们就遍历4次父亲div,然后再子div中根据下标值循环5次
子div中的处理
<topic-no-item
v-for="item in afterSplitList(exam, start - 1)"
:topicIndex="item"
:hasChecked="false"
/>
afterSplitList(exam, start) {
return exam.listData.slice(start * SPLIT_NUM, start * SPLIT_NUM + SPLIT_NUM);
},
完整代码:
<template>
<main class="exam-no-main">
<div class="exam-no-wrapper">
<div class>
<exam-header height="44px" title="题目列表" />
</div>
<div class="exam-no-body">
<div v-for="exam in examList">
<div class="exam-no-body-title">{{ exam.title }}</div>
<div class="exam-no-body-list">
<div class="display-flex" v-for="start in splitItemEveryFive(exam)">
<topic-no-item
v-for="item in afterSplitList(exam, start - 1)"
:topicIndex="item"
:hasChecked="false"
/>
</div>
</div>
</div>
</div>
</div>
</main>
</template>
<script>
import TopicNoItem from './TopicNoItem';
import ExamHeader from '../components/ExamHeader';
const SPLIT_NUM = 5;
export default {
name: 'TopicNoList',
components: {
TopicNoItem,
ExamHeader,
},
data() {
return {
examList: [
{
title: '正题',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题1',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
},
{
title: '正题2',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题3',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题4',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
{
title: '正题5',
listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
]
};
},
created() { },
watch: {
},
methods: {
afterSplitList(exam, start) {
return exam.listData.slice(start * SPLIT_NUM, start * SPLIT_NUM + SPLIT_NUM);
},
splitItemEveryFive(exam) {
return Math.floor(exam.listData.length / 5) + 1;
},
},
};
</script>
<style lang='scss' scoped>
.exam-no-main {
background-color: #f0f2f5;
min-height: 100%;
position: relative;
width: 278px;
box-sizing: border-box;
}
$padding-top-bottom: 20px;
$padding-left-right: 20px;
.exam-no-wrapper {
background: #ffffff;
height: calc(100% - $padding-top-bottom * 2);
width: calc(100% - $padding-left-right * 2);
top: $padding-top-bottom;
left: $padding-left-right;
border-radius: 8px;
position: absolute;
.exam-no-body {
padding: 15px;
box-sizing: border-box;
// 45是头部题目列表的高度
height: calc(100% - 45px);
overflow-y: auto;
&-title {
font-size: 12px;
width: 90%;
text-align: left;
margin: 12px auto;
color: #909399;
}
.display-flex {
display: flex;
min-width: 160px;
}
&-list {
display: flex;
width: 90%;
margin: 0 auto;
flex-wrap: wrap;
justify-content: flex-start;
}
}
}
</style>