Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi text search with angular material table

The search currently works fine. But I have a specific requirement for search.

STACKBLITZ

Lets say I need to search the first row Hydrogen Lithium, the user should be able to enter the following:

*Hy*Li and I should get the output Hydrogen Lithium

How do I achive this? I need break the * into an array and derive my search based on all the search items after splitting the *

like image 533
user2281858 Avatar asked Mar 01 '26 10:03

user2281858


1 Answers

You will need to modify the filter predicate of your datasource.

Example forked from your Stackblitz and based on your search requirements here.

Specifically:

constructor(private dialog: MatDialog) {
    this.dataSource.filterPredicate =
        (data: Element, filter: string) => {
            const searchArray = filter.split("*");
            let filterMatch = true;
            let prevIndex = 0;
            searchArray.forEach(subString => {
                const strIndex = data.name.toLowerCase().indexOf(subString.toLowerCase());
                if (strIndex === -1 || strIndex < prevIndex) {
                    filterMatch = false;
                } else {
                    prevIndex = strIndex;
                }
            });
            return filterMatch;
        };
}

(Apologies this is quite verbose - will review when I get a moment!)

like image 180
Matt Saunders Avatar answered Mar 03 '26 22:03

Matt Saunders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!