Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): broaden abstraction for filtering #8059

Merged

Conversation

andrewseguin
Copy link
Contributor

Currently the user can customize the MatTableDataSource's filtering by providing a custom function of how to convert a data object into a filter string.

This may be too in-depth for most users and not as intuitive.

This PR broadens the abstraction so that users instead will define a function that accepts data and a filter string, and returns true/false depending on if the data matches.

This also allows the data source some freedom in defaulting to a behavior where it trims/lowercases the filter but allows users to override that behavior easily.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 26, 2017
*/
filterTermAccessor: ((data: T) => string) = (data: T): string => {
filterMatcher: ((data: T, filter: string) => boolean) = (data: T, filter: string): boolean => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filterPredicate?

const accumulator = (currentTerm, key) => currentTerm + data[key];
return Object.keys(data).reduce(accumulator, '').toLowerCase();
const dataStr = Object.keys(data).reduce(accumulator, '').toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to filter on the keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it, but its actually iterating over each of the object's keys and then reducing it via the accumulator function. That function takes each key and accesses the data's property and appends it to the accumulated value.

In the end an object like {name: 'John', color: 'Blue', age: '27'} ends up looking like 'JohnBlue27'

Would be nice to use Object.values() but its not supported by IE

@andrewseguin
Copy link
Contributor Author

Renamed the filter function to filterPredicate

@jelbourn jelbourn added the P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful label Oct 27, 2017
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Oct 27, 2017
* Checks if a data object matches the data source's filter string. By default, each data object
* is converted to a string of its properties and returns true if the filter has
* at least one occurrence in that string. By default, the filter string has its whitespace
* removed and the match is case-insensitive. May be overriden for a custom implementation of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"...has its whitespace trimmed and the match..."?

To me, "removed" kind of implies that { name: "Mr. Smith" } would end up as mr.smith

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I can see how that could be taken both ways

@mmalerba mmalerba merged commit d47b37a into angular:master Oct 30, 2017
@andrewseguin andrewseguin deleted the table-smarter-data-source-filter branch November 28, 2017 20:42
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants