0

I faced one problem when I put multi-class binding in Vue2.

<div class="list-item clearfix" v-on:click="selectItem(trail)" :class="popupMode ? 'popup' : ''">

Here popupMode is props and I'd like to add one more class binding when I select the Item(click function:selectItem()).

For example, similar to selected.

This class is defined. How can I manage this problem?

2 Answers 2

1

Update HTML class bind like:

v-bind:class="{ popup: popupMode, selected: isSelected }"

where isSelected is a new prop like popupMode. On element click when selectItem method is called, set the bool isSelected prop to true and you will be able to see selected class after that.

JS:

data: {
    popupMode: true,
    isSelected: false
},
methods: {
    selectItem() {
        this.isSelected= true;
    }
}

For more info check this: Binding HTML Classes

Sign up to request clarification or add additional context in comments.

6 Comments

@palasH, it works for me. but I have a problem to use this. for example, there are 5 items, when I chick the first item, it'll be changed to grey color. then when I next(second) item, I'd like to change the first item to original status and second item is be changed to grey color too. In this case, how can I do it?
You can create a default class with black or any other color which you want to set as original status color. then update bind class for all like: { popup: popupMode, selected: isSelected, default: !isSelected }. This way only one will be set as selected class at one time.
default class is class="list-item clearfix". in this case, how can I call it?
{ 'list-item': !isSelected, 'clearfix': !isSelected }
well... it doesn't work for me. I'd like to discuss more details about this. where can we do it?
|
0

You can try this method:

{ active: isActive, 'text-danger': hasError }

Don't forget place isActive and hasError in data (in this case). I hope this help's.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.