Skip to content

Commit 9b8058f

Browse files
committed
🐛 update filter logic
Allow multiple filters and correctly update visible values. The logic was updated to: first loop through filters that are applicable on whole file, then make data copy and loop through the copy. Also instead of adding new values to empty array, filter methods now deletes rows that does not met given criteria.
1 parent f857b3c commit 9b8058f

File tree

1 file changed

+71
-17
lines changed

1 file changed

+71
-17
lines changed

src/views/Document.vue

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ export default {
169169
170170
computed: {
171171
tableContent () {
172-
if (this.copy.content && this.copy.content.length > 0) {
173-
return this.copy.content
172+
if (this.copy.content === null) {
173+
return this.file.content
174174
}
175-
return this.file.content
175+
return this.copy.content
176176
},
177177
tableColumns () {
178178
if (this.copy.content && this.copy.content.length > 0) {
@@ -204,7 +204,13 @@ export default {
204204
return columns
205205
},
206206
totalNumberOfPages () {
207-
return Math.ceil(this.tableContent.length / this.page.limit)
207+
const numberOfPages = Math.ceil(this.tableContent.length / this.page.limit)
208+
209+
if (numberOfPages === 0) {
210+
return 1
211+
}
212+
213+
return numberOfPages
208214
},
209215
isOnLastPage () {
210216
return this.page.currentPage === this.totalNumberOfPages
@@ -246,29 +252,77 @@ export default {
246252
}
247253
},
248254
filterData () {
249-
this.setCopyContent()
255+
if (Object.keys(this.filters).length === 0) {
256+
this.copy.content = null
257+
this.saveFile()
258+
return
259+
}
260+
261+
let data = this.file.content.map((row) => {
262+
return {...row}
263+
})
264+
265+
const copyFilters = []
266+
267+
for (const rowIndex in data) {
268+
const row = data[rowIndex]
269+
const passedFilters = []
270+
for (const filterKey in this.filters) {
271+
if (this.filters[filterKey].type === 'file') {
272+
const columnToString = row[filterKey]?.toString().toUpperCase()
273+
if (columnToString && this.filters[filterKey].values.some(item => this.checkFilterValues(item, columnToString))) {
274+
passedFilters.push(true)
275+
} else {
276+
passedFilters.push(false)
277+
}
278+
} else {
279+
if (copyFilters.indexOf(filterKey) === -1) {
280+
copyFilters.push(filterKey)
281+
}
282+
}
283+
}
250284
251-
for (const key in this.filters) {
252-
this.copy.content = this[this.filters[key].type].content.filter((row) => {
253-
const rowKeyToString = row[key]?.toString().toUpperCase().split(' ')
254-
if (rowKeyToString && this.filters[key].values.some((item) => {
255-
if (item.includes('"')) {
256-
return rowKeyToString.includes(item.trim().replaceAll('"', '').toUpperCase())
285+
if (!passedFilters.includes(true)) {
286+
delete data[rowIndex]
287+
}
288+
}
289+
290+
data.filter(Boolean)
291+
292+
if (copyFilters.length > 0) {
293+
for (const rowIndex in data) {
294+
const row = data[rowIndex]
295+
let copy = true
296+
for (const filter of copyFilters) {
297+
const columnToString = row[filter]?.toString().toUpperCase()
298+
if (copy && columnToString && this.filters[filter].values.some(item => this.checkFilterValues(item, columnToString))) {
299+
// continue
257300
} else {
258-
return rowKeyToString.find(a => a.includes(item.trim().toUpperCase()))
301+
copy = false
259302
}
260-
})) {
261-
return row
262303
}
263-
return false
264-
})
304+
305+
if (!copy) {
306+
delete data[rowIndex]
307+
}
308+
}
265309
}
266310
311+
this.copy.content = data.filter(Boolean)
312+
267313
this.saveFile()
268314
},
315+
checkFilterValues (item, columnValue) {
316+
const query = columnValue
317+
if (item.includes('"')) {
318+
return query.includes(item.trim().replaceAll('"', '').toUpperCase())
319+
} else {
320+
return query.split(' ').find(a => a.includes(item.trim().toUpperCase()))
321+
}
322+
},
269323
removeFilter (key) {
270324
delete this.filters[key]
271-
this.copy.content = this.file.content
325+
this.copy.content = null
272326
this.filterData()
273327
},
274328
loadFileContent () {

0 commit comments

Comments
 (0)