Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,956 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,660 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 90,956 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! :)