javascriptsapui5thisformatter

How to bind this using fomatter from the controller?


I am trying to re-use the formatter methods used in the xml view but from the controller. This is because I need to export to excel a ui.table and the class sap.ui.export.Spreadsheet get data is it is in the JSON Model and not as it looks like after the applying the formatter in the xml View.

sap.ui.define([
    "../controller/BaseController",
    "../model/models",
    "../model/formatter"
],
    function (BaseController, models, formatter) {
        "use strict";

        return BaseController.extend("ns.controller.Main", {
            formatter: formatter,
            _formattedUiTableData: function (sCode){
                return formatter.getStatusText(sCode);

            }
        });
});

/////////////////at formatter.js /////////////////////////////////////////////
sap.ui.define(["../controller/BaseController"

], function () {
    "use strict";

    return {
        serviceCoderDescription(sCode) {
            const oResourceBundle = this.getOwnerComponent().getModel("i18n").getResourceBundle();
            switch (sCode) {
                case "A":
                    return oResourceBundle.getText("CodeA");
                case "B":
                    return oResourceBundle.getText("CodeB");
                default:
                    return sCode;
            }
        }
    
    };
});

enter image description here

enter image description here


Solution

  • Can you try whether the following approach works?

    return formatter.getStatusText.bind(this)(sCode);
    

    You should also be able to use

    this.oView.getModel("i18n").getResourceBundle();
    

    instead of

    this.getOwnerComponent().getModel("i18n").getResourceBundle();