"use client"; import type { ColumnDef, SortingState } from "@tanstack/react-table"; import { flexRender, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import * as React from "react"; import { Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { cn } from "@/lib/utils"; const formatPrice = (value: number) => new Intl.NumberFormat("fa-IR", { style: "currency", currency: "IRR", }).format(value); const formatNumber = (value: number) => value.toLocaleString("fa-IR"); type LineItem = { id: number; sku: string; name: string; description: string; quantity: number; unitPrice: number; }; type LineItemColumnMeta = { headerClassName?: string; cellClassName?: string; }; const lineItems: LineItem[] = [ { id: 1, sku: "RBK-32-MN", name: "کوله‌پشتی ریج‌وی", description: "سرمه‌ای تیره · ظرفیت ۳۲ لیتر", quantity: 2, unitPrice: 145, }, { id: 2, sku: "EVB-20-SL", name: "بطری روزمره ۲۰ اونسی", description: "خاکستری · استیل سه‌لایه", quantity: 3, unitPrice: 28, }, { id: 3, sku: "DBH-FT-NV", name: "هودی پشمی سپیده‌دم", description: "پارچه فرنچ تری · رنگ شب‌افت", quantity: 1, unitPrice: 95, }, { id: 4, sku: "TRV-ORG-SET", name: "ست نظم‌دهنده سفر", description: "کیسه‌های مشبک · بسته ۴ عددی", quantity: 1, unitPrice: 72, }, { id: 5, sku: "EXP-90-OLV", name: "کیف دوشی اکسپدیشن ۹۰ لیتر", description: "ریپ‌استاپ ضدآب · زیتونی", quantity: 1, unitPrice: 260, }, ]; const lineTotal = (item: LineItem) => item.quantity * item.unitPrice; export function DataTable25({ className }: { className?: string }) { const [sorting, setSorting] = React.useState([]); const columns = React.useMemo[]>( () => [ { id: "product", header: "محصول", cell: ({ row }) => { const item = row.original; return (
{item.name}
{item.description}
کد کالا: {item.sku}
); }, meta: { headerClassName: "py-3.5 ps-4 pe-3 text-start text-sm font-semibold text-foreground sm:ps-0 h-auto", cellClassName: "max-w-0 py-5 ps-4 pe-3 align-top text-sm sm:ps-0 text-foreground", }, }, { accessorKey: "quantity", header: "تعداد", cell: ({ row }) => formatNumber(row.original.quantity), meta: { headerClassName: "hidden px-3 py-3.5 text-end text-sm font-semibold text-foreground sm:table-cell", cellClassName: "hidden px-3 py-5 text-end text-sm text-muted-foreground sm:table-cell", }, }, { accessorKey: "unitPrice", header: "قیمت واحد", cell: ({ row }) => formatPrice(row.original.unitPrice), meta: { headerClassName: "hidden px-3 py-3.5 text-end text-sm font-semibold text-foreground sm:table-cell", cellClassName: "hidden px-3 py-5 text-end text-sm text-muted-foreground sm:table-cell", }, }, { id: "total", header: "جمع", cell: ({ row }) => formatPrice(lineTotal(row.original)), meta: { headerClassName: "py-3.5 ps-3 pe-4 text-end text-sm font-semibold text-foreground sm:pe-0 h-auto", cellClassName: "py-5 ps-3 pe-4 text-end text-sm text-muted-foreground sm:pe-0", }, }, ], [], ); const table = useReactTable({ data: lineItems, columns, state: { sorting }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getRowId: (row) => String(row.id), }); const subtotalAmount = lineItems.reduce( (sum, item) => sum + lineTotal(item), 0, ); const taxAmount = 64.08; const totalAmount = subtotalAmount + taxAmount; const subtotal = formatPrice(subtotalAmount); const tax = formatPrice(taxAmount); const total = formatPrice(totalAmount); return (

جدول داده با رکورد‌های جمع‌بندی

جدول داده با رکورد‌های خلاصه، مناسب نمایش فاکتورها

{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { const meta = (header.column.columnDef.meta as LineItemColumnMeta) ?? {}; return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => { const meta = (cell.column.columnDef.meta as LineItemColumnMeta) ?? {}; return ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ); })} ))} جمع جزء جمع جزء {subtotal} مالیات مالیات {tax} جمع کل جمع کل {total}
); }