Matoy
Matoy

Reputation: 1808

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:

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? Say I wont to have in img: src, alt and myCustomTxt. How can I add the property 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!

Upvotes: 0

Views: 3000

Answers (1)

matbtt
matbtt

Reputation: 4232

I assume you have a property called "src" in the each record of your model. 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.

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

Upvotes: 3

Related Questions