Set default columns in UI5 Smart table

How to select the default columns for Smart table in SAP UI5

‘initiallyVisibleFields’ property of the SAP UI5 Smart table is used to select the default columns for the smart table.

initiallyVisibleFields=”Id,Name”

As you see above, the fields(columns) of the smart table are separated by Comma. The columns will be visible in the same order as mentioned above with customer Id as first column and Customer Name as second column.

Important points to keep in mind while using this Property

  • The ‘initiallyVisibleFields’ Property must be used only if the CDS view is created without the Line Item Annotations. So there should not be any Line Item Annotations in the metadata generated.
  • Only during the initialization, the ‘initiallyVisibleFields’ Property of Smart Table can be used. It cannot be modified after the Smart table control is initialized.
  • The Property is not Validated. So if the fields are not mentioned properly, then it will not work. Like if spaces exists additionally or if there is any special symbols, then the columns will not be displayed properly.

            <core:View xmlns:core="sap.ui.core" controllerName="initialView.controller"
                xmlns="sap.m"
                xmlns:smart="sap.ui.comp.smarttable"
                xmlns:customData="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1"
                height="100%">
                <Page id="page1" title="Customers1">
                   <VBox fitContainer="true">
                       <smart:SmartTable id="smartTable1" entitySet="CustomerSet"
                           tableType="ResponsiveTable"
                           showFullScreenButton="true" header="Customers" showRowCount="true"
                           editTogglable="true"
                           customData:useSmartField="true"
                           useTablePersonalisation="true"
                           initiallyVisibleFields="Id,Name"
                           enableAutoBinding="true">
                       </smart:SmartTable>
                   </VBox>
                </Page>
            </core:View>

The priority in which the columns are displayed in the following

  1. Initially if the XML View contains some fields, then these fields are displayed first. This takes the highest priority.
  2. If the LineItem Annotations are present in the metadata, then these fields are displayed second.
  3. Atlast, the initiallyVisibleFields are taken in to consideration. It appears at the end in the display.