Export Plugin
The Export plugin lets users download grid data as CSV, JSON, or other formats with a single click or API call. Great for reporting, data backup, or letting users work with data in Excel.
Installation
Section titled “Installation”import '@toolbox-web/grid/features/export';Basic Usage
Section titled “Basic Usage”Enable the feature and call exportCsv() or exportJson() when you’re ready to download. You can also combine it with the selection feature to export only selected rows.
import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');grid.gridConfig = { columns: [ { field: 'name', header: 'Name' }, { field: 'email', header: 'Email' }, { field: 'department', header: 'Department' } ], features: { export: { fileName: 'employees', includeHeaders: true, }, },};
// Trigger export via button clickdocument.getElementById('export-btn').addEventListener('click', () => { const plugin = grid.getPluginByName('export'); plugin.exportCsv();});import '@toolbox-web/grid-react/features/export';import { DataGrid, useGrid } from '@toolbox-web/grid-react';
function EmployeeGrid({ data }) { const { ref, element } = useGrid();
const handleExport = () => { element?.getPluginByName('export')?.exportCsv(); };
return ( <> <button onClick={handleExport}>Export CSV</button> <DataGrid ref={ref} rows={data} columns={[ { field: 'name', header: 'Name' }, { field: 'email', header: 'Email' }, { field: 'department', header: 'Department' }, ]} export={{ fileName: 'employees', includeHeaders: true }} /> </> );}<script setup>import '@toolbox-web/grid-vue/features/export';import { TbwGrid, TbwGridColumn } from '@toolbox-web/grid-vue';import { ref } from 'vue';
const data = [ { name: 'Alice', email: 'alice@example.com', department: 'Engineering' }, { name: 'Bob', email: 'bob@example.com', department: 'Marketing' },];
const gridRef = ref(null);
const handleExport = () => { gridRef.value?.element?.getPluginByName('export')?.exportCsv();};</script>
<template> <div> <button @click="handleExport">Export CSV</button> <TbwGrid ref="gridRef" :rows="data" :export="{ fileName: 'employees', includeHeaders: true }">
<TbwGridColumn field="name" header="Name" /> <TbwGridColumn field="email" header="Email" /> <TbwGridColumn field="department" header="Department" /> </TbwGrid> </div></template>// Feature import - enables the [exportFeature] inputimport '@toolbox-web/grid-angular/features/export';
import { Component, viewChild, ElementRef } from '@angular/core';import { Grid } from '@toolbox-web/grid-angular';import type { ColumnConfig, GridElement } from '@toolbox-web/grid';
@Component({ selector: 'app-data-grid', imports: [Grid], template: ` <button (click)="handleExport()">Export CSV</button> <tbw-grid #grid [rows]="rows" [columns]="columns" [exportFeature]="{ fileName: 'employees', includeHeaders: true }" style="height: 400px; display: block;"> </tbw-grid> `,})export class DataGridComponent { gridRef = viewChild<ElementRef<GridElement>>('grid'); rows = [];
columns: ColumnConfig[] = [ { field: 'name', header: 'Name' }, { field: 'email', header: 'Email' }, { field: 'department', header: 'Department' }, ];
handleExport() { const plugin = this.gridRef()?.nativeElement.getPluginByName('export'); plugin?.exportCsv(); }}Default Export
Section titled “Default Export”EXPORT
Include headers Include column headers in export
Visible only Export only visible columns
Selected only Export only selected rows
import '@toolbox-web/grid';import { queryGrid } from '@toolbox-web/grid';import '@toolbox-web/grid/features/export';import '@toolbox-web/grid/features/selection';
const sampleData = [ { id: 1, name: 'Alice Johnson', department: 'Engineering', salary: 95000, startDate: '2023-01-15' }, { id: 2, name: 'Bob Smith', department: 'Marketing', salary: 75000, startDate: '2022-06-20' }, { id: 3, name: 'Carol Williams', department: 'Engineering', salary: 105000, startDate: '2021-03-10' }, { id: 4, name: 'Dan Brown', department: 'Sales', salary: 85000, startDate: '2023-08-05' },];const columns = [ { field: 'id', header: 'ID', type: 'number' }, { field: 'name', header: 'Name' }, { field: 'department', header: 'Department' }, { field: 'salary', header: 'Salary', type: 'number' }, { field: 'startDate', header: 'Start Date' },];
const grid = queryGrid('tbw-grid')!;function rebuild(includeHeaders = true, onlyVisible = true, onlySelected = false) { grid.gridConfig = { columns, features: { selection: 'range', export: { includeHeaders, onlyVisible, onlySelected, fileName: 'grid-export' } }, }; grid.rows = sampleData;}
rebuild();
document.querySelector('.export-csv')?.addEventListener('click', () => grid.getPluginByName('export')?.exportCsv());document.querySelector('.export-excel')?.addEventListener('click', () => grid.getPluginByName('export')?.exportExcel());document.querySelector('.export-json')?.addEventListener('click', () => grid.getPluginByName('export')?.exportJson());<div class="export-buttons" style="padding: 8px; display: flex; gap: 8px; flex-wrap: wrap; border-bottom: 1px solid var(--sl-color-gray-5);"> <button class="export-csv" style="padding: 6px 12px; cursor: pointer;">Export CSV</button> <button class="export-excel" style="padding: 6px 12px; cursor: pointer;">Export Excel</button> <button class="export-json" style="padding: 6px 12px; cursor: pointer;">Export JSON</button></div><tbw-grid style="height: 350px;"></tbw-grid>Export all visible data to CSV. Toggle “Selected only” and select some rows to export just those.
Configuration Options
Section titled “Configuration Options”| Option | Type | Default | Description |
|---|---|---|---|
fileName | string | 'export' | Base filename (without extension) |
includeHeaders | boolean | true | Include column headers in export |
onlyVisible | boolean | true | Export only visible columns |
onlySelected | boolean | false | Export only selected rows (requires SelectionPlugin) |
Programmatic API
Section titled “Programmatic API”The ExportPlugin provides methods for exporting data with fine-grained control over which columns and rows to include. Like the ClipboardPlugin, it accepts columns and rowIndices parameters so you can export exactly the data you need.
Basic Export
Section titled “Basic Export”const exportPlugin = grid.getPluginByName('export');
// Export all visible data to CSVexportPlugin.exportCsv();
// Export to Excel XMLexportPlugin.exportExcel();
// Export to JSONexportPlugin.exportJson();Export with Column/Row Control (ExportParams)
Section titled “Export with Column/Row Control (ExportParams)”const exportPlugin = grid.getPluginByName('export');
// Export specific columnsexportPlugin.exportCsv({ columns: ['name', 'email', 'department'], fileName: 'contacts', includeHeaders: true,});
// Export specific rowsexportPlugin.exportCsv({ rowIndices: [0, 3, 7], fileName: 'selected-employees',});
// Export specific columns from specific rowsexportPlugin.exportExcel({ columns: ['name', 'salary'], rowIndices: [0, 1, 2], fileName: 'salary-report',});ExportParams Reference
Section titled “ExportParams Reference”| Option | Type | Default | Description |
|---|---|---|---|
fileName | string | config value | File name (without extension) |
columns | string[] | - | Specific column fields to export |
rowIndices | number[] | - | Specific row indices to export |
includeHeaders | boolean | config value | Include column headers in export |
processCell | (value, field, row) => any | - | Custom cell value processor |
processHeader | (header, field) => string | - | Custom header processor |
Events
Section titled “Events” Event Log:
| Event | Detail | Description |
|---|---|---|
export-complete | { format, fileName, rowCount, columnCount } | Fired after an export completes |
See Also
Section titled “See Also”
AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt