Skip to Content

Use OData Metadata to dynamically create the columns

Use OData Metadata to dynamically create the columns
You will learn

Use the Metadata to dynamically create columns in your table.

jung-thomasThomas JungJanuary 5, 2021
Created by
ccmehil
September 6, 2016
Contributors
jung-thomas
ccmehil

Prerequisites

  • This tutorial is designed for SAP HANA on premise and SAP HANA, express edition. It is not designed for SAP HANA Cloud.
  • Proficiency: Intermediate
  • Tutorials: Consume a Basic OData Service
  • Step 1

    In the previous tutorial, you hard-coded the columns to be displayed from your OData service. However, OData services expose all their meta data and we can use this feature to build the columns dynamically. You can test this by running the web module and calling the OData service and adding $metadata

    view file

    You can see the service exposes the names of the fields and other properties such as the length.

  • Step 2

    Return to the controller of odataBasic from the previous tutorial and replace the following lines:

    view file

    With the following code:

    javascript
    Copy
    function fnLoadMetadata() {
    	try {
    		oTable.setModel(bpModel);
    		oTable.setEntitySet("BusinessPartners");
    		var oMeta = bpModel.getServiceMetadata();
    		var headerFields = "";
    		for (var i = 0; i < oMeta.dataServices.schema[0].entityType[0].property.length; i++) {
    			var property = oMeta.dataServices.schema[0].entityType[0].property[i];
    			headerFields += property.name + ",";
    		}
    		oTable.setInitiallyVisibleFields(headerFields);
    	} catch (e) {
    		console.log(e.toString());
    		oDataFailed();
    	}
    }
    bpModel.attachMetadataLoaded(bpModel, function () {
    	fnLoadMetadata();
    });
    fnLoadMetadata();
    

    You can see the new function that retrieves the metadata from the service, loops at the results and concatenates the names of the fields separated by a comma. The fields are then attached to the bpModel component.

  • Step 3

    Save the files you have modified and run the web module:

    view file
Back to top