Skip to content

Commit 05c0915

Browse files
committed
feat(table): Split computedItems into multiple functions
Don't do all the processing in one function so external users (parent) can grab the items at various stages without having to reproduce all the filtering/sorting/pagination logic. For example a parent may want to have access to the filtered and sorted, but not yet paginated set of items. With the split functions it can now do something like this: const table = this.$refs.the-table let items = table.hasProvider ? table.localItems : table.items items = table.sortItems(table.filterItems(items)) This patch incorporates no functional changes.
1 parent 57af446 commit 05c0915

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/components/table/table.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -779,16 +779,6 @@ export default {
779779
})
780780
},
781781
computedItems () {
782-
// Grab some props/data to ensure reactivity
783-
const perPage = this.perPage
784-
const currentPage = this.currentPage
785-
const filter = this.filter
786-
const sortBy = this.localSortBy
787-
const sortDesc = this.localSortDesc
788-
const sortCompare = this.sortCompare
789-
const localFiltering = this.localFiltering
790-
const localSorting = this.localSorting
791-
const localPaging = this.localPaging
792782
let items = this.hasProvider ? this.localItems : this.items
793783
if (!items) {
794784
this.$nextTick(this._providerUpdate)
@@ -797,6 +787,24 @@ export default {
797787
// Array copy for sorting, filtering, etc.
798788
items = items.slice()
799789
// Apply local filter
790+
items = this.filterItems(items)
791+
// Apply local sort
792+
items = this.sortItems(items)
793+
// Apply local pagination
794+
items = this.paginateItems(items)
795+
// Update the value model with the filtered/sorted/paginated data set
796+
this.$emit('input', items)
797+
return items
798+
},
799+
computedBusy () {
800+
return this.busy || this.localBusy
801+
}
802+
},
803+
methods: {
804+
keys,
805+
filterItems (items) {
806+
const filter = this.filter
807+
const localFiltering = this.localFiltering
800808
if (filter && localFiltering) {
801809
if (filter instanceof Function) {
802810
items = items.filter(filter)
@@ -818,7 +826,13 @@ export default {
818826
// Make a local copy of filtered items to trigger filtered event
819827
this.filteredItems = items.slice()
820828
}
821-
// Apply local Sort
829+
return items
830+
},
831+
sortItems (items) {
832+
const sortBy = this.localSortBy
833+
const sortDesc = this.localSortDesc
834+
const sortCompare = this.sortCompare
835+
const localSorting = this.localSorting
822836
if (sortBy && localSorting) {
823837
items = stableSort(items, (a, b) => {
824838
let ret = null
@@ -834,21 +848,19 @@ export default {
834848
return (ret || 0) * (sortDesc ? -1 : 1)
835849
})
836850
}
851+
return items
852+
},
853+
paginateItems (items) {
854+
const currentPage = this.currentPage
855+
const perPage = this.perPage
856+
const localPaging = this.localPaging
837857
// Apply local pagination
838858
if (Boolean(perPage) && localPaging) {
839859
// Grab the current page of data (which may be past filtered items)
840860
items = items.slice((currentPage - 1) * perPage, currentPage * perPage)
841861
}
842-
// Update the value model with the filtered/sorted/paginated data set
843-
this.$emit('input', items)
844862
return items
845863
},
846-
computedBusy () {
847-
return this.busy || this.localBusy
848-
}
849-
},
850-
methods: {
851-
keys,
852864
fieldClasses (field) {
853865
return [
854866
field.sortable ? 'sorting' : '',

0 commit comments

Comments
 (0)