[英]Show csv data in lightning component
I have been trying to show CSV data in the lightning component but it is not showing as expected I need to show it in a table format.我一直试图在闪电组件中显示 CSV 数据,但它没有按预期显示我需要以表格格式显示它。 Table format
表格格式
The component is displaying data but not in table format as it is in CSV file.该组件正在显示数据,但不是 CSV 文件中的表格格式。
Component成分
<aura:attribute name="showcard" type="boolean" default="false"/>
<aura:attribute name="tabledata" type="object[]" />
<aura:attribute name="header" type="object[]" />
<div>
<lightning:input aura:id="file"
onchange="{!c.showcsvdata}"
type="file"
name="file"
accept=".csv"
multiple="false"/>
</div>
<aura:if isTrue = "{!v.showcard}">
<lightning:button label="Create Accounts" title="Neutral action"
onclick="{! c.Insertrecord }"/>
<table class="slds-table slds-table_cell-buffer slds-table_bordered">
<thead>
<tr class="slds-line-height_reset">
<aura:iteration items="{!v.header}" var="head" >
<th class="" scope="col">
<div class="slds-truncate" title="{!head}">{!head}
</div>
</th>
</aura:iteration>
</tr>
</thead>
<tr class="slds-hint-parent">
<aura:iteration items="{!v.tabledata}" var="tab">
<td >
<div class="slds-truncate" title="{!tab}">{!tab}</div>
</td>
</aura:iteration>
</tr>
</table>
</aura:if>
controller控制器
showcsvdata : function (component, event, helper){
var fileInput = component.find("file").get("v.files");
var file = fileInput[0];
console.log(file);
if (file) {
component.set("v.showcard", true);
var tabledata=[];
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
var csv = evt.target.result;
var rows = csv.split("\n");
console.log(rows[0]);
var trimrow=rows[0].split(",");
// alert(trimrow.length);
component.set("v.header",trimrow);
console.log(JSON.stringify(component.get("v.header")))
for (var i = 1; i < rows.length; i++) {
var cells = rows[i].split(",");
if(cells.length!=1){
for(var j=0; j < cells.length; j++){
// var cel=cells.split("\n");
tabledata.push(cells[j]);
component.set("v.tabledata",tabledata);
}
}
}
console.log('@@@@dynamic'+JSON.stringify(component.get("v.tabledata")));
}
}
this is show data like这是显示数据,如
It is showing data like this table data in single row.它在单行中显示类似于此表数据的数据。 I need to show it as a table
我需要将它显示为表格
Make the following changes to your code, it should work then.对您的代码进行以下更改,它应该可以工作。
Component成分
<aura:iteration items="{!v.tabledata}" var="row">
<tr class="slds-hint-parent">
<aura:iteration items="{!row}" var="tab">
<td >
<div class="slds-truncate" title="{!tab}">{!tab}</div>
</td>
</aura:iteration>
</tr>
</aura:iteration>
Controller控制器
for (var i = 1; i < rows.length; i++) {
var cells = rows[i].split(",");
var rowData=[];
if(cells.length!=1){
for(var j=0; j < cells.length; j++){
rowData.push(cells[j]);
}
tabledata.push(rowData);
}
}
component.set("v.tabledata",tabledata);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.