SAPUI5格式化程序参数值始终为null

时间:2019-03-19 09:09:31

标签: sapui5 formatter

我正在尝试在表格列中添加一个图标。应根据值选择图标。因此,我实现了一些基于model/formatter.js文件的格式化程序功能。

这是我尝试添加表格列的代码:

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
        }
    })
]
})

标签确实显示正确的值。不显示该图标。格式化程序功能始终将null作为输入值,即使这些值存在且不为空。我正在使用SAPUI5版本1.61.2。我已经尝试了parts属性的不同语法,但是没有一个起作用。我还尝试添加一些静态值而不是"Items>Status"进行测试,但是格式化程序功能中的输入仍然是null

有人知道为什么格式化程序功能的输入始终为空吗?

3 个答案:

答案 0 :(得分:1)

根据您的输入,我了解到您拥有JS视图,并且您正在根据状态更改图标及其颜色

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");
    },
};

输出

enter image description here

答案 1 :(得分:1)

我认为您的语法是错误的。删除那些零件。

尝试一下:

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

答案 2 :(得分:0)

请尝试以下语法:

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
        }
    })
]
})