Table Export
A button group above the table triggers CSV, Excel, or PDF downloads. The library reads the table's headers, body rows, and checkbox state automatically
|
|
Reference | Customer | Amount | Actions |
|---|---|---|---|---|
|
|
128 | حسن المختار | 240 | |
|
|
129 | نادية الخطيب | 1250 | |
|
|
130 | طارق السديري | 85 | |
|
|
131 | زهرة الحبيب | 3120 |
Generic Source
Card lists, definition lists, and any custom container become exportable by tagging their rows and fields with two data attributes
Built-in Features
Every source exports to CSV, Excel, or PDF from the same buttons. CSV ships with a UTF-8 BOM and RFC 4180 quoting, Excel as an HTML payload with the correct MIME, and PDF through the browser's native print dialog.
Works on any <table> or .nds-table with zero markup changes. Card lists, definition lists, and custom containers opt in by adding data-export-rows and data-export-field.
If any row carries data-state="selected" or contains a checked input.nds-check, only that subset is exported. Otherwise the full set ships. No wiring required.
Paginated tables export every page, not just the visible one. The library walks through hidden rows inside .nds-paged-content so the file always matches the full dataset. A table's own rows only: detail rows (nds-sub) and any table nested inside one are skipped, so an expanded row never leaks its inner table into the file.
Cells starting with =, +, -, or @ are auto-prefixed with an apostrophe so spreadsheets treat them as text instead of evaluating them as formulas.
The PDF path renders a clean table into a hidden iframe and triggers the browser's print dialog. Output uses light-mode tokens for legibility on paper, with header repeat across pages and proper page breaks.
Arabic and other non-Latin scripts round-trip cleanly in CSV thanks to the UTF-8 BOM. Excel and PDF outputs honor the page's text direction so right-to-left columns stay right-to-left.
Rows hidden by NDS.Filter (data-filtered attribute) are excluded from the export. This is distinct from pagination-hidden rows, which are always included. Export reflects the active filter without any extra wiring.
Drop a data-export button anywhere on the page and it works through a single delegated listener. The full NDS.Export namespace is also available for triggering exports from custom JS.
Usage Guidelines
Best Practices
- Reach for the declarative attribute pattern first: a
[data-export]button withdata-export-targetneeds no per-page JS and works for dynamically injected toolbars too. - Use the same component for non-table lists. Tag the container with
data-export-rows="<selector>"and each field inside a row withdata-export-field="key". Cards and definition lists become exportable without a new component. - Place currency, units, and other column-wide qualifiers in the header label via
data-export-label="Amount (SAR)", not in every cell. CSV and Excel can then do math on the column. - Add
data-export-skipon per-row action columns (Edit, Delete, View buttons) so they don't leak into the file. The auto-detected checkbox column is already skipped. - Exports follow the visible table. A column hidden through the Table column menu stamps its own
data-export-skip, so it drops out of the file and returns when the column is shown again. An authoreddata-export-skipalways survives. A table with anidremembers its hidden columns across visits, so a returning user's export matches the view they left behind. - Set
data-export-nameon the source for a meaningful filename. Output is{name}-YYYY-MM-DD.{ext}. - If a cell shows a formatted value but the spreadsheet needs the raw one, set
data-export-valueon the<td>. The display stays the same, the export gets the raw number or ISO date. - Selection just works: drop the canonical
nds-card-checkboxor.nds-checkmarkup, and any checked row is exported automatically whenscopeis'auto'. - For tables that use a header column for the visible label and a separate value (sortable status enums, currency cells), do not rely on
data-sort-valuefor the export. Sort and export are decoupled by design. - Don't pre-format numbers inside cells if you want spreadsheet math. Use
data-export-valuewith the raw number so the export becomes summable. - For pure source-of-truth control over the file, the programmatic API (
NDS.Export.collect+NDS.Export.toCSV) lets a custom script transform or filter the data before download. - CSV and Excel downloads announce the row count to screen readers automatically:
NDS.announce('Exported N rows')fires after the file is triggered. No extra markup is needed, but avoid suppressing the live region if you customize the download flow.
Data Attributes
| Attribute | Placement | Description |
|---|---|---|
data-export="csv|xls|pdf" | Trigger button | Marks the element as an export button and chooses the format. Required. |
data-export-target="<selector>" | Trigger button | Selector for the source. Optional: omit when the button lives inside the source. |
data-export-scope="all|selected|auto" | Trigger button | Force the export scope. Default "auto": selected rows if any, otherwise all. |
data-export-name | Source element | Filename stem. Result: {name}-YYYY-MM-DD.{ext}. Default "nds-export". |
data-export-rows="<selector>" | Non-table source | Selector that matches each row inside the source. Required to opt a non-table container into the generic adapter. |
data-export-field="key" | Element inside a row | Marks an element as a field. The set of keys across all rows becomes the export columns. |
data-export-label | <th> or field element | Override the column heading used in the file (e.g. "Amount (SAR)" for a raw number column). |
data-export-value | <td> or field element | Override the exported cell value. Useful when the cell shows a formatted string and the file needs the raw value. |
data-export-skip | <th>, <td>, or field element | Drop the column (on a <th>) or the single cell (on a <td> / field). On the source itself, accepts a space-separated list of field keys. |
JavaScript API
The library auto-binds to [data-export] buttons via a single delegated listener. The NDS.Export namespace is also available for triggering exports from custom scripts, building one-off formats, or accessing the normalized data.