"use client"; import type { ColumnDef, Row, SortingState } from "@tanstack/react-table"; import { flexRender, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import * as React from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { cn } from "@/lib/utils"; type Person = { name: string; title: string; email: string; role: string; }; type EmploymentGroup = { type: string; people: Person[]; }; type FlattenedPerson = Person & { employmentType: string; }; type ColumnMeta = { headerClassName?: string; cellClassName?: string; }; const employmentGroups: EmploymentGroup[] = [ { type: "تمام‌وقت", people: [ { name: "امیلیا هاکینز", title: "مدیر ارشد محصول", email: "amelia.hawkins@example.com", role: "مالک", }, { name: "الیاس پاتل", title: "معمار نرم‌افزار", email: "elias.patel@example.com", role: "مدیر", }, { name: "مژگان رید", title: "سرپرست موفقیت مشتری", email: "morgan.reed@example.com", role: "عضو", }, ], }, { type: "قراردادی", people: [ { name: "ایلا بنت", title: "استراتژیست برند", email: "isla.bennett@example.com", role: "مدیر", }, { name: "نوح کیم", title: "مشاور امنیت", email: "noah.kim@example.com", role: "عضو", }, ], }, { type: "کارآموزی", people: [ { name: "نیلوفر Sharma", title: "کارآموز طراحی محصول", email: "priya.sharma@example.com", role: "کارآموز", }, { name: "دانیال Brooks", title: "کارآموز بازاریابی", email: "caleb.brooks@example.com", role: "کارآموز", }, ], }, { type: "پاره‌وقت", people: [ { name: "لیلا Alvarez", title: "نویسنده فنی", email: "sofia.alvarez@example.com", role: "عضو", }, ], }, ]; export function DataTable23({ className }: { className?: string }) { const [sorting, setSorting] = React.useState([]); const flatData = React.useMemo(() => { const result: FlattenedPerson[] = []; employmentGroups.forEach((group) => { group.people.forEach((person) => result.push({ ...person, employmentType: group.type, }), ); }); return result; }, []); const columns = React.useMemo[]>( () => [ { accessorKey: "name", header: "نام مخاطب", cell: ({ row }) => (
{row.original.name}
), meta: { headerClassName: "ps-6 pe-3 text-start text-sm sm:ps-4", cellClassName: "py-4 ps-6 pe-3 text-start sm:ps-4", }, }, { accessorKey: "title", header: "سمت", cell: ({ row }) => ( {row.original.title} ), meta: { headerClassName: "px-3 text-start text-sm", cellClassName: "px-3 py-4", }, }, { accessorKey: "email", header: "ایمیل", cell: ({ row }) => ( {row.original.email} ), meta: { headerClassName: "px-3 text-start text-sm", cellClassName: "px-3 py-4", }, }, { accessorKey: "role", header: "نقش", cell: ({ row }) => ( {row.original.role} ), meta: { headerClassName: "px-3 text-start text-sm", cellClassName: "px-3 py-4", }, }, ], [], ); const table = useReactTable({ data: flatData, columns, state: { sorting, }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getRowId: (row) => row.email, }); const rowModelRows = table.getRowModel().rows; const groupedRows = React.useMemo(() => { if (!rowModelRows.length) { return []; } const groups: Array<{ employmentType: string; rows: Row[]; }> = []; let currentEmploymentType = ""; let currentGroup: Row[] = []; rowModelRows.forEach((row) => { if (row.original.employmentType !== currentEmploymentType) { if (currentGroup.length > 0) { groups.push({ employmentType: currentEmploymentType, rows: currentGroup, }); } currentEmploymentType = row.original.employmentType; currentGroup = [row]; } else { currentGroup.push(row); } }); if (currentGroup.length > 0) { groups.push({ employmentType: currentEmploymentType, rows: currentGroup, }); } return groups; }, [rowModelRows]); const columnCount = columns.length; return (

جدول با رکورد‌های گروه‌بندی‌شده

جدول داده با رکورد‌های گروه‌بندی‌شده برای تفکیک انواع مختلف همکاری در یک جدول.

{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { const meta = header.column.columnDef.meta as | ColumnMeta | undefined; return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} {groupedRows.length ? ( groupedRows.map((group) => ( {group.employmentType} {group.rows.map((row) => ( {row.getVisibleCells().map((cell) => { const meta = (cell.column.columnDef.meta as ColumnMeta) ?? {}; return ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ); })} ))} )) ) : ( نتیجه‌ای یافت نشد )}
); }