根据文本值设置图标

Set icon depending on text value

我需要构建一个包含 2 列的 table - 第一列是姓名,第二列是此人是否获得批准。

这是JSON我从服务器得到的:

{"records":[
{"approved":"Yes","name":"Doe John"},
{"approved":"No","name":"Doe Jane"}]}

我使用上面的 JSON 将模型设置为 table。

我需要根据 "approved" 键的值在第二列中显示图标:如果获得批准,则为 sap.ui.core.IconPool.getIconURI("accept"),否则为 sap.ui.core.IconPool.getIconURI("decline")

以下是我尝试定义 table 行的方式:

var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(data); //the above JSON response
oTable = sap.ui.getCore().byId("tableId");
oTable.setModel(oModel);
oTable.bindItems({
    path: "/records", 
    template: new sap.m.ColumnListItem({
        cells : [ 
        new sap.m.Text({text: "{name}"}), 
        new sap.ui.core.Icon({ //my problem starts here
            path: "approved",
            formatter: function(approved){
                if (approved==="Yes"){return new sap.ui.core.IconPool.getIconURI("accept");}
                else{return new sap.ui.core.IconPool.getIconURI("decline");};
            }
        }), 
        ]
    })
});

我无法正确定义图标列。上面的代码显示了我在寻找答案一段时间后尝试做的事情。

我怎样才能让它发挥作用?

差不多好了:-)您忘记包含要应用格式化程序的字段(在本例中,src

改变

    new sap.ui.core.Icon({
        path: "approved",
        formatter: function(approved){
            if (approved==="Yes"){return new sap.ui.core.IconPool.getIconURI("accept");}
            else{return new sap.ui.core.IconPool.getIconURI("decline");};
        }
    }),

    new sap.ui.core.Icon({
        src : {
            path: "approved",
            formatter: function(approved){
                if (approved==="Yes"){return new sap.ui.core.IconPool.getIconURI("accept");}
                else{return new sap.ui.core.IconPool.getIconURI("decline");};
            }
        }
    }),