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(gui): TableView's sorting and pagination #466

Merged
merged 1 commit into from
Feb 23, 2024
Merged
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
60 changes: 52 additions & 8 deletions ui/web/src/modules/Interactive/TableView.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { Space } from '@douyinfe/semi-ui';
import { Table, flexRender } from '@tanstack/react-table';
import { IconCaretdown, IconCaretup } from '@douyinfe/semi-icons';
import { Pagination, Space } from '@douyinfe/semi-ui';
import {
Table,
flexRender,
getFilteredRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
} from '@tanstack/react-table';

export function TableView<T>(props: { table: Table<T> }) {
const { table } = props;
const { table: t } = props;

const table = useReactTable({
...t.options,
getFilteredRowModel: getFilteredRowModel(),
getPaginationRowModel: getPaginationRowModel(),
getSortedRowModel: getSortedRowModel(),
autoResetPageIndex: false,
autoResetExpanded: false,
});

return (
<Space vertical align="start" style={{ width: '100%' }}>
<div>{table.getRowModel().rows.length} Items</div>
<div>{table.options.data.length} Items</div>
<table className="semi-table">
<thead
className="semi-table-thead"
Expand All @@ -14,10 +32,20 @@ export function TableView<T>(props: { table: Table<T> }) {
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id} className="semi-table-row">
{headerGroup.headers.map((header) => (
<th key={header.id} className="semi-table-row-head">
{header.isPlaceholder
? null
: flexRender(header.column.columnDef.header, header.getContext())}
<th
key={header.id}
className="semi-table-row-head"
onClick={header.column.getToggleSortingHandler()}
>
<Space>
{header.isPlaceholder
? null
: flexRender(header.column.columnDef.header, header.getContext())}
{{
asc: <IconCaretup />,
desc: <IconCaretdown />,
}[header.column.getIsSorted() as string] ?? null}
</Space>
</th>
))}
</tr>
Expand Down Expand Up @@ -48,6 +76,22 @@ export function TableView<T>(props: { table: Table<T> }) {
))}
</tfoot>
</table>
<Pagination
total={table.options.data.length}
showTotal
showQuickJumper
showSizeChanger
pageSizeOpts={[10, 20, 50, 200]}
pageSize={table.getState().pagination.pageSize}
onPageSizeChange={(x) => {
table.setPageSize(x);
}}
// Semi UI's page is started from 1. Tanstack Table index is started from 0
currentPage={table.getState().pagination.pageIndex + 1}
onPageChange={(x) => {
table.setPageIndex(x - 1);
}}
/>
</Space>
);
}
Loading