Skip to content
العربية
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

Invoices
Status
INV-1040MasarPaid1,500
INV-1041Nakhla FoodsOverdue11,250
INV-1042Raha ClinicsDue11,000
INV-1043SadeemPaid10,750
INV-1044Wadi TechOverdue10,500
INV-1045Bayt CoffeeDue10,250
INV-1046Najm LogisticsPaid10,000
INV-1047MasarOverdue9,750
INV-1048Nakhla FoodsDue9,500
INV-1049Raha ClinicsPaid9,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);
}