Добавить следующий код в ваш код
app.component.ts
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'
@Pipe({ name: 'getHtml' })
export class HighlihtText implements PipeTransform {
constructor(private sanitized: DomSanitizer) { }
transform(value, searchText) {
if(searchText=='' || !searchText){
return value;
}
console.log("value="+value)
var str = value.toLowerCase();
searchText=searchText.toLowerCase();
var currHtml = "";
var ind = -1;
while (str.indexOf(searchText) >= 0) {
ind = str.indexOf(searchText);
createHtml(value.substr(0, ind),value.substr(ind,searchText.length))
str = str.substr(ind + searchText.length)
value=value.substr(ind + searchText.length);
}
if (str.length > 0) {
currHtml = currHtml + str;
}
function createHtml(nohighlighText,match) {
console.log(nohighlighText)
currHtml = currHtml + nohighlighText + "<span class=\"searcHighLight\" >" + match + "</span>"
}
return this.sanitized.bypassSecurityTrustHtml(currHtml);
}
}
В app.component.html изменить часть, гдеВы выделяете результат поиска в
<a class="userlist" (click)="selectedFacility(memberFacility.facilityID)" [innerHtml]="memberFacility.facilityName | getHtml : sLetter">
</a>
В app.module.ts объявляете недавно созданный канал
import { AppComponent ,HighlihtText} from './app.component';
declarations: [ AppComponent, HelloComponent,facilityFilterPipe,HighlihtText ],
Для проблемы поиска ALL
со сбросом изменений app.component.ts добавить следующую строку в конце метода searchFacility (..)
if(search==''){
this.searchFname="";
}
и инициализировать переменную searchFname
следующим образом
searchFname:String;
Для элемента Highligting fList
, изменить
<a (click)="selectedFacility(fList.facilityID)" [innerHtml]="fList.facilityName | getHtml : sLetter">
На
<a (click)="selectedFacility(fList.facilityID)">
<strong *ngIf="sLetter!=''" [innerHtml]="fList.facilityName | getHtml : sLetter"></strong>
<strong *ngIf="sLetter==''">{{fList.facilityName}}</strong>
</a>
и инициализируйте sLetter
в app.component.ts ngOnInit()
как
this.sLetter="";
URL-адрес StackBlitz: https://stackblitz.com/edit/angular-ku9aaj
Дайте мне знать, если у вас есть какие-либо вопросы