Skip to content

Commit 94bc082

Browse files
committed
feat: improve pagination block (fixes)
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent 43a4791 commit 94bc082

5 files changed

Lines changed: 303 additions & 196 deletions

File tree

src/shared/components/ncTable/NcTable.vue

Lines changed: 171 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ deselect-all-rows -> unselect all rows, e.g. after deleting selected rows
4343
<template>
4444
<div ref="table" class="NcTable" data-cy="ncTable">
4545
<div class="options row" style="padding-right: calc(var(--default-grid-baseline) * 2);">
46-
<Options :rows="rows" :columns="parsedColumns" :element-id="elementId" :is-view="isView"
46+
<Options :rows="getSearchedAndFilteredAndSortedRows" :columns="parsedColumns" :element-id="elementId" :is-view="isView"
4747
:selected-rows="localSelectedRows" :show-options="parsedColumns.length !== 0"
4848
:view-setting.sync="localViewSetting" :config="config" @create-row="$emit('create-row')"
4949
@download-csv="data => downloadCsv(data, parsedColumns, downloadTitle)"
@@ -52,7 +52,7 @@ deselect-all-rows -> unselect all rows, e.g. after deleting selected rows
5252
</div>
5353
<div class="custom-table row">
5454
<CustomTable v-if="config.canReadRows || (config.canCreateRows && rows.length > 0)" :columns="parsedColumns"
55-
:rows="rows" :is-view="isView" :element-id="elementId" :view-setting.sync="localViewSetting"
55+
:rows="getSearchedAndFilteredAndSortedRows" :is-view="isView" :element-id="elementId" :view-setting.sync="localViewSetting"
5656
:config="config" @create-row="$emit('create-row')"
5757
@edit-row="rowId => $emit('edit-row', rowId)"
5858
@create-column="$emit('create-column')"
@@ -103,6 +103,14 @@ import { subscribe, unsubscribe } from '@nextcloud/event-bus'
103103
import { parseCol } from './mixins/columnParser.js'
104104
import { AbstractColumn } from './mixins/columnClass.js'
105105
import { translate as t } from '@nextcloud/l10n'
106+
import {
107+
TYPE_META_CREATED_AT,
108+
TYPE_META_CREATED_BY,
109+
TYPE_META_ID,
110+
TYPE_META_UPDATED_AT,
111+
TYPE_META_UPDATED_BY,
112+
} from '../../constants.ts'
113+
import { MetaColumns } from './mixins/metaColumns.js'
106114
107115
export default {
108116
name: 'NcTable',
@@ -220,6 +228,158 @@ export default {
220228
}
221229
return this.columns
222230
},
231+
232+
currentPageRows() {
233+
return this.getSearchedAndFilteredAndSortedRows.slice((this.pageNumber - 1) * this.rowsPerPage, ((this.pageNumber - 1) * this.rowsPerPage) + this.rowsPerPage)
234+
},
235+
sorting() {
236+
return this.viewSetting?.sorting
237+
},
238+
getSearchedAndFilteredRows() {
239+
const debug = false
240+
// if we don't have to search and/or filter
241+
if (!this.viewSetting?.filter?.length > 0 && !this.viewSetting?.searchString) {
242+
// cleanup markers
243+
if (this.rows && this.columns) {
244+
this.rows.forEach(row => {
245+
if (row && row.data) {
246+
this.columns.forEach(column => {
247+
const cell = row.data.find(item => item && item.columnId === column.id)
248+
if (cell === undefined) {
249+
return
250+
}
251+
delete cell.searchStringFound
252+
delete cell.filterFound
253+
})
254+
}
255+
})
256+
}
257+
return this.rows || []
258+
}
259+
260+
const data = [] // array of rows
261+
const searchString = this.viewSetting?.searchString
262+
// each row
263+
if (!this.rows || !this.columns) {
264+
return []
265+
}
266+
267+
for (const row of this.rows) {
268+
if (!row || !row.data) {
269+
continue
270+
}
271+
272+
if (debug) {
273+
console.debug('new row ===============================================', row)
274+
}
275+
let filterStatusRow = null
276+
let searchStatusRow = false
277+
278+
// each column in a row => cell
279+
this.columns.forEach(column => {
280+
if (debug) {
281+
console.debug('new column -------------------', column)
282+
}
283+
let filterStatus = null
284+
let searchStatus = true
285+
const filters = this.getFiltersForColumn(column)
286+
let cell
287+
if (column.id < 0) {
288+
cell = { columnId: column.id }
289+
switch (column.id) {
290+
case TYPE_META_ID:
291+
cell.value = row.id
292+
break
293+
case TYPE_META_CREATED_BY:
294+
cell.value = row.createdBy
295+
break
296+
case TYPE_META_UPDATED_BY:
297+
cell.value = row.editedBy
298+
break
299+
case TYPE_META_CREATED_AT:
300+
cell.value = row.createdAt
301+
break
302+
case TYPE_META_UPDATED_AT:
303+
cell.value = row.editedAt
304+
break
305+
}
306+
} else {
307+
cell = row.data.find(item => item && item.columnId === column.id)
308+
}
309+
310+
// if we don't have a value for this cell
311+
if (cell === undefined) {
312+
if (searchString) {
313+
searchStatus = false
314+
}
315+
cell = { columnId: column.id, value: null }
316+
}
317+
// cleanup possible old markers
318+
delete cell.searchStringFound
319+
delete cell.filterFound
320+
321+
// if we should filter
322+
if (filters !== null) {
323+
filters.forEach(fil => {
324+
this.addMagicFieldsValues(fil)
325+
if (filterStatus === null || filterStatus === true) {
326+
filterStatus = column.isFilterFound(cell, fil)
327+
}
328+
})
329+
}
330+
// if we should search
331+
if (searchString) {
332+
console.debug('look for searchString', searchString)
333+
searchStatus = column.isSearchStringFound(cell, searchString.toLowerCase())
334+
}
335+
336+
if (debug) {
337+
console.debug('filterStatus for cell', { cell: cell?.value, filterStatusCell: filterStatus, filterStatusRowBefore: filterStatusRow })
338+
}
339+
340+
// if filterStatus is null, this result should be ignored
341+
if (filterStatus !== null && (filterStatusRow || filterStatusRow === null)) {
342+
filterStatusRow = filterStatus
343+
}
344+
345+
if (debug) {
346+
console.debug('new filterStatusRow', filterStatusRow)
347+
}
348+
349+
// filterStatusRow = filterStatus
350+
searchStatusRow = searchStatusRow || searchStatus
351+
})
352+
353+
if (debug) {
354+
console.debug('if push row', { filterStatusRow, searchStatusRow, result: (filterStatusRow || filterStatusRow === null) && searchStatusRow })
355+
}
356+
if ((filterStatusRow || filterStatusRow === null) && searchStatusRow) {
357+
data.push({ ...row })
358+
}
359+
}
360+
return data
361+
},
362+
getSearchedAndFilteredAndSortedRows() {
363+
const allColumns = this.columns.concat(MetaColumns)
364+
const sort = (sortCols) => {
365+
const sortColumn = allColumns.find(item => item.id === sortCols?.[0].columnId)
366+
const nextSorts = []
367+
for (let i = 1; i < sortCols.length; i++) {
368+
const sortColumn = allColumns.find(item => item.id === sortCols[i].columnId)
369+
nextSorts.push(sortColumn?.sort?.(sortCols[i].mode))
370+
}
371+
return [...this.getSearchedAndFilteredRows].sort(sortColumn?.sort?.(sortCols[0].mode, nextSorts))
372+
}
373+
374+
// if we have to sort
375+
if (this.viewSetting?.presetSorting) {
376+
return sort(this.viewSetting.presetSorting)
377+
}
378+
if (this.viewSetting?.sorting) {
379+
return sort(this.viewSetting.sorting)
380+
}
381+
return this.getSearchedAndFilteredRows
382+
},
223383
},
224384
watch: {
225385
localSelectedRows() {
@@ -257,6 +417,15 @@ export default {
257417
this.localSelectedRows = []
258418
}
259419
},
420+
getFiltersForColumn(column) {
421+
if (this.viewSetting?.filter?.length > 0) {
422+
const columnFilter = this.viewSetting.filter.filter(item => item.columnId === column.id)
423+
if (columnFilter.length > 0) {
424+
return columnFilter
425+
}
426+
}
427+
return null
428+
},
260429
setSearchString(str) {
261430
this.localViewSetting.searchString = str !== '' ? str : null
262431
this.localViewSetting = JSON.parse(JSON.stringify(this.localViewSetting))

0 commit comments

Comments
 (0)