C. Ubkcah
C. Ubkcah

Reputation: 273

SAPUI5 formatter parameter value is always null

I am trying to add an icon to my table column. The icon should be selected depending on the value. Therefore I implemented some formatter function which is based in the model/formatter.js file.

Here is the code I try to add the the table column:

new sap.ui.layout.VerticalLayout({
content: [
    new sap.m.Label({text:"{Items>Status}"}),
    new sap.ui.core.Icon({
        src: {
            parts: [
                "Items>Status"
            ],
            formatter: Formatter.formatStatusIcon
        }, 
        size: "1rem",
        color: {
            parts: [
                "Items>Status"
            ],
            formatter: Formatter.formatStatusIconColor
        }
    })
]
})

The label does display the correct value. The icon is not displayed. The formatter function has as input value always null even though the values exist and are not null. I am using SAPUI5 version 1.61.2. I already tried different syntaxes of the parts property but none of them did work. I also tried adding some static value instead of "Items>Status" for testing purpose but the input in the formatter function is still null.

Has anyone an idea why the input of the formatter function is always null?

Upvotes: 0

Views: 1741

Answers (3)

Inizio
Inizio

Reputation: 2256

Based on the your input, I have understood that you have a JS view and your are changing the Icon and its color based on the status

View.js

var oTable = new sap.m.Table("idPrdList", {   
    headerText : "List of Products",
    headerDesign : sap.m.ListHeaderDesign.Standard, 
    mode : sap.m.ListMode.None,   
    includeItemInSelection : false,   
});

var col1 = new sap.m.Column({header: new sap.m.Label({text:"Product Name"})});
oTable.addColumn(col1); 

var col2 = new sap.m.Column({header: new sap.m.Label({text:"Description"})});
oTable.addColumn(col2); 

var col3 = new sap.m.Column({header: new sap.m.Label({text:"Price"})});
oTable.addColumn(col3);

controller.js

var oModel = new sap.ui.model.json.JSONModel();
oModel.setData({
  'items': [
  {
    'ProductID': "sdf",
    'ProductName': "asdf",
    "Status": "1"
  },
  {
    'ProductID': "ss",
    'ProductName': "asf",
    "Status": "1"
  },
  {
    'ProductID': "fff",
    'ProductName': "asdf",
    "Status": "2"
  },
  {
    'ProductID': "fas",
    'ProductName': "asdf",
    "Status": "1"
  },
  {
    'ProductID': "asdfa",
    'ProductName': "asdfwer",
    "Status": "2"
  }]
});
sap.ui.getCore().setModel(oModel, "tableData");
var oTable = sap.ui.getCore().byId("idPrdList");
var colItems = new sap.m.ColumnListItem("colItems", {
  type: "Active"
});
var txtNAME = new sap.m.Text({
  text: "{tableData>ProductID}"
});
colItems.addCell(txtNAME);
var txtNAME2 = new sap.m.Text({
  text: "{tableData>ProductName}"
});
colItems.addCell(txtNAME2);
var txtNAME3 = new sap.ui.layout.VerticalLayout({
  content: [
    new sap.m.Label({
      text: "{tableData>Status}"
    }),
    new sap.ui.core.Icon({
      src: {
        parts: ["tableData>Status"],
        formatter: assets.util.mFormatter.formatStatusIcon
      },
      size: "1rem",
      color: {
        parts: ["tableData>Status"],
        formatter: assets.util.mFormatter.formatStatusIconColor
      }
    })
  ]
})
colItems.addCell(txtNAME3);
oTable.bindAggregation("items", "tableData>/items", colItems);
oTable.setModel(oModel, "tableData");

Formatter.js

jQuery.sap.declare("assets.util.mFormatter");
assets.util.mFormatter = {
    formatStatusIcon: function(Status) {
        return (Status === "2" ? "ICONPath1" : "ICONPath2");
    },
    formatStatusIconColor: function(Status) {
        return (Status === "2" ? "Color1" : "Color2");
    },
};

Output

enter image description here

Upvotes: 1

Francesco Iannazzo
Francesco Iannazzo

Reputation: 626

Your syntax is wrong I think. Remove that parts stuff.

Try this:

src="{path: 'invoice>Status',
      formatter: '.Formatter.statusText'}

Upvotes: 1

fareslt
fareslt

Reputation: 198

Try this syntax below :

new sap.ui.layout.VerticalLayout({
content: [
    new sap.m.Label({text:"{Items>Status}"}),
    new sap.ui.core.Icon({
        src: {
            parts: [
{path: 'Items>Status'}

            ],
            formatter: '.Formatter.formatStatusIcon'
        }, 
        size: "1rem",
        color: {
            parts: [
                "Items>Status"
            ],
            formatter: Formatter.formatStatusIconColor
        }
    })
]
})

Upvotes: 0

Related Questions