Skip to content

Commit 38cd3a8

Browse files
committed
feat: add dynamic list page size options to resource handling
1 parent 4e2adfe commit 38cd3a8

4 files changed

Lines changed: 25 additions & 13 deletions

File tree

adminforth/modules/restApi.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,9 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
12031203
),
12041204
options: {
12051205
...resource.options,
1206+
listPageSizeOptions: typeof resource.options.listPageSizeOptions === 'function'
1207+
? await resource.options.listPageSizeOptions({ adminUser, adminforth: this.adminforth })
1208+
: resource.options.listPageSizeOptions,
12061209
fieldGroups: resource.options.fieldGroups?.map((group, i) => ({
12071210
...group,
12081211
noTitle: group.noTitle ?? false,

adminforth/spa/src/components/ResourceListTable.vue

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ const props = withDefaults(defineProps<{
395395
rows: any[] | null,
396396
totalRows: number,
397397
pageSize: number,
398+
pageSizeOptions: number[],
398399
checkboxes: any[],
399400
sort: any[],
400401
noRoundings?: boolean,
@@ -430,7 +431,7 @@ const emits = defineEmits([
430431
]);
431432
432433
const pageSizeOptionsComputed = computed(() => {
433-
let options = props.resource?.options?.listPageSizeOptions;
434+
let options = props.pageSizeOptions;
434435
435436
if (!options || options.length === 0) {
436437
return [];
@@ -468,16 +469,6 @@ watch(() => pageSizeInternal.value, (newSize) => {
468469
}
469470
});
470471
471-
onMounted(() => {
472-
const savedSize = localStorage.getItem(`pageSize_${props.resource?.resourceId}`);
473-
if (savedSize) {
474-
const sizeNum = parseInt(savedSize);
475-
if (props.resource?.options?.listPageSizeOptions?.includes(sizeNum)) {
476-
pageSizeInternal.value = sizeNum;
477-
emits('update:pageSize', sizeNum);
478-
}
479-
}
480-
});
481472
482473
const checkboxesInternal: Ref<any[]> = ref([]);
483474
const pageInput = ref('1');

adminforth/spa/src/views/ListView.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
@update:records="getListInner"
174174
@update:pageSize="(newSize) => { pageSize = newSize; page = 1; }"
175175
:sort="sort"
176+
:pageSizeOptions="resolvedPageSizeOptions"
176177
:pageSize="pageSize"
177178
:totalRows="totalRows"
178179
:checkboxes="checkboxes"
@@ -237,6 +238,8 @@ import {
237238
import Filters from '@/components/Filters.vue';
238239
import { useAdminforth } from '@/adminforth';
239240
241+
const adminforth = useAdminforth();
242+
240243
const filtersShow = ref(false);
241244
const { list, alert } = useAdminforth();
242245
const coreStore = useCoreStore();
@@ -408,12 +411,28 @@ function clearAutoRefresher() {
408411
}
409412
}
410413
414+
const resolvedPageSizeOptions = ref<number[]>([]);
415+
411416
async function init() {
412417
413418
await coreStore.fetchResourceFull({
414419
resourceId: route.params.resourceId as string
415420
});
416421
422+
const optionsRaw = coreStore.resource?.options?.listPageSizeOptions;
423+
424+
if (typeof optionsRaw === 'function') {
425+
resolvedPageSizeOptions.value = await optionsRaw({
426+
adminUser: coreStore.adminUser,
427+
adminforth: adminforth
428+
});
429+
} else if (Array.isArray(optionsRaw)) {
430+
resolvedPageSizeOptions.value = optionsRaw;
431+
} else {
432+
resolvedPageSizeOptions.value = [];
433+
}
434+
435+
417436
syncPageSize();
418437
419438
isPageLoaded.value = true;

adminforth/types/Common.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,7 @@ export interface AdminForthResourceInputCommon {
478478
* or a function returning them. When set together with `listPageSize`, the page
479479
* size should be one of the values returned here.
480480
*/
481-
listPageSizeOptions?: number[];
482-
481+
listPageSizeOptions?: number[] | ((args: { adminUser: any, adminforth: any }) => number[] | Promise<number[]>);
483482
/**
484483
* Whether to use virtual scroll in list view.
485484
*/

0 commit comments

Comments
 (0)