/utils/csvDownload

The csvDownload module exports a default method to turn an array into a comma separated text file.

Methods

(inner) csvDownload(data, paramsopt)

The function turns an array of records into a text blob. The blob is transfomed to an ObjectURL which can be referenced in an anchor [link] tag. The hidden tag element is appended to the document.body and programmatically clicked to download the ObjectURL as a text file.

Parameters:
NameTypeAttributesDescription
dataarray

An array of records for the csv text file.

paramsObject<optional>

The parameters object.

Properties
NameTypeAttributesDefaultDescription
params.separatorString<optional>
','

The seperator used in the csv text file.

params.typeString<optional>
'text/csv;charset=utf-8;'

The type for the csv text file includes the charset required for character support.

params.joinString<optional>
'\r\n'

The characters used to join the row strings. The default being [carriage] return \r and [new] line feed \n.

params.fileString<optional>
'file'

The file name for the download generated from clicking the ObjectURL link.

params.headerBoolean<optional>

Whether a header should be added to the CSV file.

params.fieldsArray<optional>

An array of field objects for the fieldsFunction and header row.

(inner) fieldsFunction(record, fields) → {Array}

The fieldsFunction method receives a record obect and the fields array param. The method iterated through the fields array and formats the record [field] property according the fields params object.

Parameters:
NameTypeDescription
recordObject

A record object represents the data for a CSV row.

fieldsArray

An array of field params objects.

Returns:

Returns an array of field string values to be merged joined as a CSV row.

Type: 
Array