From 40e61babfbc63bba951bc0e98986022d736dc033 Mon Sep 17 00:00:00 2001 From: "Qingyang(Abby) Hu" Date: Thu, 1 Feb 2024 16:54:59 -0800 Subject: [PATCH] Add pagination to discover embeddable (#5770) * Add pagination Signed-off-by: abbyhu2000 * fix an errir Signed-off-by: abbyhu2000 * small change Signed-off-by: abbyhu2000 * fix errors Signed-off-by: abbyhu2000 --------- Signed-off-by: abbyhu2000 --- .../components/data_grid/data_grid_table.tsx | 4 + .../default_discover_table.tsx | 86 +++++++++++++++---- .../search_embeddable_component.tsx | 1 + 3 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx b/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx index c738bfd65d50..f2ee6d078e2b 100644 --- a/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx +++ b/src/plugins/discover/public/application/components/data_grid/data_grid_table.tsx @@ -41,6 +41,7 @@ export interface DataGridTableProps { isContextView?: boolean; isLoading?: boolean; storage: Storage; + showPagination?: boolean; } export const DataGridTable = ({ @@ -61,6 +62,7 @@ export const DataGridTable = ({ isContextView = false, isLoading = false, storage, + showPagination, }: DataGridTableProps) => { const { services } = useOpenSearchDashboards(); @@ -161,6 +163,7 @@ export const DataGridTable = ({ onFilter={onFilter} onClose={() => setInspectedHit(undefined)} sampleSize={pageSizeLimit} + showPagination={showPagination} /> ), [ @@ -175,6 +178,7 @@ export const DataGridTable = ({ onAddColumn, onFilter, pageSizeLimit, + showPagination, ] ); diff --git a/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx b/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx index 05fe7f5cdb33..496237d7ca8d 100644 --- a/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx +++ b/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx @@ -12,6 +12,9 @@ import { EuiDataGridColumn, EuiDataGridSorting, EuiProgress, + EuiPagination, + EuiFlexGroup, + EuiFlexItem, } from '@elastic/eui'; import { FormattedMessage } from '@osd/i18n/react'; import { TableHeader } from './table_header'; @@ -35,6 +38,7 @@ export interface DefaultDiscoverTableProps { onFilter: DocViewFilterFn; onClose: () => void; sampleSize: number; + showPagination?: boolean; } export const LegacyDiscoverTable = ({ @@ -50,8 +54,15 @@ export const LegacyDiscoverTable = ({ onFilter, onClose, sampleSize, + showPagination, }: DefaultDiscoverTableProps) => { + const pageSize = 50; const [renderedRowCount, setRenderedRowCount] = useState(50); // Start with 50 rows + const [displayedRows, setDisplayedRows] = useState(rows.slice(0, pageSize)); + const [currentRowCounts, setCurrentRowCounts] = useState({ + startRow: 0, + endRow: pageSize, + }); const observerRef = useRef(null); const sentinelRef = useRef(null); @@ -81,9 +92,48 @@ export const LegacyDiscoverTable = ({ }; }, []); + const [activePage, setActivePage] = useState(0); + const pageCount = Math.ceil(rows.length / pageSize); + + const goToPage = (pageNumber: number) => { + const startRow = pageNumber * pageSize; + const endRow = + rows.length < pageNumber * pageSize + pageSize + ? rows.length + : pageNumber * pageSize + pageSize; + setCurrentRowCounts({ + startRow, + endRow, + }); + setDisplayedRows(rows.slice(startRow, endRow)); + setActivePage(pageNumber); + }; + return ( indexPattern && ( <> + {showPagination ? ( + + + goToPage(currentPage)} + /> + + + + + + ) : null} - {rows.slice(0, renderedRowCount).map((row: OpenSearchSearchHit, index: number) => { - return ( - column.id)} - columns={columns} - indexPattern={indexPattern} - onRemoveColumn={onRemoveColumn} - onAddColumn={onAddColumn} - onFilter={onFilter} - onClose={onClose} - /> - ); - })} + {(showPagination ? displayedRows : rows.slice(0, renderedRowCount)).map( + (row: OpenSearchSearchHit, index: number) => { + return ( + column.id)} + columns={columns} + indexPattern={indexPattern} + onRemoveColumn={onRemoveColumn} + onAddColumn={onAddColumn} + onFilter={onFilter} + onClose={onClose} + /> + ); + } + )}
- {renderedRowCount < rows.length && ( + {!showPagination && renderedRowCount < rows.length && (
)} - {rows.length === sampleSize && ( + {!showPagination && rows.length === sampleSize && (