JSON and XML export, text tables
The api that withExport(api) extends also gets JSON, XML and text-table methods besides CSV, Excel and PDF (1.4.0). Installation and withExport are the same as in CSV, Excel and PDF export. Reading data in is covered in Import and load from a URL.
ts
import { withExport } from '@maxgrid/export';
const ex = withExport(api);
const json = ex.exportJson({ pretty: true });
const xml = ex.exportXml({ rowTag: 'person' });
const table = ex.getTextTable({ header: true });
await ex.downloadJson('people.json');Methods
| Method | Returns |
|---|---|
exportJson(options?) | JSON text: an array of objects keyed by column id |
downloadJson(filename?, options?) | Saves a JSON file (browser); default name export.json |
exportXml(options?) | XML text |
downloadXml(filename?, options?) | Saves an XML file (browser); default name export.xml |
getTextTable(options?) | A 2-D array of strings (string[][]) |
Common options
| Option | Value | Default | Effect |
|---|---|---|---|
scope | 'visible' · 'all' · 'selected' | 'visible' | Row range; visible rows follow sort and filter, selected rows keep screen order |
includeHidden | boolean | false | Also includes hidden columns |
columns | array of column ids | visible ones | Columns and their order; an unknown id is an invalid-option |
text | 'value' · 'shown' | 'value' for JSON and XML, 'shown' for the text table | value is the stored value, shown the displayed text (formatValue, optionLabel) |
sanitizeFormulas | boolean | false | Puts ' in front of text that starts with =, +, -, @ or a tab |
locale | 'ko' · 'en' … | grid locale | Language of error messages |
Rows that are only marked as removed (removeMode: 'mark') are left out, as in CSV. A multiselect (array) value stays an array with text: 'value' and is the item labels joined by the separator with shown.
JSON
| Option | Value | Default | Effect |
|---|---|---|---|
pretty | boolean | false | Indents the output |
space | integer 1-8 | 2 | Indent width when pretty |
A missing value is null. A date is the text stored in the column. Without rows the result is [].
XML
xml
<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row>
<id>1</id>
<name>Kim & team</name>
<tags>
<item>a</item>
</tags>
<joined nil="true"/>
</row>
</rows>| Rule | Content |
|---|---|
| Declaration, root | UTF-8 declaration, root <rows> (root), one <row> per row (rowTag); an invalid name is an invalid-option |
| Column element | The column id when it is a valid XML name, otherwise <cell id="…"> |
| Values | Numbers via String, dates as ISO, booleans as true/false, arrays as one <item> each, missing values as nil="true" |
| Escaping | & < > " ' become entities; control characters XML cannot hold and lone surrogates are dropped |
pretty | Default true; false gives one line |
Text table
getTextTable({ header: true }) returns the displayed text as a 2-D array, handy for reports or other tools. A missing value is an empty string.
Gallery example: export-json
Gallery example: export-xml