The scenario is as follows...
Component template
<template>
<div>
<loader v-show="loading"></loader> // loading animation
<div v-show="!loading">
<div v-for="group in groups">
{{group.name}}
<div v-for="item in group.list">
{{item.name}}
</div>
</div>
</div>
</div>
</template>
Component data
data: function () {
return {
list: [],
groups: [],
loading: true
}
}
1. fetch 1 dimensional array from api
axios.get(API_URL).then(
(response) => {
this.list = response.data.payload;
}
);
The array structure is as follows...
[
{
"name": "bob",
"group": "A"
},
{
"name": "sally",
"group": "A"
},
{
"name": "john",
"group": "B"
},
{
"name": "jane",
"group": "B"
},
]
2. transform array into 2 dimensions using the group property of each item
current solution (blocking!, inefficient?)
// loading animation stops at this point
this.list.forEach((item, index) => {
let hasGroupChanged = false;
if (index === 0) {
hasGroupChanged = true;
} else {
let currentGroupName = item.group;
let previousGroupName = this.list[index - 1].group;
hasGroupChanged = previousGroupName !== currentGroupName;
}
if (hasGroupChanged) {
const group = {
group: item.group,
list: []
};
this.groups.push(group);
}
const groupIndex = this.groups.length - 1;
this.groups[groupIndex].list.push(item);
});
this.loading = false;
how to keep the loading animation going until groups have been populated?