Skip to content

Commit f847dae

Browse files
feat(b-table): add sortKey option for no-local-sorting events (#5746)
* feat(b-table): add sortKey option * Add Test * Update Documentation * Update table-sorting.spec.js * Update mixin-sorting.js * Update README.md * Update README.md Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
1 parent e94cc16 commit f847dae

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/components/table/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ The following field properties are recognized:
242242
| `class` | String or Array | Class name (or array of class names) to add to `<th>` **and** `<td>` in the column. |
243243
| `formatter` | String or Function | A formatter callback function or name of a method in your component, can be used instead of (or in conjunction with) scoped field slots. The formatter will be called with the syntax `formatter(value, key, item)`. Refer to [Custom Data Rendering](#custom-data-rendering) for more details. |
244244
| `sortable` | Boolean | Enable sorting on this column. Refer to the [Sorting](#sorting) Section for more details. |
245+
| `sortKey` | String | <span class="badge badge-secondary">v2.17.0+</span> Set the value of `sortBy` for the column in the emitted context when `no-local-sorting` is `true`. |
245246
| `sortDirection` | String | Set the initial sort direction on this column when it becomes sorted. Refer to the [Change initial sort direction](#change-initial-sort-direction) Section for more details. |
246247
| `sortByFormatted` | Boolean or Function | Sort the column by the result of the field's `formatter` callback function when set to `true`. Default is `false`. Boolean has no effect if the field does not have a `formatter`. Optionally accepts a formatter function _reference_ to format the value for sorting purposes only. Refer to the [Sorting](#sorting) Section for more details. |
247248
| `filterByFormatted` | Boolean or Function | Filter the column by the result of the field's `formatter` callback function when set to `true`. Default is `false`. Boolean has no effect if the field does not have a `formatter`. Optionally accepts a formatter function _reference_ to format the value for filtering purposes only. Refer to the [Filtering](#filtering) section for more details. |
@@ -2048,7 +2049,7 @@ function toString(value) {
20482049
### Disable local sorting
20492050

20502051
If you want to handle sorting entirely in your app, you can disable the local sorting in `<b-table>`
2051-
by setting the prop `no-local-sorting` to true, while still maintaining the sortable header
2052+
by setting the prop `no-local-sorting` to `true`, while still maintaining the sortable header
20522053
functionality (via `sort-changed` or `context-changed` events as well as syncable props).
20532054

20542055
You can use the syncable props `sort-by.sync` and `sort-desc.sync` to detect changes in sorting
@@ -2059,7 +2060,7 @@ with a single argument containing the context object of `<b-table>`. See the
20592060
[Detection of sorting change](#detection-of-sorting-change) section below for details about the
20602061
sort-changed event and the context object.
20612062

2062-
When `no-local-sorting` is true, the `sort-compare` prop has no effect.
2063+
When `no-local-sorting` is `true`, the `sort-compare` prop has no effect.
20632064

20642065
### Change initial sort direction
20652066

src/components/table/helpers/mixin-sorting.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,13 @@ export default {
209209
}
210210
}
211211
if (field.sortable) {
212-
if (key === this.localSortBy) {
212+
const sortKey = !this.localSorting && field.sortKey ? field.sortKey : key
213+
if (this.localSortBy === sortKey) {
213214
// Change sorting direction on current column
214215
this.localSortDesc = !this.localSortDesc
215216
} else {
216217
// Start sorting this column ascending
217-
this.localSortBy = key
218+
this.localSortBy = sortKey
218219
// this.localSortDesc = false
219220
toggleLocalSortDesc()
220221
}

src/components/table/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export declare class BTable extends BvComponent {
2424
fields?: BvTableFieldArray
2525
primaryKey?: string
2626
sortBy?: string | null
27+
sortKey?: string
2728
sortDesc?: boolean
2829
sortDirection?: BvTableSortDirection
2930
sortCompare?: BvTableSortCompareCallback

src/components/table/table-sorting.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ describe('table > sorting', () => {
4141
wrapper.destroy()
4242
})
4343

44+
it('should emit `field.sortKey` if specified and no local sorting', async () => {
45+
const wrapper = mount(BTable, {
46+
propsData: {
47+
fields: [...testFields, { key: 'd', label: 'D', sortable: true, sortKey: 'non-local' }],
48+
items: testItems,
49+
noLocalSorting: true
50+
}
51+
})
52+
53+
expect(wrapper).toBeDefined()
54+
55+
await wrapper
56+
.findAll('thead > tr > th')
57+
.at(3)
58+
.trigger('keydown.enter')
59+
expect(wrapper.emitted('sort-changed').length).toBe(1)
60+
expect(wrapper.emitted('sort-changed')[0][0].sortBy).toEqual('non-local')
61+
})
62+
4463
it('should sort column descending when sortBy set and sortDesc changed, with proper attributes', async () => {
4564
const wrapper = mount(BTable, {
4665
propsData: {

0 commit comments

Comments
 (0)