0

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 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Here is the sap help link.
1

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.

2 Comments

Thanks Matti. I am currently using formatters itself. SAPUI5 documentation says formatters are trivial. Also, how do I define my complex binding in manifest file??
That depends on your UI5 version, but generally speaking you should be able to add settings to your model. Add value defaultBindingMode with TwoWay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.