Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

Vue3: How to add a conditional class to a specific label in a table row.

I am very new to Vue 3. I have spent too long using raw JS and the transition to Vue is a challenge!

I have got the below code working fine. It is a dynamic table with checkboxes. I click a text box and I get the data relevant to the index in the array. So 99% of people would be happy. Not me - I want the label Absent/Present to have the class present applied to it, effectively giving it a green colour. If I click off the checkbox, I want the class to be removed. So far, all my efforts incorrectly apply the class to ALL the labels. I just want the labels which are true. Not that it should be relevant, but I am also using inertia. So - what am I doing wrong. I know it should be easy ...... when you know how.

Many Thanks in advance!

    <template>
            <div class="container">
                <h5 class="heading ml-4">Children Absent / Present</h5>
                <div class="border_charts">
                    <table class="table ">
                        <thead>
                        <tr class="col-4">
                            <th class="tableHeading col-4">First Name</th>
                            <th class="tableHeading col-4">Last Name</th>
                            <th class="tableHeading col-4 ">Absent / Present</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr v-for="child in Children" :key="child.child_id">
                            <td class="col-4"><a :href="'/getchild/'+ child.child_id">{{child.childFirstName}}</a></td>
                            <td class="col-4"> {{child.childLastName}}</td>
                            <td class="col-4">
                                <div class="form-check form-switch" >
                                    <input
                                        type="checkbox"
                                        class="form-check-input"
                                        role="switch"
                                        :id="child.child_id"
                                        v-model="child.status"
                                        :true-value="1"
                                        :false-value="0"
                                        @change="updateChild(child)"
                                    >
			// HERE IS THE LABEL - THIS DOES NOT WORK CORRECTLY. // 
                                    <div v-if="true">
                                    <label  class="form-check-label"  v-bind:for="child.child_id ">Absent / Present</label> 
                                    </div>

                                </div>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </template>



    <script>
    export default {

        props: {
            Children:{
                Children:Array
             },
        },

        data() {
            return {
                active:false,
                inputClass: 'present'
            }
        },

        methods:{
            updateChild(child){
                this.changedChild = child;
                console.log( this.changedChild);
                this.inputClass = 'present'
                this.active = ! this.active
            },
        },
    }
    </script>

    <style scoped>
     .present{
         background-color: #5ba17a;
         border-radius:0.25rem;
         width:auto;
         color:white;
         text-align: center;
     }
    </style>


0 likes
2 replies
jbloomstrom's avatar
<div class="form-check form-switch" >
    <input
        type="checkbox"
        class="form-check-input"
        role="switch"
        :id="child.child_id"
        v-model="child.status"
        :true-value="1"
        :false-value="0"
        @change="updateChild(child)"
    >    
    <label :class="[child.status ? 'present' : 'absent']" class="form-check-label"  
        v-bind:for="child.child_id ">
        {{ child.status ? 'Present' : 'Absent'}}
    </label> 
</div>
vincej's avatar
Level 15

@jbloomstrom Hi! Many thanks for helping out! Unfortunately the new label is not working the way I need. Two things are happening: first when you open the page view for the first time it is applying the .present class (gives a green background) to every entry on the table whether the child is absent or present. I need it to be on only those children who are present. Children who are not present have no .present class and it will be just a plain background. To that end I have added an .absent class. To help out, I have included an image at the bottom of this post.

Secondly, when a user clicks on the 'absent/present' label all that I need is that the green background .present class appears or disappears. Present = green, Absent = no green, just plain. What you have done is super clever. If you look at the second image where there is a switch you will see that 'absent/present' label stays put regardless of the status. This code was produced with Vue 2. Unfortunately, Vue 3 does not easily support switches, Many thanks for all your help, and education!

https://imgur.com/a/2u23M7t

Please or to participate in this conversation.