[note] Tanstack Table
常用 API
/**
* Header Groups and Header
**/
table.getHeaderGroups();
table.getFlatHeaders();
<thead>
{table.getHeaderGroups().map((headerGroup) => {
return (
<tr key={headerGroup.id}>
{headerGroup.headers.map(
(
header // map over the headerGroup headers array
) => (
<th key={header.id} colSpan={header.colSpan}>
{flexRender(header.column.columnDef.header, header.getContext())}
</th>
)
)}
</tr>
);
})}
</thead>;
/**
* Row
*
* table.getXXXRowModel().rows;
* table.getXXXRowModel().flatRows;
* table.getXXXRowModel().rowsById['row-id']; // 取得某一列的資料
**/
table.getRowModel().rows; // 取得所有 rows
table.getRow("rowId"); // 取得某一列的資料
row.getValue("columnName"); // 取得某一 row 中某個 column 的值,如果 value 是 undefined 會直接回傳
row.renderValue("columnName"); // 取得某一 row 中某個 column 的值,如果 value 是 undefined 會直接用 renderFallbackValue
row.original.columnName; // 取得 data 最原始的值(還沒被 accessor 轉換後的值)
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>{/* ... */}</tr>
))}
</tbody>;
/**
* Column
**/
table.getColumn("column-name"); // 取得欄位資訊
table.getAllColumns()
table.getAllFlatColumns();
header.column;
cell.column;
/**
* Cell
**/
cell.getValue("columnName"); // 是 row.getValue() 的捷徑
cell.renderValue("columnName"); // 是 row.renderValue() 的捷徑
cell.row.original.firstName; // 可以透過 cell 得到 row
row.getAllCells();
row.getVisibleCells();
<tr>
{row.getVisibleCells().map((cell) => {
return (
<td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
);
})}
</tr>;
// Filtered and Selected
table.getFilteredRowModel().rows;
table.getSelectedRowModel().rows;
table.getFilteredSelectedRowModel().rows;
// Pagination
table.previousPage();
table.nextPage();
table.getCanPreviousPage();
table.getCanNextPage();
/**
* Table State
**/
table.getState();
table.getState().rowSelection;
ColumnDef
[
columnHelper.accessor((row) => row.name, {
size: 200,
header: "Name(Age)",
cell: ({ getValue, row, table }) => {
// 拿 table 提供的 meta
const currentHour = table.options.meta?.currentHour;
// 使用 row 拿其他 column 的資料
const age = row.getValue<ApiData["age"]>("age");
// 用 getValue 可以直接拿這個 column 在這個 row(也就是這個 cell)的資料
const name = getValue<ApiData['name']>();
// 拿 raw data 中的資料
const name_original = row.original.name;
const age_original = row.original.age;
return (
<div>
{name}({age})
</div>
); // 渲染時顯示組合格式
},
footer: ({ table }) => {
// 使用 table.getCoreRowModel() 拿 row 來加總某個 column 的值
const totalRev = table
.getCoreRowModel()
.rows.reduce(
(sum, row) =>
sum + (row.getValue<ApiData["revenue"]>("revenue") ?? 0),
0
);
},
}),
];