Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Vue.js Conditionally adding classes based on the values of a property

Hi all.

Well, I've been trying but I can't seem to find the expression I need to generate the right classes based on the property values in my Vue instance.

<article class="project-list">


                <h2>{{ list }}</h2> 
                <ul id="project-status">
                    <li v-for="status in projects">
                        <span v-bind:class="{'complete': projects[true], 'incomplete': !projects[false]}">{{ status.project_status }} </span>
                    </li>
                </ul>
                <ul id="project-names">
                    <li v-for="name in projects">{{ name.project_name }}</li>

                <!--
                <li><span class="incomplete"></span>Project 1</li>
                <li><span class="complete"></span>Project 2</li>
                <li><span class="complete"></span>Project 3</li>
                <li><span class="complete"></span>Form Project</li>
                <li><span class="incomplete"></span>Project 1</li>
                <li><span class="complete"></span>Project 2</li>
                <li><span class="complete"></span>Project 3</li>

                -->
            </ul>

        </article>

So in my code I have unordered list which I have a v-for directive which generates the list items I need. The span element inside project-status have CSS applied so it renders Green for a project that is complete and a red backround for incomplete projects.

No matter what I try however, I only get all 6 list items showing red or showing green.

Here's my Vue instance. Thanks :)

const vue = new Vue({

    el: '.project-list',
    data: {
        list: "Project List",
        projects:  [{
            project_name: "Vue Project",
            project_status: "Complete",
            status: true,
        },
        {
            project_name: "jQuery Project",
            project_status: "Incomplete",
            status: false,    
        },
        {
            project_name: "JavaScript",
            project_status: "Complete",
            status: true,
        },
        {
            project_name: "HTML5",
            project_status: "Complete",
            status: true,
        },
        {
            project_name: "PHP",
            project_status: "Incomplete",
            status: false,
        },
        {
            project_name: "Python",
            project_status: "Incomplete",
            status: false,
        }]
    },
    methods: {

    },

    computed: {

    }

});

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

Hi Jonathan,

Change the v-bind:class to the following:

<span v-bind:class="{'complete': status.status, 'incomplete': !status.status}">

It probably wouldn't hurt to change the v-for to v-for="project in projects so then the v-bind could be written as v-bind:class="{'complete': project.status, 'incomplete': !project.status} so it's easier to understand, but that's personal opinion and not judgement.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Thank you. I think I've got it now! It needed a reference to the status as defined in v-for to actually iterate through the values.

Now it is so clear! Appreciate your response! (I thought I'd tried every possible expression! :)