/* eslint-disable max-len */import { Component, ViewChild } from'@angular/core';
import { IgxTextHighlightDirective } from'igniteui-angular';
@Component({
selector: 'app-text-highlight-1',
styleUrls: ['./text-highlight-sample-1.component.scss'],
templateUrl: './text-highlight-sample-1.component.html'
})
exportclassTextHighlightSample1Component{
@ViewChild(IgxTextHighlightDirective, { read: IgxTextHighlightDirective, static: true })
public highlight: IgxTextHighlightDirective;
// tslint:disable max-line-lengthpublic html = `
Use the search box to search for a certain string in this text.
All the results will be highlighted in the same color with the exception of the
the first occurrence of the string, which will have a different color in order to tell it apart.
You can use the button in the searchbox to specify if the search will be case sensitive.
You can move the active highlight by either pressing the buttons on the searchbox or by using the Enter or the arrow keys on your keyboard.
`;
// tslint:enable max-line-lengthpublic searchText = '';
public matchCount = 0;
public caseSensitive = false;
public index = 0;
publicsearchKeyDown(ev) {
if (this.searchText) {
if (ev.key === 'Enter' || ev.key === 'ArrowDown' || ev.key === 'ArrowRight') {
ev.preventDefault();
this.findNext();
} elseif (ev.key === 'ArrowUp' || ev.key === 'ArrowLeft') {
ev.preventDefault();
this.findPrev();
}
}
}
publiconTextboxChange() {
this.index = 0;
this.find(0);
}
publicupdateSearch() {
this.caseSensitive = !this.caseSensitive;
this.find(0);
}
publicclearSearch() {
this.searchText = '';
this.find(0);
}
publicgetcanMoveHighlight() {
returnthis.matchCount > 1;
}
publicfindNext() {
this.find(1);
}
publicfindPrev() {
this.find(-1);
}
privatefind(increment: number) {
if (this.searchText) {
this.matchCount = this.highlight.highlight(this.searchText, this.caseSensitive);
this.index += increment;
this.index = this.index < 0 ? this.matchCount - 1 : this.index;
this.index = this.index > this.matchCount - 1 ? 0 : this.index;
if (this.matchCount) {
IgxTextHighlightDirective.setActiveHighlight('group1', {
index: this.index
});
}
} else {
this.highlight.clearHighlight();
}
}
}
ts
@Component({
...
})
exportclassHomeComponent{
public html = '...';
@ViewChild(IgxTextHighlightDirective, {read: IgxTextHighlightDirective})
public highlight: IgxTextHighlightDirective;
public searchText: string = '';
public matchCount: number = 0;
public caseSensitive: boolean = false;
public index: number = 0;
publicgetcanMoveHighlight() {
returnthis.matchCount > 1;
}
}
typescript
/* eslint-disable max-len */import { Component, ViewChild } from'@angular/core';
import { IgxTextHighlightDirective } from'igniteui-angular';
@Component({
selector: 'app-text-highlight-1',
styleUrls: ['./text-highlight-sample-1.component.scss'],
templateUrl: './text-highlight-sample-1.component.html'
})
exportclassTextHighlightSample1Component{
@ViewChild(IgxTextHighlightDirective, { read: IgxTextHighlightDirective, static: true })
public highlight: IgxTextHighlightDirective;
// tslint:disable max-line-lengthpublic html = `
Use the search box to search for a certain string in this text.
All the results will be highlighted in the same color with the exception of the
the first occurrence of the string, which will have a different color in order to tell it apart.
You can use the button in the searchbox to specify if the search will be case sensitive.
You can move the active highlight by either pressing the buttons on the searchbox or by using the Enter or the arrow keys on your keyboard.
`;
// tslint:enable max-line-lengthpublic searchText = '';
public matchCount = 0;
public caseSensitive = false;
public index = 0;
publicsearchKeyDown(ev) {
if (this.searchText) {
if (ev.key === 'Enter' || ev.key === 'ArrowDown' || ev.key === 'ArrowRight') {
ev.preventDefault();
this.findNext();
} elseif (ev.key === 'ArrowUp' || ev.key === 'ArrowLeft') {
ev.preventDefault();
this.findPrev();
}
}
}
publiconTextboxChange() {
this.index = 0;
this.find(0);
}
publicupdateSearch() {
this.caseSensitive = !this.caseSensitive;
this.find(0);
}
publicclearSearch() {
this.searchText = '';
this.find(0);
}
publicgetcanMoveHighlight() {
returnthis.matchCount > 1;
}
publicfindNext() {
this.find(1);
}
publicfindPrev() {
this.find(-1);
}
privatefind(increment: number) {
if (this.searchText) {
this.matchCount = this.highlight.highlight(this.searchText, this.caseSensitive);
this.index += increment;
this.index = this.index < 0 ? this.matchCount - 1 : this.index;
this.index = this.index > this.matchCount - 1 ? 0 : this.index;
if (this.matchCount) {
IgxTextHighlightDirective.setActiveHighlight('group1', {
index: this.index
});
}
} else {
this.highlight.clearHighlight();
}
}
}
ts
/* eslint-disable max-len */import { Component, ViewChildren } from'@angular/core';
import { IgxTextHighlightDirective } from'igniteui-angular';
@Component({
selector: 'app-text-highlight-2',
styleUrls: ['./text-highlight-sample-2.component.scss'],
templateUrl: './text-highlight-sample-2.component.html'
})
exportclassTextHighlightSample2Component{
@ViewChildren(IgxTextHighlightDirective)
public highlights;
// tslint:disable max-line-lengthpublic firstParagraph = `
Use the search box to search for a certain string in this text.
All the results will be highlighted in the same color with the exception of the
the first occurrence of the string, which will have a different color in order to tell it apart.
You can use the button in the searchbox to specify if the search will be case sensitive.
You can move the active highlight by either pressing the buttons on the searchbox or by using the Enter or the arrow keys on your keyboard.
`;
public secondParagraph = `
On top of the functionality from the previous sample, this sample demonstrates how to implement the text highlight directive
with several different elements. In this case, we have two div elements, each containing some text. You can see that
they share the same active highlight and the returned match count includes both elements. The find method in this
sample can be reused regardless of the number of elements you have in your application.
`;
public searchText = '';
public matchCount = 0;
public caseSensitive = false;
public index = 0;
publicsearchKeyDown(ev) {
if (this.searchText) {
if (ev.key === 'Enter' || ev.key === 'ArrowDown' || ev.key === 'ArrowRight') {
ev.preventDefault();
this.findNext();
} elseif (ev.key === 'ArrowUp' || ev.key === 'ArrowLeft') {
ev.preventDefault();
this.findPrev();
}
}
}
publiconTextboxChange() {
this.index = 0;
this.find(0);
}
publicupdateSearch() {
this.caseSensitive = !this.caseSensitive;
this.find(0);
}
publicclearSearch() {
this.searchText = '';
this.find(0);
}
publicgetcanMoveHighlight() {
returnthis.matchCount > 1;
}
publicfindNext() {
this.find(1);
}
publicfindPrev() {
this.find(-1);
}
privatefind(increment: number) {
if (this.searchText) {
let count = 0;
const matchesArray = [];
this.highlights.forEach((h) => {
count += h.highlight(this.searchText, this.caseSensitive);
matchesArray.push(count);
});
this.matchCount = count;
this.index += increment;
this.index = this.index < 0 ? this.matchCount - 1 : this.index;
this.index = this.index > this.matchCount - 1 ? 0 : this.index;
if (this.matchCount) {
let row;
for (let i = 0; i < matchesArray.length; i++) {
if (this.index < matchesArray[i]) {
row = i;
break;
}
}
const actualIndex = row === 0 ? this.index : this.index - matchesArray[row - 1];
IgxTextHighlightDirective.setActiveHighlight('group1', { index: actualIndex, row });
}
} else {
this.highlights.forEach((h) => {
h.clearHighlight();
});
this.matchCount = 0;
}
}
}
ts
/* eslint-disable max-len */import { Component, ViewChild } from'@angular/core';
import { IgxTextHighlightDirective } from'igniteui-angular';
@Component({
selector: 'app-text-highlight-style',
styleUrls: ['./text-highlight-style.component.scss'],
templateUrl: './text-highlight-style.component.html'
})
exportclassTextHighlightStyleComponent{
@ViewChild(IgxTextHighlightDirective, { read: IgxTextHighlightDirective, static: true })
public highlight: IgxTextHighlightDirective;
public html = `
Use the search box to search for a certain string in this text.
All the results will be highlighted in the same color with the exception of the
the first occurrence of the string, which will have a different color in order to tell it apart.
You can use the button in the searchbox to specify if the search will be case sensitive.
You can move the active highlight by either pressing the buttons on the searchbox or by using the Enter or the arrow keys on your keyboard.
`;
public searchText = '';
public matchCount = 0;
public caseSensitive = false;
public index = 0;
publicsearchKeyDown(ev) {
if (this.searchText) {
if (ev.key === 'Enter' || ev.key === 'ArrowDown' || ev.key === 'ArrowRight') {
ev.preventDefault();
this.findNext();
} elseif (ev.key === 'ArrowUp' || ev.key === 'ArrowLeft') {
ev.preventDefault();
this.findPrev();
}
}
}
publiconTextboxChange() {
this.index = 0;
this.find(0);
}
publicupdateSearch() {
this.caseSensitive = !this.caseSensitive;
this.find(0);
}
publicclearSearch() {
this.searchText = '';
this.find(0);
}
publicgetcanMoveHighlight() {
returnthis.matchCount > 1;
}
publicfindNext() {
this.find(1);
}
publicfindPrev() {
this.find(-1);
}
privatefind(increment: number) {
if (this.searchText) {
this.matchCount = this.highlight.highlight(this.searchText, this.caseSensitive);
this.index += increment;
this.index = this.index < 0 ? this.matchCount - 1 : this.index;
this.index = this.index > this.matchCount - 1 ? 0 : this.index;
if (this.matchCount) {
IgxTextHighlightDirective.setActiveHighlight('group1', {
index: this.index
});
}
} else {
this.highlight.clearHighlight();
}
}
}
ts