Skip to content

Commit 26aaeab

Browse files
emanuelmutschlechnerpi0
authored andcommitted
feat(table): Add no-sort-reset prop (bootstrap-vue#1784)
1 parent fa60d56 commit 26aaeab

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

src/components/table/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,8 @@ export default {
886886
As mentioned in the [**Fields**](#fields-column-definitions-) section above,
887887
you can make columns sortable. Clicking on a sortable column header will sort the
888888
column in ascending direction (smallest first), while clicking on it again will switch the direction
889-
of sorting. Clicking on a non-sortable column will clear the sorting.
889+
of sorting. Clicking on a non-sortable column will clear the sorting. The prop `no-sort-reset`
890+
can be used to disable this feature.
890891

891892
You can control which column is pre-sorted and the order of sorting (ascending or
892893
descending). To pre-specify the column to be sorted, set the `sort-by` prop to

src/components/table/fixtures/table.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ <h2>Basic table</h2>
1313
</template>
1414
</b-table>
1515

16-
<b-table ref="table_stacked" striped hover stacked :items="items" :fields="fields">
17-
<template slot="name" slot-scope="data">
18-
{{ data.value.first }} {{ data.value.last }}
19-
</template>
20-
</b-table>
16+
<b-table ref="table_stacked" striped hover stacked :items="items" :fields="fields">
17+
<template slot="name" slot-scope="data">
18+
{{ data.value.first }} {{ data.value.last }}
19+
</template>
20+
</b-table>
2121

22-
<b-table ref="table_without_fields" :items="secondaryItems"></b-table>
22+
<b-table ref="table_without_fields" :items="secondaryItems"></b-table>
2323

2424
<b-table ref="table_responsive" responsive :items="items" :fields="fields"></b-table>
2525

26+
<b-table ref="table_no_sort_reset" :items="items" :fields="fields" no-sort-reset></b-table>
27+
2628
<h2>Paginated Table</h2>
2729
<div class="my-1 row">
2830
<div class="col-6">

src/components/table/table.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ export default {
527527
type: Boolean,
528528
default: false
529529
},
530+
noSortReset: {
531+
type: Boolean,
532+
default: false
533+
},
530534
busy: {
531535
type: Boolean,
532536
default: false
@@ -928,7 +932,7 @@ export default {
928932
this.localSortDesc = false
929933
}
930934
sortChanged = true
931-
} else if (this.localSortBy) {
935+
} else if (this.localSortBy && !this.noSortReset) {
932936
this.localSortBy = null
933937
this.localSortDesc = false
934938
sortChanged = true

src/components/table/table.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,38 @@ describe('table', async () => {
528528
}
529529
})
530530

531+
it('non-sortable header th should not emit a sort-changed event when clicked and prop no-sort-reset is set', async () => {
532+
const { app: { $refs } } = window
533+
const vm = $refs.table_no_sort_reset
534+
const spy = jest.fn()
535+
const fieldKeys = Object.keys(vm.fields)
536+
537+
vm.$on('sort-changed', spy)
538+
const thead = [...vm.$el.children].find(el => el && el.tagName === 'THEAD')
539+
expect(thead).toBeDefined()
540+
if (thead) {
541+
const tr = [...thead.children].find(el => el && el.tagName === 'TR')
542+
expect(tr).toBeDefined()
543+
if (tr) {
544+
let sortBy = null
545+
const ths = [...tr.children]
546+
expect(ths.length).toBe(fieldKeys.length)
547+
ths.forEach((th, idx) => {
548+
th.click()
549+
if (vm.fields[fieldKeys[idx]].sortable) {
550+
expect(spy).toHaveBeenCalledWith(vm.context)
551+
expect(vm.context.sortBy).toBe(fieldKeys[idx])
552+
sortBy = vm.context.sortBy
553+
} else {
554+
expect(spy).not.toHaveBeenCalled()
555+
expect(vm.context.sortBy).toBe(sortBy)
556+
}
557+
spy.mockClear()
558+
})
559+
}
560+
}
561+
})
562+
531563
it('table_paginated pagination works', async () => {
532564
const { app: { $refs } } = window
533565
const vm = $refs.table_paginated

0 commit comments

Comments
 (0)