简体   繁体   English

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

[英]SAPUI5: how to getBindProperty from property that was binded from model?

I have this code: 我有以下代码:

//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);

Problem is that I am defining a property in the constructor that should print the img source: 问题是我在构造函数中定义了一个应打印img源的属性:

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

But when I am trying to take it like this: 但是,当我尝试采用这种方式时:

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

I am getting blank text (nothing). 我收到空白文本(什么都没有)。

How can I get this bounded value? 如何获得此有界值? Any other function? 还有其他功能吗?

Second question: How can I add custom property to img? 第二个问题:如何向img?添加自定义属性img? Say I wont to have in img: src, alt and myCustomTxt. 说我不会在img中使用src,alt和myCustomTxt。 How can I add the property myCustomTxt ? 如何添加属性myCustomTxt

update: 更新:

I've tried: 我试过了:

  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());

        }
    });

but I am getting this error when pressing a image: 但按图像时出现此错误:

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

Thanks! 谢谢!

I assume you have a property called "src" in the each record of your model. 我假设您在模型的每个记录中都有一个名为“ src”的属性。 Then you can bind it as follows: 然后,您可以按以下方式绑定它:

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

To add custom attributes you can use method addCustomData which expects and instance of sap.ui.core.CustomData . 要添加自定义属性,你可以用它期望方法addCustomData和实例sap.ui.core.CustomData的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM