0
votes

I am trying to convert a string value to Boolean while binding it from a JSONModel. Ideally the value in my model is "true"/"false" and I want to bind it to the visible property of an item. The model is defined to be TwoWay binding but I guess that does not matter in this case

I have declared "complex binding" in the index.html.

data-sap-ui-xx-bindingSyntax="complex" 

Then I create my XML view and bind the property from the model as below:

<P13nColumnsItem>
  columnKey="{tableVariantAFModel>Fieldname}" 
visible="{path:'tableVariantAFModel>Visible', type: 'sap.ui.model.type.Boolean', mode: 'sap.ui.model.BindingMode.TwoWay'}" 
index="{tableVariantAFModel>DisplayOrder}">
  <P13nColumnsItem>

When I run my app,it throws the below error: enter image description here

Is there any step I am missing? Also, I need to add this app to the Fiori Launchpad, so I need to define the complex binding in manifest.json file rather than in index.html . Where can I define it in the manifest file.

2

2 Answers

3
votes

For simple use cases like this you can use an expression binding instead of implementing additional logic somewhere.

<P13nColumnsItem>
  columnKey="{tableVariantAFModel>Fieldname}" 
  visible="{= ${tableVariantAFModel>Visible} === 'true'}" 
  index="{tableVariantAFModel>DisplayOrder}">
<P13nColumnsItem>
1
votes

i would advise to use a formatter. See here. In the formatter you could write:

visible="{path:'tableVariantAFModel>Visible', formatter: '.formatter.stringToBoolean'}" 

in the formatter you could create the function like:

stringToBoolean: function(_stringBoolean){
    (_stringBoolean === "true") ? return true : return false;
}

You have to make sure that you instantiate the formatter in your controller, or optionally you could choose a function in your controller itself.