Skip to content
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

feat(Datagrid-web): added pagination for load more & virtual scrolling #1270

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export function getProperties(
});
if (values.pagination !== "buttons") {
hidePropertyIn(defaultProperties, values, "showPagingButtons");
hidePropertyIn(defaultProperties, values, "pagingPosition");
}
if (values.pagination !== "loadMore") {
hidePropertyIn(defaultProperties, values, "loadMoreButtonCaption");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export function preview(props: DatagridPreviewProps): ReactElement {
headerWrapperRenderer={selectableWrapperRenderer(previewColumns)}
numberOfItems={props.pageSize ?? numberOfItems}
page={0}
isInfiniteLoad={false}
paginationType={props.pagination}
pageSize={props.pageSize ?? numberOfItems}
showPagingButtons={props.showPagingButtons}
Expand Down
6 changes: 6 additions & 0 deletions packages/pluggableWidgets/datagrid-web/src/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const Container = observer((props: Props): ReactElement => {
const items = props.datasource.items ?? [];

const [exportProgress, abortExport] = useDataExport(props, props.columnsStore, props.progressStore);
useEffect(() => {
if (props.pagination !== "buttons") {
props.datasource.requestTotalCount(true);
}
}, [props.datasource, props.pagination]);

useEffect(() => {
let timer: ReturnType<typeof setTimeout>;
Expand Down Expand Up @@ -98,6 +103,7 @@ const Container = observer((props: Props): ReactElement => {
columnsResizable={props.columnsResizable}
columnsSortable={props.columnsSortable}
data={items}
isInfiniteLoad={isInfiniteLoad}
emptyPlaceholderRenderer={useCallback(
(renderWrapper: (children: ReactNode) => ReactElement) =>
props.showEmptyPlaceholder === "custom" ? renderWrapper(props.emptyPlaceholder) : <div />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface WidgetProps<C extends GridColumn, T extends ObjectItem = Object
data: T[];
emptyPlaceholderRenderer?: (renderWrapper: (children: ReactNode) => ReactElement) => ReactElement;
exporting: boolean;
isInfiniteLoad: boolean;
filterRenderer: (renderWrapper: (children: ReactNode) => ReactElement, columnIndex: number) => ReactElement;
hasMoreItems: boolean;
headerContent?: ReactNode;
Expand Down Expand Up @@ -141,7 +142,7 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
const [isDragging, setIsDragging] = useState<[ColumnId | undefined, ColumnId, ColumnId | undefined] | undefined>();
const [dragOver, setDragOver] = useState<[ColumnId, "before" | "after"] | undefined>(undefined);
const showHeader = !!headerContent;
const showTopBar = paging && (pagingPosition === "top" || pagingPosition === "both");
const showTopBar = pagingPosition === "top" || pagingPosition === "both";

const renderFilterWrapper = useCallback(
(children: ReactNode) => (
Expand All @@ -152,19 +153,21 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
[isDragging]
);

const pagination = paging ? (
const pagination = (
<Pagination
canNextPage={hasMoreItems}
canPreviousPage={page !== 0}
gotoPage={(page: number) => setPage && setPage(() => page)}
nextPage={() => setPage && setPage(prev => prev + 1)}
numberOfItems={numberOfItems}
page={page}
paginationType={props.paginationType}
pageSize={pageSize}
isInfiniteLoad={props.isInfiniteLoad}
showPagingButtons={props.showPagingButtons}
previousPage={() => setPage && setPage(prev => prev - 1)}
/>
) : null;
);

const cssGridStyles = gridStyle(visibleColumns, {
selectItemColumn: selectActionHelper.showCheckboxColumn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function mockWidgetProps(): WidgetProps<GridColumn, ObjectItem> {
onExportCancel: jest.fn(),
page: 1,
pageSize: 10,
isInfiniteLoad: false,
paginationType: "buttons",
paging: false,
pagingPosition: "bottom",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface PaginationProps {
numberOfItems?: number;
page: number;
pageSize: number;
isInfiniteLoad: boolean;
paginationType: "buttons" | "virtualScrolling" | "loadMore";
previousPage: () => void;
setPaginationIndex?: Dispatch<SetStateAction<number>>;
showPagingButtons?: "always" | "auto";
Expand All @@ -25,8 +27,12 @@ export function Pagination(props: PaginationProps): ReactElement | null {
props.numberOfItems !== undefined ? Math.ceil(props.numberOfItems / props.pageSize) : undefined;
const lastPage = numberOfPages !== undefined ? numberOfPages - 1 : 0;
const hasLastPage = numberOfPages !== undefined;
const initialItem = props.numberOfItems === 0 ? 0 : props.page * props.pageSize + 1;
const lastItem = getLastItem(props.canNextPage, props.numberOfItems, props.page, props.pageSize);
const initialItem = props.numberOfItems === 0 ? 0 : props.isInfiniteLoad ? 1 : props.page * props.pageSize + 1;
const lastItem = props.isInfiniteLoad
? props.page * props.pageSize > (props.numberOfItems ?? 0)
? props.numberOfItems ?? 0
: props.page * props.pageSize
: getLastItem(props.canNextPage, props.numberOfItems ?? 0, props.page, props.pageSize);

const setPageIndex = (page: number): void => {
if (props.setPaginationIndex) {
Expand All @@ -46,9 +52,9 @@ export function Pagination(props: PaginationProps): ReactElement | null {
<div aria-label={props.labelPagination ?? "Pagination"} className="pagination-bar">
<button
className="btn pagination-button"
disabled={props.page === 0}
disabled={props.page === 0 || (props.isInfiniteLoad && props.page === 1)}
{...getEvents(() => {
props.gotoPage(0);
props.gotoPage(props.isInfiniteLoad ? 1 : 0);
setPageIndex(0);
})}
aria-label={props.labelFirstPage ?? "Go to first page"}
Expand All @@ -57,7 +63,7 @@ export function Pagination(props: PaginationProps): ReactElement | null {
</button>
<button
className="btn pagination-button"
disabled={!props.canPreviousPage}
disabled={!props.canPreviousPage || (props.isInfiniteLoad && props.page === 1)}
{...getEvents(() => {
props.previousPage();
setPageIndex(props.page - 1);
Expand Down Expand Up @@ -87,9 +93,9 @@ export function Pagination(props: PaginationProps): ReactElement | null {
<button
aria-label={props.labelLastPage ?? "Go to last page"}
className="btn pagination-button"
disabled={props.page === lastPage || props.numberOfItems === 0}
disabled={props.page === lastPage || props.numberOfItems === 0 || props.numberOfItems === lastItem}
{...getEvents(() => {
props.gotoPage(lastPage);
props.gotoPage(props.isInfiniteLoad ? lastPage + 1 : lastPage);
setPageIndex(lastPage);
})}
>
Expand Down
Loading