I am working with two vue components.
The first component (parent component) looks like this:
<template>
...
<ul class="filter-category" v-for="list in categories">
<list-category :data="list" :category-id="categoryId"></list-category>
</ul>
...
</template>
<script>
...
export default {
...
data() {
return {
categories: [
{
id: 1,
name: 'England',
children: [
{
id: 3,
name: 'Chelsea',
children: [
{id: 7, name: 'Hazard'},
{id: 8, name: 'Morata'}
]
},
{
id: 4,
name: 'Manchester United',
children: [
{id: 9, name: 'Pogba'},
{id: 10, name: 'Lukaku'}
]
}
]
},
{
id: 2,
name: 'Spain',
children: [
{
id: 5,
name: 'Real Madrid',
children: [
{id: 11, name: 'Ronaldo'},
{id: 12, name: 'Bale'}
]
},
{
id: 6,
name: 'Barcelona',
children: [
{id: 13, name: 'Messi'},
{id: 14, name: 'Suarez'}
]
},
]
}
],
categoryId: 7
}
}
}
</script>
The second component (child component) is structured as follows:
<template>
<li>
<!--parent-->
<a v-if="isFolder" href="javascript:" @click="toggle">
<span class="fa fa-fw" :class="icon"></span> {{data.name}}
</a>
<!--if not folding, we do not need an binding event-->
<a v-else href="javascript:" :title="data.name"><span class="fa fa-fw fa-circle-o"></span> {{data.name}}</a>
<!--children-->
<ul v-if="isFolder" :class="isShow">
<list-category v-for="(data, index) in data.children" :key="index" :data="data" :search="search"></list-category>
</ul>
</li>
</template>
<script>
export default {
props: ['data', 'categoryId'],
data() {
return {
open: false
}
},
computed: {
icon() {
return {
'fa-plus': !this.open,
'fa-minus': this.open,
}
},
isFolder() {
return this.data.children && this.data.children.length
},
isShow() {
return this.open ? 'show': 'hide'
}
},
methods: {
toggle() {
this.open = !this.open
}
}
}
</script>
If the prop categoryId
matches the category ID in categories
, then I want that specific category to be active.
In my scenario, I would like the "Hazard" category to be active like this:
==========================================================================
https://i.stack.imgur.com/0gmy9.png
==========================================================================
How can I achieve this?