Vue
HTML
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.js"></script>
<div id="app">
<h2>Phone tree:</h2>
<ul>
<li v-for="({group, subitems}) in phoneGroups" :key="group.id">
<span>{{group.name}}</span>
<ol>
<li v-for="subitem in subitems">-- {{subitem.name}}</li>
</ol>
</li>
</ul>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
Vue
new Vue({
el: "#app",
data: {
phoneList: []
},
mounted() {
axios.get('https://private-4f7c1-zyl1.apiary-mock.com/questions')
.then(response=> {
console.log(response);
this.phoneList = response.data;
})
.catch(error => {
console.log(error);
});
},
computed: {
phoneGroups() {
const data = {};
const groups = this.phoneList.filter(x => x.pid === 0);
for (const g of groups) {
const subitems = this.phoneList.filter(x => x.pid === g.id);
data[g.id] = {
group: g,
subitems,
};
}
return data;
}
}
})