Custome Formatter within a Controller - SAPUI5

3k Views Asked by At

I'm trying to format gender field (in SAP table field: CHAR1 to 0(F) and 1(M) to fit the selectedIndex property of RadioButtonGroup.

This is my view : (DetailDialog.fragment.xml )

<RadioButtonGroup width="100%" columns="2" selectedIndex="{path: 'Gendr', formatter:'.formatter' }" id="__group1">

The above XML Fragment is called by the main view controller:

ItemPress: function(oEvent) {
        var detailDialog = this.getView().byId("DetailDialog");
        var that = this;
        var view = this.getView();
        var path = oEvent.getParameter("listItem").getBindingContext().getPath();
        var oDummyController = {
            formatter: function(gendr) {
                switch (gendr) {
                    case "M":
                        return 0;
                    case "F":
                        return 1;
                }
            },

            closeDialog: function() {
                detailDialog.close();
            }
        };
        if (!detailDialog) {
            detailDialog = sap.ui.xmlfragment(view.getId(), "Demo1.view.DetailDialog", oDummyController);
        }
        var jSonModel = new sap.ui.model.json.JSONModel();
        function fnSuccess(oData, oResponse) {
            jSonModel.setData(oData);
        }
        var oModel = view.getModel();
        oModel.read(path, {
                success: fnSuccess
            })
            //Set data for dialog
        this.getView().byId("__formDetail").setModel(jSonModel);
        detailDialog.open();
    }

My problem is that the formatter is not working at all.

Any suggestion?

1

There are 1 best solutions below

0
On BEST ANSWER

Option one: (not sure if it works for fragments too)

Change formatter:'.formatter' to formatter:'Demo1.view.DetailDialog.formatter'.

Option two: Format the data since anyway you are binding data from controller. (And surely will work.)

function fnSuccess(oData, oResponse) {
   oData.GendrValue = oData.Gendr == "M"?1:0;
   jSonModel.setData(oData);
}

and also change the binding: selectedIndex="{path: 'GendrValue'}"