Data display
ColumnsMenu
Shows and hides table columns. It takes the DataTable's columns and lists those that can be hidden (hideable: false keeps a column); give the hidden keys to DataTable as hiddenColumns. The example also shows pageSizeOptions (a rows-per-page choice that goes back to page 1) and stickyHeader (the table scrolls in its own box and the header row stays in view).
import { ColumnsMenu } from "@percentsa/ui";Examples
| Status | |||
|---|---|---|---|
| INV-1040 | Masar | Paid | 1,500 |
| INV-1041 | Nakhla Foods | Overdue | 11,250 |
| INV-1042 | Raha Clinics | Due | 11,000 |
| INV-1043 | Sadeem | Paid | 10,750 |
| INV-1044 | Wadi Tech | Overdue | 10,500 |
| INV-1045 | Bayt Coffee | Due | 10,250 |
| INV-1046 | Najm Logistics | Paid | 10,000 |
| INV-1047 | Masar | Overdue | 9,750 |
| INV-1048 | Nakhla Foods | Due | 9,500 |
| INV-1049 | Raha Clinics | Paid | 9,250 |
Code
export function ColumnsMenuExample() {
const t = useT();
const columns = useInvoiceColumns().map((c) => (c.key === "id" ? { ...c, hideable: false } : c));
const [hidden, setHidden] = useState<string[]>([]);
const [size, setSize] = useState(10);
return (
<div className="grid w-full grid-cols-1 gap-3">
<ColumnsMenu className="justify-self-end" columns={columns} hidden={hidden} onHiddenChange={setHidden} />
<DataTable
columns={columns}
rows={INVOICES}
getRowId={(r) => r.id}
caption={t("Invoices", "الفواتير")}
hideCaption
hiddenColumns={hidden}
pageSize={size}
pageSizeOptions={[5, 10, 25]}
onPageSizeChange={setSize}
stickyHeader
/>
</div>
);
}
const PAGE_SIZE = 5;
// Stands in for the server: it sorts and slices, and the table shows the rows as given.
function fetchInvoices(sort: DataTableSort, page: number) {
const sign = sort.direction === "asc" ? 1 : -1;
const sorted = [...INVOICES].sort((a, b) => sign * (sort.key === "amount" ? a.amount - b.amount : a.id.localeCompare(b.id)));
return sorted.slice((page - 1) * PAGE_SIZE, page * PAGE_SIZE);
}