Skip to content

Commit

Permalink
feat(table): Add no-sort-reset prop (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelmutschlechner authored and pi0 committed Apr 29, 2018
1 parent fa60d56 commit 26aaeab
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/components/table/README.md
Expand Up @@ -886,7 +886,8 @@ export default {
As mentioned in the [**Fields**](#fields-column-definitions-) section above, As mentioned in the [**Fields**](#fields-column-definitions-) section above,
you can make columns sortable. Clicking on a sortable column header will sort the you can make columns sortable. Clicking on a sortable column header will sort the
column in ascending direction (smallest first), while clicking on it again will switch the direction column in ascending direction (smallest first), while clicking on it again will switch the direction
of sorting. Clicking on a non-sortable column will clear the sorting. of sorting. Clicking on a non-sortable column will clear the sorting. The prop `no-sort-reset`
can be used to disable this feature.


You can control which column is pre-sorted and the order of sorting (ascending or You can control which column is pre-sorted and the order of sorting (ascending or
descending). To pre-specify the column to be sorted, set the `sort-by` prop to descending). To pre-specify the column to be sorted, set the `sort-by` prop to
Expand Down
14 changes: 8 additions & 6 deletions src/components/table/fixtures/table.html
Expand Up @@ -13,16 +13,18 @@ <h2>Basic table</h2>
</template> </template>
</b-table> </b-table>


<b-table ref="table_stacked" striped hover stacked :items="items" :fields="fields"> <b-table ref="table_stacked" striped hover stacked :items="items" :fields="fields">
<template slot="name" slot-scope="data"> <template slot="name" slot-scope="data">
{{ data.value.first }} {{ data.value.last }} {{ data.value.first }} {{ data.value.last }}
</template> </template>
</b-table> </b-table>


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


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


<b-table ref="table_no_sort_reset" :items="items" :fields="fields" no-sort-reset></b-table>

<h2>Paginated Table</h2> <h2>Paginated Table</h2>
<div class="my-1 row"> <div class="my-1 row">
<div class="col-6"> <div class="col-6">
Expand Down
6 changes: 5 additions & 1 deletion src/components/table/table.js
Expand Up @@ -527,6 +527,10 @@ export default {
type: Boolean, type: Boolean,
default: false default: false
}, },
noSortReset: {
type: Boolean,
default: false
},
busy: { busy: {
type: Boolean, type: Boolean,
default: false default: false
Expand Down Expand Up @@ -928,7 +932,7 @@ export default {
this.localSortDesc = false this.localSortDesc = false
} }
sortChanged = true sortChanged = true
} else if (this.localSortBy) { } else if (this.localSortBy && !this.noSortReset) {
this.localSortBy = null this.localSortBy = null
this.localSortDesc = false this.localSortDesc = false
sortChanged = true sortChanged = true
Expand Down
32 changes: 32 additions & 0 deletions src/components/table/table.spec.js
Expand Up @@ -528,6 +528,38 @@ describe('table', async () => {
} }
}) })


it('non-sortable header th should not emit a sort-changed event when clicked and prop no-sort-reset is set', async () => {
const { app: { $refs } } = window
const vm = $refs.table_no_sort_reset
const spy = jest.fn()
const fieldKeys = Object.keys(vm.fields)

vm.$on('sort-changed', spy)
const thead = [...vm.$el.children].find(el => el && el.tagName === 'THEAD')
expect(thead).toBeDefined()
if (thead) {
const tr = [...thead.children].find(el => el && el.tagName === 'TR')
expect(tr).toBeDefined()
if (tr) {
let sortBy = null
const ths = [...tr.children]
expect(ths.length).toBe(fieldKeys.length)
ths.forEach((th, idx) => {
th.click()
if (vm.fields[fieldKeys[idx]].sortable) {
expect(spy).toHaveBeenCalledWith(vm.context)
expect(vm.context.sortBy).toBe(fieldKeys[idx])
sortBy = vm.context.sortBy
} else {
expect(spy).not.toHaveBeenCalled()
expect(vm.context.sortBy).toBe(sortBy)
}
spy.mockClear()
})
}
}
})

it('table_paginated pagination works', async () => { it('table_paginated pagination works', async () => {
const { app: { $refs } } = window const { app: { $refs } } = window
const vm = $refs.table_paginated const vm = $refs.table_paginated
Expand Down

1 comment on commit 26aaeab

@tmorehouse
Copy link
Member

Choose a reason for hiding this comment

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

This might have been better implemented by making the sortable column headers tri-state click... i.e. cycle from asc (false) => desc (true) => no sort (null) => asc (false) => desc (true) => etc,, and removing the click handlers on the non-sortable headers. This would make for a better user interface design.

Please sign in to comment.