Skip to Content
Previous

Use oData Metadata to dynamically create the columns

By Craig Cmehil

Use oData Metadata to dynamically create the columns

You will learn

Use the Metadata to dynamically create columns in your table.

Details


Step 1: Check the metadata from the service

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: Adapt the controller

Return to the controller and replace the following lines:

view file

With the following code:


function fnLoadMetadata() { oTable.setModel(bpModel); oTable.setEntitySet("BP"); 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); } bpModel.attachMetadataLoaded(bpModel, function() { 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 and run

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

view file

Next Steps

Updated 09/13/2017

Time to Complete

10 Min.

Intermediate

Prerequisites

Next
Back to top

Call me now