Overview
Export adds download buttons to a list. A .nds-export button group holds one button per format. Each button names its source, the list it exports, through data-export-target. A table exports as it is. Any other list marks its rows with data-export-rows and its fields with data-export-field. Your code can run the same export, or read the data first, through NDS.Export.
Pick another component when:
- users pick the rows that an action changes: Selection
- users hide or sort the columns on the screen: Tables
- users send a file to your server: File Upload
Markup
|
|
Reference | Customer | Amount | Actions |
|---|---|---|---|---|
|
|
128 | حسن المختار | 240 | |
|
|
129 | نادية الخطيب | 1250 | |
|
|
130 | طارق السديري | -85 |
Variants
A Scope option goes on every export button of the list. The Column hide target is the toolbar end that holds the buttons of the Table structure, so the option is off on Cards.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Table (default) | — | — | A <table> or .nds-table. The script reads its header and body rows as they are |
| Structure | Cards | canon #export-cards |
— | Any list that is not a table: cards, a definition list. Mark the rows with data-export-rows and each field with data-export-field |
| Scope | Auto (default) (hint: Selected rows if any are selected, else every row) | — | — | The selected rows when any row is selected, else every row |
| Scope | Selected | [data-export-scope="selected"] |
[data-export] |
Only the selected rows. With nothing selected, the file has only the header |
| Scope | All | [data-export-scope="all"] |
[data-export] |
Every row, selected or not |
| Column hide | Column hide (hint: Adds a Columns menu, and hidden columns stay out of the file) | canon #export-columns |
.nds-toolbar-end:has([data-export-target="#export-orders"]) (start) |
The Tables column menu. A column the user hides drops out of the file |
Behavior
Table Source
A <table> or .nds-table needs no export attributes. The header cells become the columns, and the body rows become the rows. The checkbox column is left out on its own. A detail row (tr.nds-sub) and any table inside it are left out too.
List Source
Use it for cards or any list that is not a table. Give the list data-export-rows with a selector for its rows. Mark each field in a row with data-export-field and a key. Each new key becomes a column, in the order the script first finds it. A row without a key leaves that cell empty.
Export Scope
By default, the file holds the selected rows when any row is selected, else every row. A row is selected when it holds a checked input.nds-checkbox-input or carries data-state="selected", the same rule as Selection. Set data-export-scope="selected" or "all" on the buttons to fix the scope. Every format follows the scope. With nothing selected, the Selected scope exports only the header row.
Server Pages
When your server sends one page of rows at a time, the page holds only those rows, and so does the file. The script cannot get the other pages, and nothing on the screen says the file is short. Do one of these: hide the export buttons, give them a label that says they export this page, or offer your server’s own export of every row in their place.
Hidden Columns
When a user hides a column with the Tables column menu, the column drops out of the file. Put the menu in the toolbar’s .nds-toolbar-end, before the export group, and give its data-columns-target the table’s id. Tables sets data-export-skip on its header cell, and removes it when the column shows again. A data-export-skip that you write stays. A table with an id keeps its hidden columns for the next visit, so the next export leaves them out too.
Built-in Features
-
Multi-Format Output
One source exports to CSV, Excel or PDF. CSV quotes a value that holds a comma, a quote or a line break. Excel gets an HTML table that it opens as a sheet.
-
Pagination Aware
A paged list exports every page in the HTML, not only the page on the screen. Rows a server has not sent are not in the file: see Server Pages.
-
Filter Aware
Rows that a Filter hides are left out, so the file matches the active filter.
-
Formula Guard
A value that starts with
=,+,-,@, a tab or a line break gets a leading apostrophe. A spreadsheet then reads it as text, not as a formula. A plain negative number stays a number. -
Print-Ready PDF
PDF opens the browser's print dialog on a plain table of the data, even for a card list. The user saves it as a PDF. The table uses the light-mode table colors, repeats the header row on each page, and never splits a row. Focus goes back to the button when the dialog closes.
-
Arabic in Every Format
CSV and Excel files start with a UTF-8 byte order mark, so a spreadsheet shows Arabic text correctly. Excel and PDF use the page's language and text direction.
-
Dated File Names
The file is named
{name}-YYYY-MM-DD, with today in the site's timezone (data-timezone), or the visitor's day without it. The script removes characters that a file system does not allow, and cuts the name to 64 characters. -
Screen Reader Notice
After a CSV or Excel download, screen readers announce how many rows the file holds.
-
Auto-initialization
The script loads when the page has a
data-exportbutton. One listener on the document serves every button, including buttons added later.
Best Practices
- Put the export group in the
.nds-toolbar-endof a Toolbar above the list. - Give every source a
data-export-name. Without it, every file name starts withnds-export. - Write a unit or a currency once, in the column label:
data-export-label="Amount (SAR)". Keep the cells to raw numbers, so a spreadsheet can add them up. - Give a cell
data-export-valuewhen it shows a formatted value: a currency, a local date. Put the raw number or the ISO date in it. - Add
data-export-skipto the header cell of an action column (View, Edit, Delete). - Do not use
data-sort-valuefor the file. Export never reads it. - Keep the Selection counter in the toolbar with the export group. The count tells users what the file will hold.
- Use
NDS.Export.collect()when your code must change the data before the download.
API
Data Attributes
A button with no data-export-target exports the table or [data-export-rows] list that holds it.
| Attribute | Element | Effect |
|---|---|---|
data-export="csv", "xls" or "pdf" |
a button | Makes the button export, in that format |
data-export-target="selector" |
a button | Names the source with a CSS selector |
data-export-scope |
a button | "selected" exports only the selected rows, and "all" exports every row. The default is "auto": see Export Scope above |
data-export-name |
a source | The file name, before the date. The default is nds-export. On a button it does nothing |
data-export-rows="selector" |
a list that is not a table | Selects its rows. Without it, the rows are the list’s direct children |
data-export-skip="keys" |
a list that is not a table | Leaves out the fields with these keys. Separate the keys with spaces or commas |
data-export-label |
a <th> |
The column name in the file. The default is the header text |
data-export-skip |
a <th> |
Leaves out the column. Tables also sets it while a user hides the column. It has no effect on a <td> |
data-export-field="key" |
an element in a row | Makes the element a field: its text goes in the column with this key |
data-export-label |
an element with data-export-field |
The column name in the file. The default is the key. Write it once per key: the first one wins |
data-export-skip |
an element with data-export-field |
Leaves out this one field |
data-export-value |
a <td>, or an element with data-export-field |
The value in the file, in place of the text on the screen |
data-state~="selected" |
a row | Marks the row as selected when it has no checkbox. Selection sets it on rows with a checked box |
JavaScript
The NDS.Export script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
A source is an element or a CSS selector. A scope is "auto" (the default), "selected" or "all".
| Method | Effect |
|---|---|
NDS.Export.init() |
Adds the click listener for data-export buttons. The loader calls it |
NDS.Export.export(source, format, scope, options) |
Exports the source. format is "csv", "xls" or "pdf". If Selection has not loaded yet, it waits for it and returns a promise, so the selected rows are right |
NDS.Export.exportCsv(source, scope, options) |
Exports the source as CSV. exportXls() and exportPdf() work the same way |
NDS.Export.collect(source, scope) |
Returns { columns, rows } and downloads nothing. columns holds { key, label } items. Each row maps a key to its value. It cannot wait for Selection: call it after Selection loads, or it sees no selected row |
NDS.Export.download(data, format, options) |
Downloads a collect() result, or opens the print dialog for "pdf" |
NDS.Export.toCsv(data) |
Returns a collect() result as CSV text, with a UTF-8 byte order mark at the start |
NDS.Export.toXlsHtml(data, options) |
Returns a collect() result as the HTML that Excel opens |
NDS.Export.openPrint(data, options) |
Opens the print dialog on a collect() result |
| Option | Default | Effect |
|---|---|---|
filename |
{name}-YYYY-MM-DD.{ext} |
The whole file name, with no date added. In the default, {name} is the source’s data-export-name, or nds-export for download() |
title |
the file name without its extension | The heading of the printed PDF, and the name the browser offers when the user saves it |
dir |
the page’s direction | The text direction of the Excel file and the printed PDF: "rtl" or "ltr" |
| Event | Fired on | Detail |
|---|---|---|
nds:export:start |
the source (bubbles, cancelable) | { format, scope, data, filename }. It fires before the file is made. A button, export(), exportCsv(), exportXls() and exportPdf() fire it, and download() does not. Change data.rows to change the file. Call preventDefault() to stop the export |
A table column’s key is c plus its position, starting at 0. The checkbox column counts, so the Amount column above is c3. A list’s keys are its data-export-field values. The full API is in the banner of _js/nds-export.js.