Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue nested v-for, get index of parent

Tags:

I have a nested v-for, basically, I want to get the index of the parent v-for and use it inside the child v-for

<template v-for="(branch, index) in $store.state.agency.branch_users.users">
    <table  class="table" id="agency-contact-table" >
        <thead>
            <tr>
                <th aria-colindex="1" class="">#</th>
                <th aria-colindex="1" class="">Name</th>
            </tr>
        </thead>
        <tbody>
            <tr v-if="branch.role_users" v-for="(branch_users, index) in branch.role_users">
                <td>{{$parent.$index}}</td>
                <td>{{branch_users.user.first_name}} {{branch_users.user.last_name}}</td>
            </tr>
        </tbody>
    </table>
</template>

I tried using $parent.$index but it still doesn't work. It doesn't show any errors but no data also. What should I do to get the index in my parent v-for?

like image 383
Kapitan Teemo Avatar asked Oct 20 '18 05:10

Kapitan Teemo


1 Answers

Just define the other index with another name, or name the first as parent_index

<template v-for="(branch, parent_index) in $store.state.agency.branch_users.users">
    <table  class="table" id="agency-contact-table" >
        <thead>
            <tr>
                <th aria-colindex="1" class="">#</th>
                <th aria-colindex="1" class="">Name</th>
            </tr>
        </thead>
        <tbody>
            <tr v-if="branch.role_users" v-for="(branch_users, index) in branch.role_users">
                <td>{{parent_index}}</td>
                <td>{{branch_users.user.first_name}} {{branch_users.user.last_name}}</td>
            </tr>
        </tbody>
    </table>
</template>
like image 169
Nermin Avatar answered Nov 15 '22 06:11

Nermin