Skip to content

Commit bb1c550

Browse files
gaafmosinve
authored andcommitted
feat(table): Split computedItems into multiple functions (#1893)
* 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 2970509 commit bb1c550

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/components/table/table.js

Lines changed: 47 additions & 40 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,30 +787,49 @@ export default {
797787
// Array copy for sorting, filtering, etc.
798788
items = items.slice()
799789
// Apply local filter
800-
if (filter && localFiltering) {
801-
if (filter instanceof Function) {
802-
items = items.filter(filter)
790+
this.filteredItems = 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+
if (this.localFiltering && this.filter) {
807+
if (this.filter instanceof Function) {
808+
return items.filter(this.filter)
809+
}
810+
811+
let regex
812+
if (this.filter instanceof RegExp) {
813+
regex = this.filter
803814
} else {
804-
let regex
805-
if (filter instanceof RegExp) {
806-
regex = filter
807-
} else {
808-
regex = new RegExp('.*' + filter + '.*', 'ig')
809-
}
810-
items = items.filter(item => {
811-
const test = regex.test(recToString(item))
812-
regex.lastIndex = 0
813-
return test
814-
})
815+
regex = new RegExp('.*' + this.filter + '.*', 'ig')
815816
}
817+
818+
return items.filter(item => {
819+
const test = regex.test(recToString(item))
820+
regex.lastIndex = 0
821+
return test
822+
})
816823
}
817-
if (localFiltering) {
818-
// Make a local copy of filtered items to trigger filtered event
819-
this.filteredItems = items.slice()
820-
}
821-
// Apply local Sort
824+
return items
825+
},
826+
sortItems (items) {
827+
const sortBy = this.localSortBy
828+
const sortDesc = this.localSortDesc
829+
const sortCompare = this.sortCompare
830+
const localSorting = this.localSorting
822831
if (sortBy && localSorting) {
823-
items = stableSort(items, (a, b) => {
832+
return stableSort(items, (a, b) => {
824833
let ret = null
825834
if (typeof sortCompare === 'function') {
826835
// Call user provided sortCompare routine
@@ -834,21 +843,19 @@ export default {
834843
return (ret || 0) * (sortDesc ? -1 : 1)
835844
})
836845
}
846+
return items
847+
},
848+
paginateItems (items) {
849+
const currentPage = this.currentPage
850+
const perPage = this.perPage
851+
const localPaging = this.localPaging
837852
// Apply local pagination
838-
if (Boolean(perPage) && localPaging) {
853+
if (!!perPage && localPaging) {
839854
// Grab the current page of data (which may be past filtered items)
840-
items = items.slice((currentPage - 1) * perPage, currentPage * perPage)
855+
return items.slice((currentPage - 1) * perPage, currentPage * perPage)
841856
}
842-
// Update the value model with the filtered/sorted/paginated data set
843-
this.$emit('input', items)
844857
return items
845858
},
846-
computedBusy () {
847-
return this.busy || this.localBusy
848-
}
849-
},
850-
methods: {
851-
keys,
852859
fieldClasses (field) {
853860
return [
854861
field.sortable ? 'sorting' : '',

0 commit comments

Comments
 (0)