melai
melai

Reputation: 97

Check if text binded to a control is null

I'm trying to check if I'm binding a null data on a controller. If the data is null, I need to not show the label as well as the binded data.

Below is my code right now.

var oMatNrRow1 = new sap.ui.commons.layout.MatrixLayoutRow();
    control1 = new sap.ui.commons.Label({
    text : Appcc.getText("MATERIAL_NO") + ":"
});

matrixCell1 = new sap.ui.commons.layout.MatrixLayoutCell();
matrixCell1.addContent(control1);
control = new sap.ui.commons.Label();
control.bindProperty("text", "matnr");

matrixCell1.addContent(control);
oMatNrRow1.addCell(matrixCell1);
vendorTable.addRow(oMatNrRow1);

I have tried control.getProperty("text") but it only returns null when it should have return a number if matnr is not null.

I also tried formatter. I will have no problem with formatter if matnr is not null. But if it is null, the point is to destroy/delete contents of both matrixCell1 instances. In my code below, addition of matrixCell1 content will still push through.

...
formatter: function(matnr){
   if (matnr !== ''){
       return contract
   } else{
       matrixCell.destroyContent();
   }
});

matrixCell1.addContent(control);
oMatNrRow1.addCell(matrixCell1);
vendorTable.addRow(oMatNrRow1);

Not sure if you can move the ff code inside if statement

matrixCell1.addContent(control);
oMatNrRow1.addCell(matrixCell1);
vendorTable.addRow(oMatNrRow1);

Any ideas are appreciated.

Upvotes: 0

Views: 2086

Answers (2)

melai
melai

Reputation: 97

Found a workaround on my issue. It was a simple if else condition. For the if statement, I just added data[j].matnr and it worked! I also noticed that this was how SAP implemented the behavior also e.g. oSearchViewData.description.

Upvotes: 0

hackToxa
hackToxa

Reputation: 47

I would also suggest to user the visible property. Are you aware of conditional binding of UI5? Using them you do not need the formatter at all in that case. see

Upvotes: 1

Related Questions