IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

Commit 66ace37b authored by APalumbo97's avatar APalumbo97
Browse files

Initial code for exporting a template CSV with headers.

parent b4e6a382
<template>
<lightning:layout horizontalalign="space">
<lightning:layoutItem size="10">
<lightning-combobox
name="datasource"
label="Export a template CSV"
value={value}
placeholder={value}
options={combo_options}
onchange={handleChange}
></lightning-combobox>
</lightning:layoutItem>
<br/>
<lightning:layoutItem size="2">
<lightning-button variant="success" label="Download" onclick={download}></lightning-button>
</lightning:layoutItem>
</lightning:layout>
</template>
import { LightningElement, track } from 'lwc';
import { assetDefinitions } from 'c/controllers';
export default class ExportHeaders extends LightningElement {
@track value = 'Select an asset type...';
@track valueID = null;
@track placeholder = 'N/A';
@track combo_options = [];
/**
* Sets the combobox options.
*/
connectedCallback() {
var i;
assetDefinitions.fetchAssetTypes()
.then(data => {
var temp_options = [];
for (i = 0; i < data.rows.length; i++) {
temp_options.push({
'label': data.rows[i]['name'] + ': ' + data.rows[i]['id'],
'value': data.rows[i]['name'] + ': ' + data.rows[i]['id']
});
}
this.combo_options = temp_options;
})
.catch(e => {
console.log('Exception: ', e);
});
}
/**
* Event handler for when something is selected from the combobox.
* @param {*} event - the event object
*/
handleChange(event) {
this.value = event.detail.value;
this.valueID = event.detail.value.split(':')[1].trimLeft();
}
/**
* Event handler for when the download button is pressed.
*/
download() {
var i;
var csv_data = '';
var rows = [];
var hiddenElement;
assetDefinitions.fetchAssetPropTypes(this.valueID)
.then(properties => {
rows = ['asset_id'];
for (i = 0; i < properties.rows.length; i++) {
rows.push(properties.rows[i]['name']);
}
csv_data += rows.join(',') + '\n';
// Creates the CSV file and downloads it.
hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv_data);
hiddenElement.target = '_blank';
hiddenElement.download = this.value + '_template.csv';
hiddenElement.click();
})
.catch(e => {
console.log('Exception: ', e);
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>47.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
...@@ -7,5 +7,5 @@ ...@@ -7,5 +7,5 @@
], ],
"namespace": "", "namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com", "sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "48.0" "sourceApiVersion": "47.0"
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment