SAPUI5:如何从模型中绑定的属性获取getBindProperty?

时间:2016-12-01 12:25:31

标签: javascript sapui5

我有这段代码:

//create table
tableContent=getcontent();

    var oTable2 = new sap.ui.table.Table(tableId, {
        width : "100%",
        visibleRowCount: tableContent.length,
        selectionMode : sap.ui.table.SelectionMode.None,
        resizable : false,
        flexible : false
    });

    var img = new sap.m.Image({
            press: function() {console.log(img.getProperty("src"))
        }});

    img.bindProperty("src", "src");

    oTable2.addColumn(new sap.ui.table.Column({
            label: new sap.ui.commons.Label({ text: "" }),
            template: img,
            autoResizable: false,
            width : '10%'
        }));
    var oModel2 = new sap.ui.model.json.JSONModel();
    oModel2.setData({ modelData: tableContent });
    oTable2.setModel(oModel2);
    oTable2.bindRows("/modelData");
    oTable2.sort(oTable2.getColumns()[0]);
    oTable2.placeAt(containingDivId);

问题是我在构造函数中定义了一个应该打印img源的属性:

var img = new sap.m.Image({
            press: function() {console.log(img.getProperty("src"))
        }}); 

但是当我试图这样做时:

img.bindProperty("src", "src");

我收到空白文本(没有)。

我怎样才能获得这个有限的价值?还有其他功能吗?

第二个问题:如何向img?添加自定义属性说我不会拥有img:src,alt和myCustomTxt。如何添加属性myCustomTxt

更新

我试过了:

  var img = new sap.m.Image({
        "src" : "assets/images/btn-X.png",
        "press" : function(event) {
            var binding = event.getSource().getBindingInfo("src").binding;

                console.log(binding.getValue());

        }
    });

但按下图像时出现此错误:

cart-module.js:151 Uncaught TypeError: Cannot read property 'binding' of undefined(…)

谢谢!

1 个答案:

答案 0 :(得分:2)

我假设您在模型的每条记录中都有一个名为“src”的属性。然后你可以按如下方式绑定它:

new sap.m.Image({
    "path" : "{src}",
    "press" : function(event) {
        var binding = event.getSource().getBindingInfo("src").binding;
        if (binding) {
            jQuery.sap.log.debug(binding.getValue());
        }
     }
});

要添加自定义属性,您可以使用 sap.ui.core.CustomData sap.ui.core.CustomData 的方法 addCustomData

img.addCustomData(new sap.ui.core.CustomData({ "key" : myCustomTxt, "value" : "myCustomText" });