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

[Table Visualization] restore pagination to table vis #2461

Merged
Merged
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 @@ -10,6 +10,7 @@ import { IInterpreterRenderHandlers } from 'src/plugins/expressions';
import { Table } from '../table_vis_response_handler';
import { TableVisConfig } from '../types';
import { getDataGridColumns } from './table_vis_grid_columns';
import { usePagination } from '../utils';

interface TableVisComponentProps {
table: Table;
Expand All @@ -19,15 +20,8 @@ interface TableVisComponentProps {

export const TableVisComponent = ({ table, visConfig, handlers }: TableVisComponentProps) => {
const { rows, columns } = table;
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 5 });
const onChangeItemsPerPage = useCallback(
(pageSize) => setPagination((p) => ({ ...p, pageSize, pageIndex: 0 })),
[setPagination]
);

const onChangePage = useCallback((pageIndex) => setPagination((p) => ({ ...p, pageIndex })), [
setPagination,
]);
const pagination = usePagination(visConfig, rows.length);

// toDo: it is a sample renderCellValue to render a data grid component
// will check on it and it might be replaced
Expand All @@ -37,15 +31,16 @@ export const TableVisComponent = ({ table, visConfig, handlers }: TableVisCompon

// If we are doing the pagination (instead of leaving that to the grid)
// then the row index must be adjusted as `data` has already been pruned to the page size
adjustedRowIndex = rowIndex - pagination.pageIndex * pagination.pageSize;
adjustedRowIndex = rowIndex - pagination!.pageIndex * pagination!.pageSize;

return rows.hasOwnProperty(adjustedRowIndex)
? rows[adjustedRowIndex][columnId] || null
: null;
}) as EuiDataGridProps['renderCellValue'];
}, [rows, pagination.pageIndex, pagination.pageSize]);
}, [rows, pagination]);

const dataGridColumns = getDataGridColumns(table, visConfig, handlers);

return (
<EuiDataGrid
aria-label="tableVis"
Expand All @@ -56,11 +51,7 @@ export const TableVisComponent = ({ table, visConfig, handlers }: TableVisCompon
}}
rowCount={rows.length}
renderCellValue={renderCellValue}
pagination={{
...pagination,
onChangeItemsPerPage,
onChangePage,
}}
pagination={pagination}
/>
);
};
2 changes: 1 addition & 1 deletion src/plugins/vis_type_table/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface TableVisConfig extends TableVisParams {
}

export interface TableVisParams {
perPage: number | '';
perPage: number;
showPartialRows: boolean;
showMetricsAtAllLevels: boolean;
showTotal: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_type_table/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/

export * from './convert_to_formatted_data';
export * from './use_pagination';
38 changes: 38 additions & 0 deletions src/plugins/vis_type_table/public/utils/use_pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { useCallback, useEffect, useMemo, useState } from 'react';
import { TableVisConfig } from '../types';

export const usePagination = (visConfig: TableVisConfig, nrow: number) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit nrow -> nRow?

const [pagination, setPagination] = useState({
pageIndex: 0,
pageSize: visConfig.perPage,
});
const onChangeItemsPerPage = useCallback(
(pageSize) => setPagination((p) => ({ ...p, pageSize, pageIndex: 0 })),
[setPagination]
);
const onChangePage = useCallback((pageIndex) => setPagination((p) => ({ ...p, pageIndex })), [
setPagination,
]);

useEffect(() => {
const maxiPageIndex = Math.ceil(nrow / visConfig.perPage) - 1;
setPagination((p) => ({
pageIndex: p.pageIndex > maxiPageIndex ? maxiPageIndex : p.pageIndex,
pageSize: visConfig.perPage,
}));
}, [nrow, visConfig.perPage]);

return useMemo(
() => ({
...pagination,
onChangeItemsPerPage,
onChangePage,
}),
[pagination, onChangeItemsPerPage, onChangePage]
);
};