I have a challenge with implementing a background color change when clicking on a th
element. Currently, it works fine when I click on it, but I would like to make it so that if I click on another th
element, the previous one will turn off. However, if I click on the same th
again, it should not turn off until I switch to another th
. I am working with Angular 8 and my directive code is as follows:
@Directive({
selector: '[sortColumn]'
})
export class SortDirective implements OnInit {
@Input() data: any;
@Input('sortKey') key: any;
@Input('sortNumber') num: number;
private toggleSort: boolean = false;
constructor(private el: ElementRef, private renderer: Renderer) {
}
ngOnInit() {
this.renderer.listen(this.el.nativeElement, 'click', (event) => {
let parentNode = this.el.nativeElement.parentNode;
let children = parentNode.children;
if (this.data && this.key) {
let sortedData: any = this.sortArray();
}
if (this.data && this.num) {
let sortedData: any = this.sortNums()
}
this.changeTextColor()
})
}
changeTextColor() {
this.renderer.setElementStyle(this.el.nativeElement,"background-color","green");
this.toggleSort = !this.toggleSort;
}
}