-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from sadmann7/update-table
feat: update data-table
- Loading branch information
Showing
8 changed files
with
150 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { type Table } from "@tanstack/react-table" | ||
|
||
export function exportTableToCSV<TData>( | ||
/** | ||
* The table to export. | ||
* @type Table<TData> | ||
*/ | ||
table: Table<TData>, | ||
opts: { | ||
/** | ||
* The filename for the CSV file. | ||
* @default "table" | ||
* @example "tasks" | ||
*/ | ||
filename?: string | ||
/** | ||
* The columns to exclude from the CSV file. | ||
* @default [] | ||
* @example ["select", "actions"] | ||
*/ | ||
excludeColumns?: (keyof TData | "select" | "actions")[] | ||
} = {} | ||
): void { | ||
const { filename = "table", excludeColumns = [] } = opts | ||
|
||
// Retrieve headers (column names) | ||
const headers = table | ||
.getAllLeafColumns() | ||
.map((column) => column.id) | ||
.filter((id) => !excludeColumns.includes(id)) | ||
|
||
// Build CSV content | ||
const csvContent = [ | ||
headers.join(","), | ||
...table.getRowModel().rows.map((row) => | ||
headers | ||
.map((header) => { | ||
const cellValue = row.getValue(header) | ||
// Handle values that might contain commas or newlines | ||
return typeof cellValue === "string" | ||
? `"${cellValue.replace(/"/g, '""')}"` | ||
: cellValue | ||
}) | ||
.join(",") | ||
), | ||
].join("\n") | ||
|
||
// Create a Blob with CSV content | ||
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" }) | ||
|
||
// Create a link and trigger the download | ||
const url = URL.createObjectURL(blob) | ||
const link = document.createElement("a") | ||
link.setAttribute("href", url) | ||
link.setAttribute("download", `${filename}.csv`) | ||
link.style.visibility = "hidden" | ||
document.body.appendChild(link) | ||
link.click() | ||
document.body.removeChild(link) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters