DataTable
- Ejecutar
npx shadcn-ui@latest add table
,npm install @tanstack/react-table
ynpx shadcn-ui@latest add badge
- Crear
app/(dashboard)/(routes)/teacher/courses/_components/columns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"use client"
import { ColumnDef } from "@tanstack/react-table"
// This type is used to define the shape of our data.
// You can use a Zod schema here if you want.
export type Payment = {
id: string
amount: number
status: "pending" | "processing" | "success" | "failed"
email: string
}
export const columns: ColumnDef<Payment>[] = [
{
accessorKey: "status",
header: "Status",
},
{
accessorKey: "email",
header: "Email",
},
{
accessorKey: "amount",
header: "Amount",
},
]
- Crear
app/(dashboard)/(routes)/teacher/courses/_components/data-table.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use client"
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
}
export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
})
return (
<div className="rounded-md border-2 border-t-black border-b-black dark:border-t-white dark:border-b-white bg-white dark:bg-black">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
)
}
- Editar
app/(dashboard)/(routes)/teacher/courses/page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Button } from "@/components/ui/button";
import Link from "next/link";
import { DataTable } from "./_components/data-table";
import { columns } from "./_components/columns";
async function getData(): Promise<any[]> {
// Fetch data from your API here.
return [
{
id: "728ed52f",
amount: 100,
status: "pending",
email: "m@example.com",
},
// ...
]
}
const CoursesPage = async () => {
const data = await getData();
return (
<div className="p-6">
<Link href="/teacher/create">
<Button> Crear curso </Button>
</Link>
<DataTable columns={columns} data={data} />
</div>
);
}
export default CoursesPage;
- Ejecutar la aplicación para ver los datos en la datatable
- Editar
app/(dashboard)/(routes)/teacher/courses/_components/columns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"use client"
import { tbl_cursos } from "@prisma/client"
import { ColumnDef } from "@tanstack/react-table"
export const columns: ColumnDef<tbl_cursos>[] = [
{
accessorKey: "titulo",
header: "Titulo",
},
{
accessorKey: "categoria.nombre",
header: "Categoria",
},
{
accessorKey: "precio",
header: "Precio",
},
{
accessorKey: "publicado",
header: "Publicado",
},
]
- Editar
app/(dashboard)/(routes)/teacher/courses/page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { DataTable } from "./_components/data-table";
import { columns } from "./_components/columns";
import { auth } from "@clerk/nextjs";
import { redirect } from "next/navigation";
import { db } from "@/lib/db";
const CoursesPage = async () => {
const { userId } = auth();
if (!userId) {
return redirect("/");
}
const courses = await db.tbl_cursos.findMany({
where: {
id_usuario: userId
},
orderBy: {
createdAt: "desc",
},
include: {
categoria: true
}
});
return (
<div className="p-6">
<div className="mt-6 border-2 bg-[#cfcfcf] dark:bg-[#1f1f1f] rounded-md p-4 border-solid">
<DataTable columns={columns} data={courses} />
</div>
</div>
);
}
export default CoursesPage;
- Agregar paginación, editar
app/(dashboard)/(routes)/teacher/courses/_components/data-table.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"use client"
import { Button } from "@/components/ui/button"
import {
ColumnDef,
flexRender,
getCoreRowModel,
getPaginationRowModel,
useReactTable,
} from "@tanstack/react-table"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
}
export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
})
return (
<div>
<div className="rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</div>
)
}
- Agregar re-ordenamiento en cabeceras de las columnas, editar
app/(dashboard)/(routes)/teacher/courses/_components/data-table.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use client"
import { Button } from "@/components/ui/button";
import * as React from "react";
import {
ColumnDef,
SortingState,
flexRender,
getCoreRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
}
export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = React.useState<SortingState>([])
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
state: {
sorting,
},
})
return (
<div>
<div className="rounded-md border-2 border-t-black border-b-black dark:border-t-white dark:border-b-white bg-white dark:bg-black">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</div>
)
}
- Editar
app/(dashboard)/(routes)/teacher/courses/_components/columns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use client"
import { Button } from "@/components/ui/button"
import { tbl_cursos } from "@prisma/client"
import { ColumnDef } from "@tanstack/react-table"
import { ArrowUpDown, MoreHorizontal } from "lucide-react"
export const columns: ColumnDef<tbl_cursos>[] = [
{
accessorKey: "titulo",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Titulo
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
accessorKey: "categoria.nombre",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Categoria
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
accessorKey: "precio",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Precio
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
}, },
{
accessorKey: "publicado",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Publicado
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
}, },
]
- Agregar Acciones, editar
app/(dashboard)/(routes)/teacher/courses/_components/columns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use client"
import { Button } from "@/components/ui/button"
import { tbl_cursos } from "@prisma/client"
import { ColumnDef } from "@tanstack/react-table"
import { ArrowUpDown, MoreHorizontal, Pencil } from "lucide-react"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import Link from "next/link"
export const columns: ColumnDef<tbl_cursos>[] = [
{
accessorKey: "titulo",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Titulo
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
accessorKey: "categoria.nombre",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Categoria
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
accessorKey: "precio",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Precio
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
accessorKey: "publicado",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Publicado
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
id: "actions",
header: "Acciones",
cell: ({ row }) => {
const { uuid, titulo } = row.original;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Acciones</DropdownMenuLabel>
<DropdownMenuSeparator />
<Link href={`/teacher/courses/${uuid}`}>
<DropdownMenuItem>
<Pencil className="h-4 w-4 mr-2" />
Editar Curso
</DropdownMenuItem>
</Link>
<DropdownMenuItem
onClick={() => navigator.clipboard.writeText(uuid)}
>
Copiar UUID del curso
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => navigator.clipboard.writeText(titulo)}
>
Copiar Titulo del curso
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
},
},
]
- Formatear columnas de precio y publicado, editar
app/(dashboard)/(routes)/teacher/courses/_components/columns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use client"
import { Button } from "@/components/ui/button"
import { tbl_cursos } from "@prisma/client"
import { ColumnDef } from "@tanstack/react-table"
import { ArrowUpDown, MoreHorizontal, Pencil } from "lucide-react"
import { Badge } from "@/components/ui/badge";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import Link from "next/link"
import { cn } from "@/lib/utils"
export const columns: ColumnDef<tbl_cursos>[] = [
{
accessorKey: "titulo",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Titulo
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
accessorKey: "categoria.nombre",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Categoria
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
},
{
accessorKey: "precio",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Precio
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
cell: ({ row }) => {
const price = parseFloat(row.getValue("precio") || "0");
const formatted = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(price);
return <div>{formatted}</div>
}
},
{
accessorKey: "publicado",
header: ({ column }) => {
return (
<Button
variant="customghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Publicado
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
cell: ({ row }) => {
const isPublished = row.getValue("publicado") || false;
return (
<Badge className={cn(
"bg-sky-500 text-black",
isPublished && "bg-green-600 text-black"
)}>
{isPublished ? "Publicado" : "Borrador"}
</Badge>
)
}
},
{
id: "actions",
header: "Acciones",
cell: ({ row }) => {
const { uuid, titulo } = row.original;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Acciones</DropdownMenuLabel>
<DropdownMenuSeparator />
<Link href={`/teacher/courses/${uuid}`}>
<DropdownMenuItem>
<Pencil className="h-4 w-4 mr-2" />
Editar Curso
</DropdownMenuItem>
</Link>
<DropdownMenuItem
onClick={() => navigator.clipboard.writeText(uuid)}
>
Copiar UUID del curso
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => navigator.clipboard.writeText(titulo)}
>
Copiar Titulo del curso
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
},
},
]
- Agregar busqueda y posicionar boton de Crear Curso, editar
app/(dashboard)/(routes)/teacher/courses/_components/data-table.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use client"
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"
import * as React from "react";
import Link from "next/link"
import {
ColumnDef,
ColumnFiltersState,
SortingState,
flexRender,
getCoreRowModel,
getFilteredRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
import { PlusCircle } from "lucide-react";
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
}
export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = React.useState<SortingState>([])
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
[]
)
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
onColumnFiltersChange: setColumnFilters,
getFilteredRowModel: getFilteredRowModel(),
state: {
sorting,
columnFilters,
},
})
return (
<div>
<div className="flex items-center py-4 justify-between">
<Input
placeholder="Buscar..."
value={(table.getColumn("titulo")?.getFilterValue() as string) ?? ""}
onChange={(event) =>
table.getColumn("titulo")?.setFilterValue(event.target.value)
}
className="max-w-sm"
/>
<Link href="/teacher/create">
<Button>
<PlusCircle className="h-4 w-4 mr-2" />
Crear curso
</Button>
</Link>
</div>
<div className="rounded-md border-2 border-t-black border-b-black dark:border-t-white dark:border-b-white bg-white dark:bg-black">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</div>
)
}
- Agregar busqueda global, editar
app/(dashboard)/(routes)/teacher/courses/_components/data-table.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use client"
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"
import * as React from "react";
import Link from "next/link"
import {
ColumnDef,
ColumnFiltersState,
SortingState,
flexRender,
getCoreRowModel,
getFilteredRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
import { PlusCircle } from "lucide-react";
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
}
export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = React.useState<SortingState>([])
//const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([])
const [filtering, setFiltering] = React.useState<string>("")
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
//onColumnFiltersChange: setColumnFilters,
getFilteredRowModel: getFilteredRowModel(),
state: {
sorting,
//columnFilters,
globalFilter: filtering,
},
onGlobalFilterChange: setFiltering,
})
return (
<div>
<div className="flex items-center py-4 justify-between">
<Input
placeholder="Buscar..."
type="text"
value={ filtering }
//value={(table.getColumn("titulo")?.getFilterValue() as string) ?? ""}
onChange={(event) =>
//table.getColumn("titulo")?.setFilterValue(event.target.value)
setFiltering(event.target.value)
}
className="max-w-sm"
/>
<Link href="/teacher/create">
<Button>
<PlusCircle className="h-4 w-4 mr-2" />
Crear curso
</Button>
</Link>
</div>
<div className="rounded-md border-2 border-t-black border-b-black dark:border-t-white dark:border-b-white bg-white dark:bg-black">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<Button
variant="outline"
size="sm"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</div>
)
}