Skip to content

feat : added dynamic page size #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/pluggableWidgets/datagrid-web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]


### Added

- Added option for users to dynamically change the page size.

## [2.18.0] - 2024-04-30

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ export function getProperties(
if (values.showEmptyPlaceholder === "none") {
hidePropertyIn(defaultProperties, values, "emptyPlaceholder");
}

if (values.pageSizeType === "dynamic") {
hidePropertyIn(defaultProperties, values, "pageSize");
}
if (values.pageSizeType === "static") {
hidePropertyIn(defaultProperties, values, "dynamicPageSize");
}
hideSelectionProperties(defaultProperties, values);

changePropertyIn(
Expand Down
32 changes: 21 additions & 11 deletions packages/pluggableWidgets/datagrid-web/src/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { DatagridContainerProps } from "../typings/DatagridProps";
import { Cell } from "./components/Cell";
import { Widget } from "./components/Widget";
import { isAvailable } from "@mendix/widget-plugin-platform/framework/is-available";
import { WidgetHeaderContext } from "./components/WidgetHeaderContext";
import { UpdateDataSourceFn, useDG2ExportApi } from "./features/export";
import "./ui/Datagrid.scss";
Expand All @@ -27,9 +28,11 @@

const Container = observer((props: Props): ReactElement => {
const isInfiniteLoad = props.pagination === "virtualScrolling" || props.pagination === "loadMore";
const currentPage = isInfiniteLoad
? props.datasource.limit / props.pageSize
: props.datasource.offset / props.pageSize;
const pageSizeNew =
props.pageSizeType === "dynamic"
? Number(isAvailable(props.dynamicPageSize) ? props.dynamicPageSize.value : 0)
: props.pageSize;
const currentPage = isInfiniteLoad ? props.datasource.limit / pageSizeNew : props.datasource.offset / pageSizeNew;

const { FilterContext } = useFilterContext();
const { columnsStore, rootStore } = props;
Expand All @@ -54,7 +57,6 @@
if (limit != null) {
props.datasource?.setLimit(limit);
}

if (reload) {
props.datasource.reload();
}
Expand All @@ -70,17 +72,22 @@
}, props.refreshInterval * 1000);
}
}, [props.datasource, props.refreshInterval]);

useEffect(() => {
if (props.pageSizeType === "dynamic") {
props.datasource.setLimit(pageSizeNew);
props.datasource.setOffset(0);
}
}, [pageSizeNew]);

Check warning on line 80 in packages/pluggableWidgets/datagrid-web/src/Datagrid.tsx

View workflow job for this annotation

GitHub Actions / Run code quality check

React Hook useEffect has missing dependencies: 'props.datasource' and 'props.pageSizeType'. Either include them or remove the dependency array
const setPage = useCallback(
(computePage: (prevPage: number) => number) => {
const newPage = computePage(currentPage);
if (isInfiniteLoad) {
props.datasource.setLimit(newPage * props.pageSize);
props.datasource.setLimit(newPage * pageSizeNew);
} else {
props.datasource.setOffset(newPage * props.pageSize);
props.datasource.setOffset(newPage * pageSizeNew);
}
},
[props.datasource, props.pageSize, isInfiniteLoad, currentPage]
[props.datasource, pageSizeNew, isInfiniteLoad, currentPage]
);

const selectionHelper = useSelectionHelper(props.itemSelection, props.datasource, props.onSelectionChange);
Expand All @@ -101,7 +108,7 @@
const focusController = useFocusTargetController({
rows: items.length,
columns: visibleColumnsCount,
pageSize: props.pageSize
pageSize: pageSizeNew
});

const cellEventsController = useCellEventsController(selectActionHelper, clickActionHelper, focusController);
Expand Down Expand Up @@ -163,13 +170,16 @@
</WidgetHeaderContext>
)
}
hasMoreItems={props.datasource.hasMoreItems ?? false}
hasMoreItems={
pageSizeNew !== 0 &&
(props.datasource.hasMoreItems || (currentPage + 1) * pageSizeNew < (props.datasource.totalCount || 0))
}
headerWrapperRenderer={useCallback((_columnIndex: number, header: ReactElement) => header, [])}
id={useMemo(() => `DataGrid${generateUUID()}`, [])}
numberOfItems={props.datasource.totalCount}
onExportCancel={abort}
page={currentPage}
pageSize={props.pageSize}
pageSize={pageSizeNew}
paginationType={props.pagination}
loadMoreButtonCaption={props.loadMoreButtonCaption?.value}
paging={useShowPagination({
Expand Down
15 changes: 15 additions & 0 deletions packages/pluggableWidgets/datagrid-web/src/Datagrid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@
</property>
</propertyGroup>
<propertyGroup caption="Rows">
<property key="pageSizeType" type="enumeration" defaultValue="static">
<caption>Pagination</caption>
<description />
<enumerationValues>
<enumerationValue key="static">Static</enumerationValue>
<enumerationValue key="dynamic">Dynamic</enumerationValue>
</enumerationValues>
</property>
<property key="dynamicPageSize" type="attribute" required="true">
<caption>PageSize</caption>
<description>Select an attribute for dynamic page size.</description>
<attributeTypes>
<attributeType name="Integer" />
</attributeTypes>
</property>
<property key="pageSize" type="integer" defaultValue="20">
<caption>Page size</caption>
<description />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export interface ColumnsType {
wrapText: boolean;
}

export type PageSizeTypeEnum = "static" | "dynamic";

export type PaginationEnum = "buttons" | "virtualScrolling" | "loadMore";

export type PagingPositionEnum = "bottom" | "top" | "both";
Expand Down Expand Up @@ -107,6 +109,8 @@ export interface DatagridContainerProps {
showSelectAllToggle: boolean;
columns: ColumnsType[];
columnsFilterable: boolean;
pageSizeType: PageSizeTypeEnum;
dynamicPageSize: EditableValue<Big>;
pageSize: number;
pagination: PaginationEnum;
pagingPosition: PagingPositionEnum;
Expand Down Expand Up @@ -150,6 +154,8 @@ export interface DatagridPreviewProps {
showSelectAllToggle: boolean;
columns: ColumnsPreviewType[];
columnsFilterable: boolean;
pageSizeType: PageSizeTypeEnum;
dynamicPageSize: string;
pageSize: number | null;
pagination: PaginationEnum;
pagingPosition: PagingPositionEnum;
Expand Down
Loading