Skip to Content
0

How to store my checkbox values as 1 for true and null for false in hana db TwoWay binding?

Aug 18, 2017 at 06:03 AM

85

avatar image
Former Member

Hello guys,

I have an application in SAPUI5 with a lot of checkboxes.

We store the checkboxes values as TINYINT in the database, how can I convert them to send in the update batch request 1 for true and null for false values?

I tried with formatter, but it doesn't work in TwoWay binding. Tried to convert this old snippet

oCheckBox.bindProperty("checked", {path: "/some_path", type: BoolString, mode: sap.ui.model.BindingMode.TwoWay});

in my xml view, because this is js view, unfortunately didn't work also.

Could you give me an example with a code snippet or an idea how to fix this issue, please?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
avatar image
Former Member Aug 28, 2017 at 07:01 AM
0

Hello guys,

I found a solution about this and want to share with you:

You can create your custom data type in SAPUI5, which inheriting from sap.ui.model.SimpleType and you can change the logic in the functions formatValue (called whenever the value in the model is changed to convert it to the type of the control property it is bound to) and parseValue (called whenever the user has modified a value in the UI and the change is transported back into the model). validateValue is called to check additional constraints, like minimum or maximum value, and throws a ValidateException if any constraints are violated.

You can find the code snippet from our app below:

Controller:

sap.ui.model.SimpleType.extend("sap.ui.model.type.Boolean", {

formatValue: function(oValue) {

if (oValue === 1) {

return true;

} if (oValue === null || oValue === 0)

{ return false; }

},

parseValue: function(oValue) {

if (oValue === true) {

return 1; }

else if (oValue === false) {

return 0;

}

},

validateValue: function(oValue) {

return oValue;

}

});

View:

<CheckBox text="{i18n>hdCheckBox}" selected="{path: 'hd', type: 'sap.ui.model.type.Boolean'}" editable="true" enabled="true" visible="true" width="auto" textDirection="Inherit"/>

Thanks for your answers!

Share
10 |10000 characters needed characters left characters exceeded
Sharath M G Aug 18, 2017 at 07:00 AM
0

Use formatter function in your XML view control for the checkbox. So, when the user selects a checkbox this function is invoked and you can manipulate checked to your preferred value(for HANA DB) and vice versa(when the user unchecks the field.

Look up on formatter functions in XML views.

Regards,

Sharath

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hello Sharath,

It was the first try, unfortunately formatter doesn't work in TwoWay binding as I said.

Thanks anyway.

0
Sergio Guerrero Aug 18, 2017 at 03:44 PM
0

your checkbox can use Expression Binding on the checked property as follows:

var oCheckbox = new sap.m.Checkbox({ checked: "{= ${/some_path} == 1 }" });

Show 5 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Sergio,

This is a snippet of a js view I guess. We're using xml views and the property checked doesn't exist for the control CheckBox. The new one is selected.

In our case should look like

<CheckBox selected="{path: 'some_path' == 1}"/>

and it doesn't work

Thanks

0

so try as i recommended -

<CheckBox selected="{= ${/some_path} == 1 }"/>

0
Former Member

Nope, doesn't work, SyntaxError: Expected '}' and instead saw '=' in expression binding {= ${hd} == 1 } at position 9

0
Former Member

It's the same with / before the path.

0

if you have a sap.ui.commons.Checkbox then use the checked property.

if you are using the sap.m.Checkbox, then use the selected property/

0