Search code examples
sapui5

How can I get the EDM type from 'context' bound to an element in SAPUI5?


I have an SAPUI5 application.

I defined an element by the smart fields like the following:

<smartField:SmartField value="{GefahrInVerzug}" width="auto">
    <smartField:configuration>
        <smartField:Configuration preventInitialDataFetchInValueHelpDialog="false" displayBehaviour="descriptionOnly"/>
    </smartField:configuration>
</smartField:SmartField>

The GefahrInVerzug field is defined as a boolean in my metadata:

<Property Name="GefahrInVerzug" Type="Edm.Boolean" sap:creatable="true" 
sap:updatable="true" sap:deletable="true" sap:label="Gefahr in Verzug"/>

Assume I have the following handler for onInputChange event of the rendered control:

onInputChange: function (oEvent) {
    var oField = oEvent.getSource(),
    oContext = oField.getBindingContext();
    //oContext.getEdmType();
}

How can I get the Edm Type by accessing the element (i.e. oField) or the context object (i.e. oContext).

In this case I am looking for a solution that return Edm.Boolean to me!


Solution

  • We can define the following functions in our controller to extract Edm Type from a field:

     // Returns a list that contains a map between 
     // UI5 elements' types and the property that contains the value!
     // Normally bound to the oData property 
     _getFieldTypeAttribute: function () {
        var aFieldTypes = {
            "sap.m.Input": "value",
            "sap.m.Select": "selectedKey",
            "sap.m.ComboBox": "selectedKey",
            "sap.m.CheckBox": "selected",
            "sap.m.DatePicker": "dateValue",
            "sap.m.DateTimePicker": "value",
            "sap.m.TextArea": "value",
            "sap.m.Switch": "state",
            "sap.ui.comp.smartfield.SmartField": "value"
        };
        return aFieldTypes;
    },
    
    // Extract the EDM type from Metadata
    _getEdmType: function(oField, sPropertyName){
        var regex = /\/([^(]+)/gm,
        oContext = oField.getBindingContext(),
        oModel = oContext.getModel(),
        oMetaModel = oModel.getMetaModel(),
        sBindingPath = oContext.getPath(),
        sType = null;
        //
        var aMatches = regex.exec(sBindingPath);
        if(aMatches.length > 0){
            var sSetName = aMatches[1],
                oEntitySet = oMetaModel.getODataEntitySet(sSetName),
                sEntityType = oEntitySet.entityType,
                oEntityType = oMetaModel.getODataEntityType(sEntityType),
                oProperty = oMetaModel.getODataProperty(oEntityType, sPropertyName);
                if (oProperty ) {
                    sType = oProperty.type;
                }
        }
        //
        return sType;
    },
    
    // Is fied when the input value is changed!
    onInputChange: function (oEvent) {
        var oField = oEvent.getSource(),
            oContext = oField.getBindingContext(),
            oModel = oContext.getModel(),
            aFieldTypes = this._getFieldTypeAttribute(),
            sFieldType = oField.getMetadata().getName(),
            sFieldPath = oField.getBinding(aFieldTypes[sFieldType]).getPath(),
            sPropertyName = sFieldPath && sFieldPath.startsWith("/") ? sFieldPath.substring(1) : sFieldPath,
            sBindingPath = sPropertyName ? oContext.getPath() + "/" + sPropertyName : null; 
        console.log(this._getEdmType(oField, sPropertyName));
    }
    

    It prints Edm.Boolean for example when this function is fired for an element of boolean type!