Showing posts with label table. Show all posts
How to import Excel xlsx,xls file to SAP UI5 Application.

How to import Excel xlsx,xls file to SAP UI5 Application.

Sanjo Thomas•06:42:00

There can be multiple scenarios, where user would be required to import excel, csv file to UI5 application and then perform actions on selected line Items. In this blog, let us see ways to upload an excel or CSV file to UI5 application.



Normaly to do so, we have 2 ways:

1) Use external library - Sheet.js

2) Use javascript abilities - only for CSV files

Use External Library

Let's keep the use case simple, we will have one view having a FileUploader and to display the data lets bind it to a table control.

<mvc:View controllerName="com.UploadExcel.controller.MainView" xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m"
	xmlns:u="sap.ui.unified">
	<Shell id="shell">
		<App id="app">
			<pages>
				<Page id="page" title="Read From Excel">
					<customHeader>
						<Bar>
							<contentMiddle>
								<Label text="Read Data From Excel"/>
							</contentMiddle>
							<contentRight>
								<u:FileUploader id="FileUploaderId" sameFilenameAllowed="true" iconOnly="false" buttonOnly="true" fileType="XLSX,xlsx"
									icon="sap-icon://upload" iconFirst="true" style="Emphasized" change="onUpload"/>
							</contentRight>
						</Bar>
					</customHeader>
					<content>
						<Table items="{localModel>/items}">
							<columns>
								<Column>
									<Label text="Name"/>
								</Column>
								<Column>
									<Label text="Age"/>
								</Column>
								<Column>
									<Label text="Job"/>
								</Column>
								<Column>
									<Label text="Address"/>
								</Column>
							</columns>
							<items>
								<ColumnListItem>
									<cells>
										<Text text="{localModel>Name}"/>
										<Text text="{localModel>Age}"/>
										<Text text="{localModel>Job}"/>
										<Text text="{localModel>Address}"/>
									</cells>
								</ColumnListItem>
							</items>
						</Table>
					</content>
				</Page>
			</pages>
		</App>
	</Shell>
</mvc:View>


Now, update the code in onUpload function in the respective controller.



		onUpload: function (e) {
			this._import(e.getParameter("files") && e.getParameter("files")[0]);
		},

		_import: function (file) {
			var that = this;
			var excelData = {};
			if (file && window.FileReader) {
				var reader = new FileReader();
				reader.onload = function (e) {
					var data = e.target.result;
					var workbook = XLSX.read(data, {
						type: 'binary'
					});
					workbook.SheetNames.forEach(function (sheetName) {
						// Here is your object for every sheet in workbook
						excelData = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);

					});
					// Setting the data to the local model 
					that.localModel.setData({
						items: excelData
					});
					that.localModel.refresh(true);
				};
				reader.onerror = function (ex) {
					console.log(ex);
				};
				reader.readAsBinaryString(file);
			}
		}

Our main part of the code is done, we need to add the external library reference in the component.js file.


var jQueryScript = document.createElement('script'); jQueryScript.setAttribute('src', 'https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.10.0/jszip.js'); document.head.appendChild(jQueryScript); var jQueryScript = document.createElement('script'); jQueryScript.setAttribute('src', 'https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.10.0/xlsx.js'); document.head.appendChild(jQueryScript);

Now, click on the browse button and select an excel file, and click on open. After selection the data is shown in the table.


Hope, this was easy to understand. Now in the next blog let us see how to import a CSV file to UI5 application.

We can also see how we can validate the excel file using the header names in the next blog.

Read more
How to get Selected table index value?

How to get Selected table index value?

Sanjo Thomas•05:14:00


In this particular blog, I will show how to get the index of the selected row. For this I will create a sample application named Live_Img, which I have already created.

I will show you initially how to define a table and then how to bind data within the table. Once the table is populated, then I will show how to get the index of the selected row item from the table.

Let’s then start with the project. First create a project named Live_Img(this is in my case). Here’s my output screen.


Maintain the code in index.html file and component.js file. I hope you know how to maintain these files, if not then see the documentation on project structuring from sapui5tutors blog. Now, I will mention the routing part in the manifest.json file.
Routing in manifest.json
"routing": {

"config": {
"routerClass": "sap.m.routing.Router",
"viewType": "XML",
"viewPath": "sap.ui.live_img.view",
"targetAggregation": "pages"
},
"routes": [{
"pattern": "",
"name": "info1",
"view": "info1",
"targetAggregation": "pages",
"controlId": "id1"
}]
}
Now, maintain the code in info1.view.xml. In this file I have used the sap.m.Table control. And used json model for binding the data in table.

Info1.view.xml
<Table items="{json>/Content1}" id="tab1" visible="true" inset="true"   >
<columns>
<Column >
<Text text="Product _ID"></Text>
</Column>
<Column>
<Text text="Product_Name"></Text>
</Column>
<Column>
<Text text="Product_Price"></Text>
</Column>
</columns>

<items>
<ColumnListItem type="Navigation" press="onClick">
<cells>
<ObjectNumber number="{json>Num1}"></ObjectNumber>
<Text text="{json>Product1}"/>
<ObjectNumber number="{json>Price1}"/>
</cells>
</ColumnListItem>
</items>
</Table>

Here my model name is “json”. I have mentioned it in the component.js file, within the init function.
var a =new sap.ui.model.json.JSONModel();
a.loadData("model/model.json");
this.setModel(a,"json");

I have maintained the json data in “model.json” file. Here is the data:
{
"Content1": [{
"Num1": "1000",
"Product1": "Lenovo",
"Price1": "20K"
}, {
"Num1": "1001",
"Product1": "MI",
"Price1": "25K"
}]}

Now, the main part; to define the press event on the click of table rows and to extract the index of the row item clicked. This is the code maintained in the press event.

onClick: function(oEvt){
var item = oEvt.oSource.oBindingContexts.json.sPath;
var index = item.split('/')[2];
sap.m.MessageToast.show("Item Index = "+index);
}

The index here gives the position of the particular row item from the array, and therefore the position starts from 0. Hence the 1st item in the table has an index value of 0, similarly, 2nd would have a position of 1.

Here is the output:

 

Stay tuned for more blogs on SAPUI5. Comment down for any queries or mail me at sapui5tutors@gmail.com.

Read more