Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Swathi_Rani
Explorer
32,588
Hi All,

In this blog, I am going to explain Customizing the existing Default Column with Formatter and Creating Entire New Custom Column in SmartTable Control.


Introduction:

What is Smart Table: The Smart Table control creates a table based on OData metadata and When We Set Property useTablePersonalisation="true" it provides many operations (list sorting, filtering, grouping and arranging columns).

For More information: https://ui5.sap.com/#/api/sap.ui.comp.smarttable.SmartTable

What is Custom Column: A Custom Column is nothing but a column which is not available in oData metadata entity directly, such column is generated by the user as per the requirement. 

-- For Smart Table We Have to Bind oData Services(in my case I used Northwind oData Services)

  • Maintain routes in neo-app.json file. This step basically relates to maintaining the connection configurations.


Note: Please make sure the connection settings are maintained if you are using Northwind Services.
{
"path": "/Northwind",
"target": {
"type": "destination",
"name": "Northwind"
},
"description": "Northwind odata services"
}


  • In manifest.json file, We Define NorthWind Service to model.


"sap.app": {
"dataSources": {
"mainService": {
"uri": "/Northwind/V2/Northwind/Northwind.svc/",
"type": "OData",
"settings": {
"odataVersion": "2.0",
"localUri": "localService/metadata.xml"
}
}
}
},
"sap.ui5": {
"models": {
"": {
"dataSource": "mainService",
"preload": true
}
},
}

 

Situations Where We Add Custom Columns in Smart Table:

  • Assuming situation where the data is getting displayed in a table but you want to change the look of data using a formatter.


For example: If you are showing a “UnitsInStock” column. As a user you want to format it to display in “UnitsInStock >50 = Success State” & “UnitsInStock < 50 = Error State” format using a formatter.

  • Assuming of situation where as a user you would like to see Two different columns combined with each other under a single column instead of Two different column.


For example: It would be better to show 1 Column of “ProductID” in table along with its ProductName (like “1 -- Chai”) instead of showing 2 columns of “ProductID” & “ProductName” (“1” & “Chai”).

 

Situation 1: How we can Customized Column in a SmartTable Using Formatter.

Step 1: Create a new UI5 Application in SAP Cloud Web IDE.


Step 2: In the View Part, We will use the Smart Table Control using namespace called “sap.ui.comp.smarttable” and “sap.ui.core”.
<content>
<smartTable:SmartTable entitySet="Products" tableType="ResponsiveTable" showRowCount="true" useExportToExcel="false" useVariantManagement="false" useTablePersonalisation="true" demandPopin="true" enableAutoBinding="true" header="Line Items" placeToolbarInTable="true" persistencyKey="SmartTablePKey" initiallyVisibleFields="ProductName,QuantityPerUnit,UnitPrice,UnitsOnOrder,DisContinued, UnitsInStock” >
<Table sticky="ColumnHeaders,HeaderToolbar">
<columns>
<Column>
<customData>
<core:CustomData key="p13nData" value='\{"columnKey": "UnitsInStock", "leadingProperty": "UnitsInStock", "columnIndex": 5}'/>
</customData>
<Text text="{i18n>UnitsInStock}"></Text>
</Column>
</columns>
<items>
<ColumnListItem>
<ObjectStatus text="{UnitsInStock}" state="{path: 'UnitsInStock', formatter: '.formatter.StockStatus'}"></ObjectStatus>
</ColumnListItem>
</items>
</Table>
</smartTable:SmartTable>
</content>

Created a new column to the smart table, and Link the new custom column with key: “p13nData”.

Note: I’ve highlighted the added code with Red Rectangular box.


Step 3: In Model Folder, We Create New File Name formatter.js and wrote Formatter Logic.
sap.ui.define(function () {
return {
StockStatus: function (iNo) {
if (iNo < 50) {
return sap.ui.core.ValueState.Error;
} else {
return sap.ui.core.ValueState.Success;
}
}
};
});

Step 4: In the Controller Part, we will Define Formatter.
sap.ui.define([
"sap/ui/core/mvc/Controller",
"../model/formatter"
], function (Controller, formatter) {
"use strict";
return Controller.extend("com.data.BindingData.controller.BindingData", {
formatter: formatter,
});
});

To load our formatter functions, we have to add it to the controller. This controller simply stores the loaded formatter functions in a local property formatter to be able to access them in the view.The formatter references are loaded as a dependency to “sap.ui.define”.

