Home > Software design >  I am having a problem with v-if in a nested loop [vuejs]
I am having a problem with v-if in a nested loop [vuejs]

Time:11-01

My problem is I am trying to edit a role so I have two functions, one to bring back the role and its permissions and one to bring all permissions. I am trying to do a nested v-for loop to show all permissions and check the one that was already selected. everything works fine but I can't show all permissions for some reason. There is one more permission called "Delete user" that doesn't show up. anyway, the problem is 100% from the template side.

 <div class="container" v-for="(mission,key,index) in permissions" :key="key">
    <div class="form-group" v-for="(per,key,index) in selected" :key="key" v-if="mission.id == per.id">
        <input type="checkbox"  v-model="mission[key]"  checked >{{mission.name}}
        <div v-if="mission.id !== per.id">
        <input type="checkbox"  v-model="mission[key]">{{mission.name}}
        <p>fucking hell</p>
      </div>
   </div>                      
</div>
                    
<script>
export default {
    data(){
        return{
            role:'',
            permissions:[],
            selected:[],
        }
    },
    mounted(){
        this.showRole()
        this.getPermissions()
        
    },
    methods:{
        showRole(){
           
            this.axios.get(`/api/role/${this.$route.params.id}`).then(response=>{
                const{role ,selected}= response.data
                this.role= role
                this.selected = selected
            }).catch(error=>{
                console.log(error)
            })
        },
        getPermissions(){
            this.axios.get('/api/permissions').then(response=>{
                this.permissions =response.data
            }).catch(error=>{
                console.log(error)
            })
        },
    }
}
</script>

here is a picture of the API that returns all permission. you can see delete user is there at the end.

enter image description here

CodePudding user response:

Using v-if and v-for together is not recommended. Look at style guide for more info.

When used together with v-if, v-for has a higher priority than v-if.

  • Related