- Home
- Forum
- Angular - EJ 2
- Highlight searched text in grid
Highlight searched text in grid
Is there a way to highlight searched text in grid??
Snapshot attached.
Attachment: highlighttext_2a987e14.zip
|
export class AppComponent {
@ViewChild("grid") gridObj: GridComponent;
public data: Object[];
public pageSettings: Object;
public toolbar: string[];
public flag = false;
public word = '';
public ngOnInit(): void {
this.data = orderDataSource;
this.pageSettings = { pageSize: 8 };
this.toolbar = ["Search"];
}
public onActionBegin(args) {
if (args.requestType === "searching") {
if (args.searchString === "") {
this.flag = false;
} else {
this.flag = true;
this.word = args.searchString.toLowerCase();
}
}
}
public onActionComplete(args) {
if (args.requestType === "searching") {
this.flag = false;
}
}
public onQueryCellInfo(args) {
if (this.flag === true) {
var cellContent = args.data[args.column.field];
var parsedContent = cellContent.toString().toLowerCase();
if (parsedContent.indexOf(this.word.toLowerCase()) >= 0) {
var i = 0;
var searchStr = '';
while(i < this.word.length) {
var index = parsedContent.indexOf(this.word[i]);
searchStr = searchStr + cellContent[index];
i++;
}
args.cell.innerHTML= args.cell.innerText.replaceAll(searchStr, "<span class='customcss'>" + searchStr + "</span>")
}
}
}
}
|
|
.e-grid .customcss {
background: yellow;
}
|
Hi Joseph ,
Thanks for your quick response.
However, I am facing one more challenge with search. We have used pipes for currency and date.
Now when I search say 12(assume there's a row with 12 as currency, image attached), the resulted grid removes the currency symbol and shows just 12 instead of CNY12.
Same issue occurs with date(image attached).
I want to apply search such that my view does not gets hampered.
Search field should contain CNY12 and date should be 2021-07-22.
Before adding the highlight fix given by you, it was working fine.
Can you let us know the possible solution.
Attachment: Search_c75a372.zip
Hi Joseph,
Attaching the zip again.
For number we are using {{number | currency: 'USD'}}
For date , formats vary as per user's choice.
Attachment: Search_e44f16ad.zip
Hi Joseph,
The solution given by you doesn't seems to work for our requirement.
We have replicated the same in the stackblitz link which you provided:
https://stackblitz.com/edit/angular-xztu2u-ef7yfz?file=app.component.html
Can you please check this once.
|
public onQueryCellInfo(args) {
debugger;
if (this.flag === true) {
var cellContent = args.data[args.column.field].toString();
if (args.column.field == 'Freight') {
cellContent = 'INR' + args.data[args.column.field].toString();
}
if (cellContent) {
var parsedContent = cellContent.toString().toLowerCase();
if (parsedContent.indexOf(this.word.toLowerCase()) >= 0) {
var i = 0;
var searchStr = '';
while (i < this.word.length) {
var index = parsedContent.indexOf(this.word[i]);
searchStr = searchStr + cellContent[index];
i++;
}
args.cell['innerText'] = cellContent;
let regSearch = new RegExp(searchStr, 'ig'); // ignores case
args.cell.innerHTML = args.cell['innerText'].replaceAll(
regSearch,
"<span style='background:yellow;'>" + searchStr + '</span>'
);
}
}
}
}
|
- 8 Replies
- 2 Participants
-
RS Rohit Swarup
- Jun 28, 2021 10:08 AM UTC
- Jul 16, 2021 02:05 PM UTC