更新UI5中的模型,使用格式化程序时,双向数据绑定变为单向 [英] Updating model in UI5, two-way data binding becomes one-way when using formatter

查看:63
本文介绍了更新UI5中的模型,使用格式化程序时,双向数据绑定变为单向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的UI5-app中,我有一个表,其中每行包含一个 <格式>格式化程序 ,因为数据是从数据库中以 1 / 0 而不是> true / false ,这可能会破坏默认的

In my UI5-app, I have a table where each row contains a sap.m.Switch, which is bound to the model via formatter, since the data is coming from the DB as 1/0, rather than true/false, and that, probably, breaks the default two-way data binding.

要根据此开关的编辑值更新数据模型,我实现了以下

To update a data model upon the editing value of this switch, I implemented the following change-event:

onChangeSwitch: function onChangeSwitch(oEvent) {
    let context = oEvent.oSource.getBindingContext();
    let itemIndex = context.sPath.substr(1);
    let oModel = this.getView().byId("idTablePersons").getModel();
    oModel.oData[itemIndex].isPersonActive = (oEvent.mParameters.state) ? 1 : 0;
    oModel.refresh();
}

它可以工作,但是我不确定这是否是实现这种逻辑的正确方法.
更改 sap.m.Switch 值后,是否有标准的方法来更新模型?

It works, but I'm not sure if it is a proper way to implement such logic.
Is there a standard way to update a model after changing sap.m.Switch value?

推荐答案

我认为您正以错误的方式来解决这个问题. sap.m.Switch 已经具有指示可以直接绑定到模型的状态的属性.

I think you're approaching this the wrong way around. sap.m.Switch already has an attribute to indicate state which you can directly bind to a model.

<Switch state="{IsPersonActive}" />

假定您将表中的项目绑定到一个未命名的模型,这会将绑定行上的 IsPersonActive 标志设置为 true false ,具体取决于开关的状态.

Assuming you bound the items in the table to an unnamed model, that'll set the IsPersonActive flag on the bound line to true or false depending on the state of the switch.

这也意味着,如果您的实体集中某些 IsPersonActive 标志已经设置为true或false,它将以正确的状态显示开关.

This also means it'll come out with the switches in the correct state if certain IsPersonActive flags are already set to true or false in your entity sets.

(...)数据以 1 / 0 来自数据库,而不是 true / false (...).
更改 sap.m.Switch 值后,是否有标准的方法来更新模型?

(…) the data is coming from the DB as 1/0, rather than true/false (…).
Is there a standard way to update a model after changing sap.m.Switch value?

来自 https://embed.plnkr.co/wwQXf8bTuiTP4RlP :

NumericBoolean.js (最小示例):

sap.ui.define([
  "sap/ui/model/SimpleType",
], Type => Type.extend('demo.model.type.NumericBoolean', {
  constructor: function() {
    Type.apply(this, arguments);
  },
  formatValue: iValue => !!+iValue,
  parseValue: bValue => bValue ? 1 : 0,
  validateValue: vValue => { /*validate...*/ },
}));

<Switch xmlns="sap.m" xmlns:core="sap.ui.core"
  core:require="{ NumericBoolean: 'demo/model/type/NumericBoolean' }"
  state="{
    path: '/1or0',
    type: 'NumericBoolean'
  }"
/>

重要说明:
即使未提供实现,也必须保留 validateValue 声明,否则 sap.m.Switch 将无法正常工作.

Important note:
It's mandatory to keep the validateValue declaration even if the implementation is not provided, otherwise sap.m.Switch will not work correctly.

这篇关于更新UI5中的模型,使用格式化程序时,双向数据绑定变为单向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