From 29cc42e2e8e0b64269b853da41c88c2258cf256a Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 23 Mar 2024 11:29:57 +0200 Subject: [PATCH] pagination offset fix --- client/res/templates/record/list.tpl | 36 ++++++++++------------ client/src/collection.js | 23 ++++++++------ client/src/views/record/list-pagination.js | 4 +++ client/src/views/record/list.js | 31 ++++++++++--------- 4 files changed, 50 insertions(+), 44 deletions(-) diff --git a/client/res/templates/record/list.tpl b/client/res/templates/record/list.tpl index 41f34f741c..bd6866abbf 100644 --- a/client/res/templates/record/list.tpl +++ b/client/res/templates/record/list.tpl @@ -208,25 +208,23 @@ - {{#unless hasPagination}} - {{#if showMoreEnabled}} -
- - {{#if showCount}} -
{{moreCountFormatted}}
- {{/if}} - {{translate 'Show more'}} -
-
- {{/if}} - {{/unless}} + {{#if showMoreEnabled}} +
+ + {{#if showCount}} +
{{moreCountFormatted}}
+ {{/if}} + {{translate 'Show more'}} +
+
+ {{/if}} {{else}} diff --git a/client/src/collection.js b/client/src/collection.js index ec8e8425eb..5a27f6db08 100644 --- a/client/src/collection.js +++ b/client/src/collection.js @@ -657,7 +657,9 @@ class Collection { * @returns {Promise} */ nextPage() { - return this.setOffset(this.offset + this.maxSize); + const offset = this.offset + Math.max(this.maxSize, this.length); + + return this.setOffset(offset); } /** @@ -710,7 +712,7 @@ class Collection { this.offset = offset; - return this.fetch(); + return this.fetch({maxSize: this.maxSize}); } /** @@ -751,6 +753,10 @@ class Collection { * @param {{ * remove?: boolean, * more?: boolean, + * offset?: number, + * maxSize?: number, + * orderBy?: string, + * order?: 'asc'|'desc', * } & Object.} [options] Options. * @returns {Promise} * @fires Collection#sync Unless `{silent: true}`. @@ -767,20 +773,17 @@ class Collection { const length = this.length + this.lengthCorrection; - if (!('maxSize' in options)) { - options.data.maxSize = options.more ? this.maxSize : ( - (length > this.maxSize) ? length : this.maxSize - ); + if ('maxSize' in options) { + options.data.maxSize = options.maxSize; + } else { + options.data.maxSize = options.more ? this.maxSize : Math.max(length, this.maxSize); if (this.maxMaxSize && options.data.maxSize > this.maxMaxSize) { options.data.maxSize = this.maxMaxSize; } } - else { - options.data.maxSize = options.maxSize; - } - options.data.offset = options.more ? length : this.offset; + options.data.offset = options.more ? (this.offset + length) : this.offset; options.data.orderBy = this.orderBy; options.data.order = this.order; options.data.where = this.getWhere(); diff --git a/client/src/views/record/list-pagination.js b/client/src/views/record/list-pagination.js index a8b706fb9f..efc73b192f 100644 --- a/client/src/views/record/list-pagination.js +++ b/client/src/views/record/list-pagination.js @@ -57,6 +57,10 @@ class RecordListPagination extends View { noTotal: noTotal, }; } + + setup() { + this.listenTo(this.collection, 'sync', () => this.reRender()); + } } export default RecordListPagination; diff --git a/client/src/views/record/list.js b/client/src/views/record/list.js index c2f403b2cc..18982ff40d 100644 --- a/client/src/views/record/list.js +++ b/client/src/views/record/list.js @@ -706,24 +706,25 @@ class ListRecordView extends View { if (page === 'current') { this.collection.fetch().then(() => onSync()); - } - else { - if (page === 'next') { - this.collection.nextPage().then(() => onSync()); - } - else if (page === 'previous') { - this.collection.previousPage().then(() => onSync()); - } - else if (page === 'last') { - this.collection.lastPage().then(() => onSync()); - } - else if (page === 'first') { - this.collection.firstPage().then(() => onSync()); - } + this.deactivate(); - this.trigger('paginate'); + return; } + if (page === 'next') { + this.collection.nextPage().then(() => onSync()); + } + else if (page === 'previous') { + this.collection.previousPage().then(() => onSync()); + } + else if (page === 'last') { + this.collection.lastPage().then(() => onSync()); + } + else if (page === 'first') { + this.collection.firstPage().then(() => onSync()); + } + + this.trigger('paginate'); this.deactivate(); }