on 2021 Dec 06 7:21 AM
onImport: function() {
that = this;
if (this.fixedDialog === undefined) {
this.fixedDialog = new sap.m.Dialog({
title: "Choose CSV File For Upload",
beginButton: new sap.m.Button({
text: "Upload",
press: function(oEvent) {
that.fixedDialog.close();
}
}),
content: [
new FileUploader("excelUploader")
],
endButton: new sap.m.Button({
text: "Cancel",
press: function() {
that.fixedDialog.close();
}
})
});
this.getView().addDependent(this.fixedDialog);
this.fixedDialog.attachBeforeClose(this.setDataToJsonFromExcel, this);
}
this.fixedDialog.open();
}
setDataToJsonFromExcel: function(oEvent) {
var oUploader = oEvent.getSource().getContent()[0];
var domRef = oUploader.getFocusDomRef();
if (domRef.files.length === 0) {
return;
}
var file = domRef.files[0];
that = this;
this.fileName = file.name;
this.fileType = file.type;
var reader = new FileReader();
reader.onload = function(e) {
var arrCSV = e.currentTarget.result.match(/[\w .]+(?=,?)/g);
var noOfCol = 3;
var headerRow = arrCSV.splice(0, noOfCol);
var data = [];
while (arrCSV.length > 0) {
var record = {};
var excelData = arrCSV.splice(0, noOfCol);
for (var i = 0; i < excelData.length; i++) {
record[headerRow[i]] = excelData[i].trim();
}
data.push(record);
}
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(data);
var oTable = that.byId("idTable");
oTable.setModel(oModel);
};
reader.readAsBinaryString(file);
oUploader.clear();
}<br>
<content>
<Button text="Import" press="onImport" />
<Table id="idTable" items="{/}">
<columns>
<Column>
<Label text="Name" />
</Column>
<Column>
<Label text="Age" />
</Column>
<Column>
<Label text="Gender" />
</Column>
</columns>
<ColumnListItem>
<cells>
<Input value="{Name}" />
<Input value="{Age}" />
<Input value="{Gender}" />
</cells>
</ColumnListItem>
</Table>
</content><br>
Adding First Data
Adding Second data.
Requirement: Second data to get added below first data.
As you can see table gets replaced with new data. But I want to keep my old data.
You can find the entire source code here on my Github
Thank You.
Request clarification before answering.
Hi,
set the data array as a attribute of the class:
aData: [], // new Class Attribute
setDataToJsonFromExcel: function(oEvent) {
...
var reader = new FileReader();
var data = this.aData; // set reference to class Attribute
reader.onload = function(e) {
...
//var data = [];
while (arrCSV.length > 0) {
var record = {};
var excelData = arrCSV.splice(0, noOfCol);
for (var i = 0; i < excelData.length; i++) {
record[headerRow[i]] = excelData[i].trim();
}
data.push(record);
}
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(data);
var oTable = that.byId("idTable");
oTable.setModel(oModel);
};
reader.readAsBinaryString(file);
oUploader.clear();
}
kind regards
Thorsten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
30 | |
9 | |
8 | |
8 | |
7 | |
7 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.