Skip to main content
Login

Sign in to access your account

Export - National Design System

Export turns a table, a card list or any marked-up list into a CSV file, an Excel file or a printed PDF

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

Preview
0 selected of 3 orders
Reference Customer Amount Actions
128 حسن المختار 240
129 نادية الخطيب 1250
130 طارق السديري -85
<div class="nds-toolbar"> <div class="nds-toolbar-row"> <div class="nds-toolbar-start"> <span class="nds-toolbar-text" data-selection-target="export-orders-body"> <b data-selection-count>0</b> selected of <b data-selection-total>3</b> orders </span> </div> <div class="nds-toolbar-end"> <div class="nds-export nds-btn-group"> <button type="button" class="nds-btn nds-secondary-outline nds-md" data-export="csv" data-export-target="#export-orders"> <span class="nds-label">CSV</span> </button> <button type="button" class="nds-btn nds-secondary-outline nds-md" data-export="xls" data-export-target="#export-orders"> <span class="nds-label">Excel</span> </button> <button type="button" class="nds-btn nds-secondary-outline nds-md" data-export="pdf" data-export-target="#export-orders"> <span class="nds-label">PDF</span> </button> </div> </div> </div> </div> <table id="export-orders" class="nds-table nds-compact" data-export-name="orders"> <thead> <tr> <th> <div class="nds-form-container nds-checkbox"> <div class="nds-form-control"> <input type="checkbox" class="nds-checkbox-input" aria-label="Select all orders"> </div> </div> </th> <th>Reference</th> <th>Customer</th> <th data-export-label="Amount (SAR)">Amount</th> <th data-export-skip>Actions</th> </tr> </thead> <tbody id="export-orders-body"> <tr> <td> <div class="nds-form-container nds-checkbox"> <div class="nds-form-control"> <input type="checkbox" class="nds-checkbox-input" aria-label="Select order 128"> </div> </div> </td> <td>128</td> <td>حسن المختار</td> <td data-export-value="240"><span class="nds-number-format" data-currency="SAR">240</span></td> <td><button type="button" class="nds-btn nds-subtle nds-sm">View</button></td> </tr> <tr> <td> <div class="nds-form-container nds-checkbox"> <div class="nds-form-control"> <input type="checkbox" class="nds-checkbox-input" aria-label="Select order 129"> </div> </div> </td> <td>129</td> <td>نادية الخطيب</td> <td data-export-value="1250"><span class="nds-number-format" data-currency="SAR">1250</span></td> <td><button type="button" class="nds-btn nds-subtle nds-sm">View</button></td> </tr> <tr> <td> <div class="nds-form-container nds-checkbox"> <div class="nds-form-control"> <input type="checkbox" class="nds-checkbox-input" aria-label="Select order 130"> </div> </div> </td> <td>130</td> <td>طارق السديري</td> <td data-export-value="-85"><span class="nds-number-format" data-currency="SAR">-85</span></td> <td><button type="button" class="nds-btn nds-subtle nds-sm">View</button></td> </tr> </tbody> </table>

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-export button. One listener on the document serves every button, including buttons added later.

Best Practices

  • Put the export group in the .nds-toolbar-end of a Toolbar above the list.
  • Give every source a data-export-name. Without it, every file name starts with nds-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-value when it shows a formatted value: a currency, a local date. Put the raw number or the ISO date in it.
  • Add data-export-skip to the header cell of an action column (View, Edit, Delete).
  • Do not use data-sort-value for 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
// Export every row, with a file name of your own NDS.Export.exportXls('#export-orders', 'all', { filename: 'orders-q3.xls' }); // Change the data before the download: leave out orders under 100 SAR var data = NDS.Export.collect('#export-orders'); data.rows = data.rows.filter(function (row) { return Number(row.c3) >= 100; }); NDS.Export.download(data, 'csv', { filename: 'large-orders.csv' });

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.

Last Modified Date: 02/11/2026 - 03:47 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks