ExportCSVButton
Reference
- The
ExportCSVButtoncomponent is a React component designed to export data as a CSV file. - This component renders a button that allows users to export data in CSV format.
- It formats the data based on the provided headers, date/time formats, and additional attributes before exporting.
- If exporting is disabled (
exportDisabled), it shows a disabled button with anExcelIcon.
Props
data: The array of objects to be exported as CSV.headers: Defines the structure of the CSV file, including keys, labels, and formatting options (e.g.,attributeType,prefix,suffix).- For a header with
attributeTypeDate,DateTimeorPicklist, always giveprefixandsuffix(use empty strings''when you need none). The cell is written asprefix + value + suffix, so a missing one shows asundefined. - The button is for a button inside your widget (for example in a drill-down popup). The report header's own Export to CSV / Export to Excel buttons use
registerExport.
- For a header with
filename: The name of the exported CSV file.attributeFields: Additional metadata for picklist fields to map display names.exportDisabled: A boolean to disable the export button.style: Custom styles for the button.
Usage Example
function Example(pageContext) {
const { protrakComponents } = React.useContext(customWidgetContext);
const { ExportCSVButton } = protrakComponents;
const {
data,
config,
showDrilldown,
onClick,
setShowDrilldown,
totalData,
filters,
typeName,
sortState,
onSortApplied,
typePluralName,
states,
} = pageContext;
const columns = [
{ key: "DELIVERED Nos", label: "DELIVERED Nos", type: "number" },
{ key: "DELIVERED Cum", label: "DELIVERED Cum", type: "number" },
{ key: "DELIVERED %", label: "DELIVERED %", type: "%" },
{ key: "Stock Nos", label: "Stock Nos", type: "number" },
{ key: "Stock Cum", label: "Stock Cum", type: "number" },
{ key: "ERECTED Nos", label: "ERECTED Nos", type: "number" },
{ key: "ERECTED Cum", label: "ERECTED Cum", type: "number" },
{ key: "ERECTED %", label: "ERECTED %", type: "%" },
{ key: "Stock At Site Nos", label: "Stock At Site Nos", type: "number" },
{ key: "Stock At Site Cum", label: "Stock At Site Cum", type: "number" },
];
return (
<ExportCSVButton
data={data}
headers={columns}
filename={typePluralName + "-" + config.name}
attributeFieldss={config.fields}
/>
);
}