2
votes

I have some implementation where I loop over an array of objects and create a new GenericTile for each object. I need to set the blocked property of the tile dependant of the objects Count property.

I tried to use some expression binding for this but I get the following error:

"true" is of type string, expected boolean for property "blocked" of Element sap.m.GenericTile#exampleTile0

Since my data is not in a model but in an object I try to use some variable in the expression binding. I did not find any example for this case so I am not sure if this can even work or if I need to create a new model (at least the error text sounds for me like the variable is interpreted).

Here is my relevant code:

for (var i = 0; i < aExampleData.length; i++) {
    var oNewTile = new GenericTile("exampleTile" + i, {
        tileContent: new sap.m.TileContent({
            content: new sap.ui.layout.HorizontalLayout({
                content: [
                    new sap.m.Title({
                        text: aExampleData[i].Name
                    }),
                    new sap.m.Text({
                        text: aExampleData[i].Description
                    }),
                    new sap.m.HBox({
                        items: [
                            new sap.m.Text({
                                text: that.getResourceBundle().getText("count")
                            })
                            new sap.m.Text({
                                text: aExampleData[i].Count
                            })
                        ]
                    })
                ]
            })
        }),
        blocked: "{=" + aExampleData[i].Count + "> 0 ? false : true }"
    });

    this.byId(xxx).addItem(oNewTile);
}

Can someone give me a hint how to solve this? Thanks!

3
Why not simply do blocked: aExampleData[i].Count <= 0 ? If you want to do it in a more proper "UI5 way", you will probably have to put the data in a JSONModel and bind the parent of the Tiles to it. - TiiJ7
Btw. .set/getBlocked are private APIs and the property blocked is hidden (See github.com/SAP/openui5/commit/…). I'd suggest to avoid using this property. - Boghyon Hoffmann

3 Answers

0
votes

I forgot that I can just use some variable in the loop like:

if (aExampleData[i].Count > 0) {
    bBlocked = false;
} else {
    bBlocked = true;
}

...and assign it to the blocked property.

However, it would still be interesting to find out if the variable can also be used in the expression binding and how the error can be solved.

0
votes

replace

blocked: "{=" + aExampleData[i].Count + "> 0 ? false : true }"

with

blocked: !(aExampleData[i].Count > 0)

Using expression syntax here is pointless since there is no binding involved

0
votes

Well essentially Expression binding was introduced for XML Views

Expression binding is especially useful in the context of SAPUI5 XML template where XML views with template are prepossessed so that the SAPUI5 controller is a natural place to put custom formatter functions that are not available. Source

In JS you can use formatter function for binded values or as Ji aSH mentined just indicating variable condition