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

feat(b-table): allow field definition properties filterByFormatted and sortByFormatted accept a formatter function reference (closes #3892) #3898

Merged
merged 9 commits into from Aug 16, 2019

Conversation

tmorehouse
Copy link
Member

@tmorehouse tmorehouse commented Aug 16, 2019

Describe the PR

Allow user to pass a function to field definition properties filterByFormatted and sortByFormatted

Properties can now be a Boolean (existing functionality) to use field's formatter or not, or a function ref that will format specifically for filtering or sorting.

Closes #3892

PR checklist

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Enhancement
  • ARIA accessibility
  • Documentation update
  • Other (please describe)

Does this PR introduce a breaking change? (check one)

  • No
  • Yes (please describe)

The PR fulfills these requirements:

  • It's submitted to the dev branch, not the master branch
  • When resolving a specific issue, it's referenced in the PR's title (i.e. [...] (fixes #xxx[,#xxx]), where "xxx" is the issue number)
  • It should address only one issue or feature. If adding multiple features or fixing a bug and adding a new feature, break them into separate PRs if at all possible.
  • The title should follow the Conventional Commits naming convention (i.e. fix(alert): not alerting during SSR render, docs(badge): update pill examples, fix typos, chore: fix typo in README, etc). This is very important, as the CHANGELOG is generated from these messages.

If new features/enhancement/fixes are added or changed:

  • Includes documentation updates (including updating the component's package.json for slot and event changes)
  • Includes any needed TypeScript declaration file updates
  • New/updated tests are included and passing (if required)
  • Existing test suites are passing
  • The changes have not impacted the functionality of other components or directives
  • ARIA Accessibility has been taken into consideration (Does it affect screen reader users or keyboard only users? Clickable items should be in the tab index, etc.)

If adding a new feature, or changing the functionality of an existing feature, the PR's
description above includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

…rmatted` accept a formatter function reference
@tmorehouse tmorehouse added this to In Progress in 2.0.0 Stable via automation Aug 16, 2019
@codecov
Copy link

@codecov codecov bot commented Aug 16, 2019

Codecov Report

Merging #3898 into dev will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev    #3898      +/-   ##
==========================================
+ Coverage   99.33%   99.33%   +<.01%     
==========================================
  Files         233      233              
  Lines        4484     4487       +3     
  Branches     1266     1269       +3     
==========================================
+ Hits         4454     4457       +3     
  Misses         24       24              
  Partials        6        6
Impacted Files Coverage Δ
src/components/table/helpers/sanitize-row.js 100% <100%> (ø) ⬆️
src/components/table/helpers/mixin-sorting.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 03536a5...5a2f251. Read the comment docs.

@tmorehouse tmorehouse marked this pull request as ready for review Aug 16, 2019
@tmorehouse tmorehouse requested a review from jacobmllr95 Aug 16, 2019
@tmorehouse tmorehouse merged commit 5492b38 into dev Aug 16, 2019
8 of 9 checks passed
2.0.0 Stable automation moved this from In Progress to Completed Aug 16, 2019
@tmorehouse tmorehouse deleted the tmorehouse/table-filter-sort branch Aug 16, 2019
@mschlitz-trux
Copy link

@mschlitz-trux mschlitz-trux commented Nov 3, 2019

I've been unsuccessful in using this filterByFormatted property as a function on a field. If I use a function and add a console.log or debugger, it just never fires.

@tmorehouse
Copy link
Member Author

@tmorehouse tmorehouse commented Nov 4, 2019

@mschlitz-trux make sure you are using the latest version of BootstrapVue

@mschlitz-trux
Copy link

@mschlitz-trux mschlitz-trux commented Nov 4, 2019

@tmorehouse Yes, currently using "bootstrap-vue": "^2.0.4",

Edit: here's my current contents snippets:

                        <template v-slot:cell(name)="row">
                            <div class="user-name ellipsis-fit">{{ row.value }}</div>
                        </template>
            userFields: [
...
                {
                    key: 'name',
                    label: 'Name',
                    formatter: 'formatUserFullName',
                    sortable: true,
                    sortByFormatted: true,
                    filterByFormatted: true,
                    tdClass: 'col-primary',
                },
...
           ]

formatUserFullName works fine as expected. But if I change filterByFormatted into a function, it simply won't trigger. I tried an inline anon function, and a function reference.

@tmorehouse
Copy link
Member Author

@tmorehouse tmorehouse commented Nov 4, 2019

@mschlitz-trux is your method trying to access anything in the this context?

@tmorehouse
Copy link
Member Author

@tmorehouse tmorehouse commented Nov 4, 2019

@mschlitz-trux Could you provide a simple sample of the formatter function you are trying to use in filterByFormatted?

@mschlitz-trux
Copy link

@mschlitz-trux mschlitz-trux commented Nov 4, 2019

@tmorehouse here's a fiddle:
https://jsfiddle.net/fex4sdvm/

Note that I tried both filterByFormatted: this.filterUserId, as well as filterByFormatted: 'filterUserId',. Am I doing something wrong?

@tmorehouse
Copy link
Member Author

@tmorehouse tmorehouse commented Nov 4, 2019

this context is not available in data if data is not a function.

filterByFormatted only accepts a boolean (true/false) or a reference to a function (not a function name).

See this fiddle: https://jsfiddle.net/r17nujvo/

@mschlitz-trux
Copy link

@mschlitz-trux mschlitz-trux commented Nov 4, 2019

Thank you @tmorehouse I think I'm good now. My jsfiddle just didn't have the proper data format:

  data() {
    return {

And I just wasn't referencing this.functionName directly. I think my original comment I was using it in "string" format, just because that's how the formatter was. I know the docs specifically mentions by reference but I think it was easier to miss because of how formatter worked.

Thanks for your help! Sorry to muck up this old merged PR. A working in example in the Wiki may help a future user!

@mschlitz-trux
Copy link

@mschlitz-trux mschlitz-trux commented Nov 20, 2019

So, just to bump this again, and point out a thing that stumped me at first.

Let's say you have an array of objects that looks like this:

[
{ id: 0, firstName: 'Jane', lastName: 'Doe' },
{ id: 1, firstName: 'John', lastName: 'Doe' },
]

If you try to setup a field with key: 'name', then your custom formatter will work, it just won't have a "value" defined in the first paramtere.
However, it will never trigger your filterByFormatted. The function won't be called.

It seems as-if this relies on matching with a key name on the object itself. If you change to using key: 'firstName' and then adjust your code references as needed, it seems to work okay. However, this seems like a workaround.

On the plus side, once I made this change, I can just use filterByFormatted: true since it will pick up on the correct formatted value from formatter: 'formatUserFullName'. But if I needed any other further customization, I still need to ensure I use this workaround to point at a valid key name.

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
2.0.0 Stable
  
Completed
Development

Successfully merging this pull request may close these issues.

3 participants