I have the following html snippets
<ul>
<li *ngFor="let report of searchResult.reports let i = index;">
<input type="checkbox" [(ngModel)]="report.reportChecked" (click)="selectedReport(report, i)">
<p kf-text weight="heavy" [ngClass]="{'bgColor': showBgd}">{{ report.name }}</p>
</li>
</ul>
And
<ul>
<li *ngFor="let language of languages" (click)="selectedLang(language)">
{{ language.name }}
<li>
<ul>
When I click on a particular report or reports and then select a language, I need to add the class bgColor to the report or reports which does not have that language.
Example: I have 3 reports: Report 1(english) , Report 2(spanish) , Report 3(english, french)
And 3 languages: English, Spanish, French
If i check Report 1 and Report 2 which are available only in English and Spanish respectively and from the drop down I choose the language English, the class bgColor needs to be added to Report 2 as it is not available in the chosen language, in this case english. If I check Report 1 and Report 3 and language spanish is chosen, the class bgcolor needs to be added to both Report 1 and Report 3 elements. If English was chosen, the class will not be added to any element since both are available in english. Bottom line, whichever report is checked, if it is not available in the chosen language, the class bgColor is to be added. If it is available class will not be added. Reports can be checked or unchecked at will.
The following is the typescript snippet
selectedReport(report, index) {
if(report.reportChecked) {
this.selectedReports.push({
type: report.type,
name: report.name,
reportChecked: report.reportChecked
});
this.indexItems.push(index);
} else {
this.selectedReports = _.reject(this.selectedReports, (el) => {
return el.type === report.type;
});
let indexRemove = this.indexItems.indexOf(index);
if(indexRemove > -1) {
this.indexItems.splice(indexRemove, 1);
}
}
}
selectedLang(language) {
let reportType;
let reportLanguage;
this.selectedLanguage = language;
this.selectedReports.forEach((report) => {
if (report.reportChecked) {
reportType = this.reportsWithLang.find((checkedReport) => {
return checkedReport.type === report.type;
});
reportLanguage = reportType.languages.find((language) => {
return language.id === this.selectedLanguage.id;
});
if(!reportLanguage) {
this.showBgd= true;
} else {
this.showBgd= false;
}
}
});
}
In my current implementation, When Report 1 is checked and language English is chosen the bgColor class is not added. But if language other than English, example, spanish is chosen the bgColor class is added to all report elements even though only Report 1 is checked. Again keeping Report 1 checked and language chosen as spanish, if Report 2 is now checked, the class bgColor is removed from all elements although the class should be added to Report 1 element. I tried a lot of different ways but still could not come up with a solution. Any help on this will be mightily appreciated.