Note: I’ve highlighted the added code with Red Rectangular box.


Output: You will see that the Custom Column that you created in the table with Formatter.

  • Here We See “UnitsInStock” column,with formatter “UnitsInStock >50 Will Change into Green Colour ” & “UnitsInStock < 50 Will Change into Red Colour”.



 

Situation 2: How we can create our own Custom Column in a SmartTable.

Step 1: Create a new UI5 Application in SAP Cloud Web IDE.


Step 2: In the View Part, We will use the Smart Table Control using namespace called “sap.ui.comp.smarttable” and “sap.ui.core”
<content>
<smartTable:SmartTable class="sapUiSizeCompact " editable="false" placeToolbarInTable="true" enableAutoBinding="true" entitySet="Products" header="Products" showFullScreenButton="true" showRowCount="true" tableType="ResponsiveTable" demandPopin="false" useExportToExcel="false" useTablePersonalisation="true" useVariantManagement="false"
initiallyVisibleFields="SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder, ReorderLevel,Discontinued" requestAtLeastFields="ProductID,ProductName" persistencyKey="SmartTablePKey">
<Table sticky="ColumnHeaders,HeaderToolbar">
<columns>
<Column>
<Label text="Product ID-Name"/>
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<ObjectIdentifier text="{ProductID} -- {ProductName}"/>
</cells>
</ColumnListItem>
</items>
</Table>
</smartTable:SmartTable>
</content>

Note:  -  The oData properties which are to be consumed (If not fetched initially using property “initiallyVisibleFields”) should be mentioned under property “requestAtLeastFields”.

Note: I’ve highlighted the added code with Red Rectangular box.


Output:


Conclusion:  Hence, To Customize any field with formatter or to Create an entire new column which is not available by default in SmartTable control, the “Custom Columns” approach will be used.
10 Comments
maheshpalavalli
Active Contributor
Nice example Swathi!! Thanks for sharing 🙂

You can change the title of the blog.post to a different one as you are showing only one way of adding a custom column to a smart table. The title could be like "Extending smart table with additional columns and customising them" or something similar.

Again, great work and thanks for sharing this with the community!! Looking forward to your other blog posts in future..
DenisGaland
Participant
0 Kudos
I really like the solution 2 that I was not aware of and will start using it by now, thanks Swathi 🙂
former_member781986
Discoverer
Hello,

 

Good one.

 

By Adding custom column, if my number of columns are more then data gets wrapped instead of scroll bar of a normal table, my table has 18 columns, all looks weird wrapped like a box. Is there any solution to this?

 

Thanks,

Mahenaz
mrbino60
Discoverer
0 Kudos
Hello Mahenaz,

I'm encountering the same issue . Did you find a solution for it ?
Swathi_Rani
Explorer
0 Kudos
You can use tableType="Table" then u will get the Scroll bar in both horizontal and vertical scroll
m1412
Discoverer
0 Kudos
The second method is extremely helpful!!! But can you tell me how to position the columns?
0 Kudos
m1412 - The column order can be changed by creating columns in the required order in the custom table in the xml view.
ayaanjafri
Discoverer
0 Kudos
Hi All,

I am using smart table in which I have some custom columns and some columns came with oData(Northwind) when I rendered the view my all columns are not coming horizontally.

 

 

 


 

 

The property of smart table which i am using are given below

 
entitySet="Invoices"
id="smarttable"
header="Invoices"
smartFilterId="smartFilterBar"
enableExport="true"
tableType="ResponsiveTable"
beforeExport="onBeforeExport"
persistencyKey="SmartTableAnalytical_Explored"
demandPopin="true" class="sapUiResponsiveContentPadding"
useTablePersonalisation="true"
enableAutoBinding="false"
beforeRebindTable="onRebindInvTable"
showFullScreenButton="true"
0 Kudos
Hi Jafri,

 

I'm having the same problem with the standard Fiori app. If still no problem, please share your solution.

 

 

Regards

Lax
lfrey91
Participant
Hi All,

Hi ayaanjafri

Hi t.suresh

 

i had the same problem. (smartTable, CustomColumns, VerticalScroll)

 

The solution is, to use the sap.ui.table instead of using the sap.m.table

https://sapui5.hana.ondemand.com/#/entity/sap.ui.table.Table