checkbox selection table
This commit is contained in:
parent
ee10608ef2
commit
ecd453cb41
|
|
@ -1,5 +1,4 @@
|
||||||
"use client";
|
"use client";
|
||||||
import { makeData } from "@/lib/utils/makeData";
|
|
||||||
import {
|
import {
|
||||||
ColumnDef,
|
ColumnDef,
|
||||||
ColumnResizeDirection,
|
ColumnResizeDirection,
|
||||||
|
|
@ -8,39 +7,20 @@ import {
|
||||||
getCoreRowModel,
|
getCoreRowModel,
|
||||||
getPaginationRowModel,
|
getPaginationRowModel,
|
||||||
getSortedRowModel,
|
getSortedRowModel,
|
||||||
PaginationState,
|
|
||||||
SortingState,
|
SortingState,
|
||||||
useReactTable,
|
useReactTable,
|
||||||
} from "@tanstack/react-table";
|
} from "@tanstack/react-table";
|
||||||
import React, { FC, useCallback, useEffect, useState } from "react";
|
import React, { useCallback, useEffect, useState } from "react";
|
||||||
import {
|
import { Button, Label, Table } from "flowbite-react";
|
||||||
Breadcrumb,
|
import { HiChevronLeft, HiChevronRight, HiPlus } from "react-icons/hi";
|
||||||
Button,
|
|
||||||
Label,
|
|
||||||
Modal,
|
|
||||||
Table,
|
|
||||||
TextInput,
|
|
||||||
} from "flowbite-react";
|
|
||||||
import {
|
|
||||||
HiChevronLeft,
|
|
||||||
HiChevronRight,
|
|
||||||
HiHome,
|
|
||||||
HiOutlinePencilAlt,
|
|
||||||
HiPlus,
|
|
||||||
HiSearch,
|
|
||||||
HiTrash,
|
|
||||||
} from "react-icons/hi";
|
|
||||||
import classNames from "classnames";
|
|
||||||
import { useLocal } from "@/lib/utils/use-local";
|
import { useLocal } from "@/lib/utils/use-local";
|
||||||
import { debouncedHandler } from "@/lib/utils/debounceHandler";
|
import { debouncedHandler } from "@/lib/utils/debounceHandler";
|
||||||
import { FaArrowDownLong, FaArrowUp, FaChevronUp } from "react-icons/fa6";
|
import { FaChevronUp } from "react-icons/fa6";
|
||||||
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { init_column } from "./lib/column";
|
import { init_column } from "./lib/column";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Check, Loader2, Sticker } from "lucide-react";
|
import { Loader2, Sticker } from "lucide-react";
|
||||||
import { InputSearch } from "../ui/input-search";
|
import { InputSearch } from "../ui/input-search";
|
||||||
import { Input } from "../ui/input";
|
|
||||||
import { FaChevronDown } from "react-icons/fa";
|
import { FaChevronDown } from "react-icons/fa";
|
||||||
import get from "lodash.get";
|
import get from "lodash.get";
|
||||||
import { Checkbox } from "../ui/checkbox";
|
import { Checkbox } from "../ui/checkbox";
|
||||||
|
|
@ -98,6 +78,10 @@ export const TableList: React.FC<any> = ({
|
||||||
local.data.push(row);
|
local.data.push(row);
|
||||||
local.render();
|
local.render();
|
||||||
},
|
},
|
||||||
|
selection: {
|
||||||
|
all: false,
|
||||||
|
partial: [] as any[],
|
||||||
|
},
|
||||||
renderRow: (row: any) => {
|
renderRow: (row: any) => {
|
||||||
setData((prev) => [...prev, row]);
|
setData((prev) => [...prev, row]);
|
||||||
local.data = data;
|
local.data = data;
|
||||||
|
|
@ -241,21 +225,49 @@ export const TableList: React.FC<any> = ({
|
||||||
table.getToggleAllRowsSelectedHandler();
|
table.getToggleAllRowsSelectedHandler();
|
||||||
const handler = table.getToggleAllRowsSelectedHandler();
|
const handler = table.getToggleAllRowsSelectedHandler();
|
||||||
handler(e); // Pastikan ini memanggil fungsi handler yang benar
|
handler(e); // Pastikan ini memanggil fungsi handler yang benar
|
||||||
|
local.selection.all = !local.selection.all;
|
||||||
|
local.render();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
cell: ({ row }) => (
|
cell: ({ row }) => {
|
||||||
|
const findCheck = (row: any) => {
|
||||||
|
if (row.getIsSelected()) return true;
|
||||||
|
const data: any = row.original;
|
||||||
|
const res = local.selection.partial.find((e) => e === data?.id);
|
||||||
|
return res ? true : false;
|
||||||
|
};
|
||||||
|
return (
|
||||||
<div className="px-0.5 items-center justify-center flex flex-row">
|
<div className="px-0.5 items-center justify-center flex flex-row">
|
||||||
<Checkbox
|
<Checkbox
|
||||||
id="terms"
|
id="terms"
|
||||||
checked={row.getIsSelected()}
|
checked={findCheck(row)}
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
const handler = row.getToggleSelectedHandler();
|
const handler = row.getToggleSelectedHandler();
|
||||||
handler(e); // Pastikan ini memanggil fungsi handler yang benar
|
handler(e); // Pastikan ini memanggil fungsi handler yang benar
|
||||||
|
const data: any = row.original;
|
||||||
|
const checked = local.selection.all
|
||||||
|
? true
|
||||||
|
: local.selection.partial.find((e) => e === data?.id);
|
||||||
|
if (!checked) {
|
||||||
|
local.selection.partial.push(data?.id);
|
||||||
|
} else {
|
||||||
|
if (
|
||||||
|
local.selection.partial.find((e) => e === data?.id)
|
||||||
|
) {
|
||||||
|
local.selection.partial =
|
||||||
|
local.selection.partial.filter(
|
||||||
|
(e: any) => e !== data?.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
local.selection.all = false;
|
||||||
|
}
|
||||||
|
local.render();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
),
|
);
|
||||||
|
},
|
||||||
sortable: false,
|
sortable: false,
|
||||||
},
|
},
|
||||||
...defaultColumns,
|
...defaultColumns,
|
||||||
|
|
@ -626,7 +638,6 @@ export const TableList: React.FC<any> = ({
|
||||||
e?.name ===
|
e?.name ===
|
||||||
get(ctx, "column.columnDef.accessorKey")
|
get(ctx, "column.columnDef.accessorKey")
|
||||||
);
|
);
|
||||||
console.log(head);
|
|
||||||
const renderData =
|
const renderData =
|
||||||
typeof head?.renderCell === "function"
|
typeof head?.renderCell === "function"
|
||||||
? head.renderCell(param)
|
? head.renderCell(param)
|
||||||
|
|
@ -634,7 +645,6 @@ export const TableList: React.FC<any> = ({
|
||||||
cell.column.columnDef.cell,
|
cell.column.columnDef.cell,
|
||||||
cell.getContext()
|
cell.getContext()
|
||||||
);
|
);
|
||||||
console.log(name);
|
|
||||||
return (
|
return (
|
||||||
<Table.Cell
|
<Table.Cell
|
||||||
className={cx(
|
className={cx(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue