3

I have a list group and I want to add a css class when one item is selected using ngClass directive

how to verify is the item is selected?

I only have one option.. this is a sidebar component, and when I pick an option, it has to send it to the parent (I did that with @Output) and also have a css class on selected option...

this is my component.html

<ul class="list-group">
    <li *ngFor="let option of options" (click)="selectOption(option)" class="list-group-item" [ngClass]="option ? 'activate-class' : 'deactivate-class'">
        {{option.title}}
    </li>
</ul>

component.ts

  @Output() optionSelected: EventEmitter<any> = new EventEmitter();
  options = [
    {
        title: "Activate account",
        value: NavigationOptions.ACTIVATE_VENDOR
    },
    {
        title: "Deactivate account",
        value: NavigationOptions.DEACTIVATE_VENDOR
    }
];

  constructor() { }

  ngOnInit() {
  }

  selectOption(option) {
    this.optionSelected.emit(option);
  }
2
  • It depends on what selectOption() does, i.e. how you store the selected option(s) in your component. Add a method isOptionSelected(option)in your component, and use it in your template. Commented Dec 10, 2019 at 10:15
  • Show selectOption(option) Commented Dec 10, 2019 at 10:16

3 Answers 3

3

You could store the selected option in a variable and check for that:

public selectedOption;

selectOption(option) {
  this.optionSelected.emit(option);
  this.selectedOption = option;
}

and then check in the ngClass for this variable:

[ngClass]="option === selectedOption ? 'activate-class' : 'deactivate-class'"

or if you only want to set activate-class and don't need deactivate-class:

[class.activate-class]="option === selectedOption"
Sign up to request clarification or add additional context in comments.

Comments

2

There is no need to over-complicate things or bloat your ts file with code.

You can simply have a selectOption in the ts file.

TS :

selectOption; // If your option is an object, which it is - then consider adding an interface for it here    

There is also no need to have 'deactivate-class', if the class doesn't exist on it, we can assume it doesn't apply.

HTML :

<ul class="list-group">
  <li *ngFor="let option of options" (click)="selectOption = option" class="list-group-item" [ngClass]="{'activate-class' : selectOption === option}">
    {{option.title}}
  </li>
</ul>

Comments

0

you can store the selected option in a variable and show the class name dynamically based on that

Try like this:

HTML:

<ul class="list-group">
    <li *ngFor="let option of options" (click)="selectOption(option);selectedOption = option.title" class="list-group-item" [ngClass]="selectedOption == option.title ? 'activate-class' : 'deactivate-class'">
        {{option.title}}
    </li>
</ul>

Working Demo

5 Comments

that can be useful when selecting multiple items
i have updated my question with all my code from my .ts file; I only have one option.. this is a sidebar component, and when I pick an option, it has to send it to the parent (I did that with @Output) and also have a css class on selected option...
Try the modified answer
one last question... how can i make one option to be selected by default?
In the .ts file, set selectedOption = "Activate account"

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.