我试着学习SAPUI5与样品兄弟会演示工具包输入检查。我收到一条错误消息:oInput.getBinding不是函数
我有一个简单的输入字段xml:
<Label text="Name" required="false" width="60%" visible="true"/>
<Input id="nameInput" type="Text" enabled="true" visible="true" valueHelpOnly="false" required="true" width="60%" valueStateText="Name must not be empty." maxLength="0" value="{previewModel>/name}" change= "onChange"/>我的控制器:
_validateInput: function(oInput) {
var oView = this.getView().byId("nameInput");
oView.setModel(this.getView().getModel("previewModel"));
var oBinding = oInput.getBinding("value");
var sValueState = "None";
var bValidationError = false;
try {
oBinding.getType().validateValue(oInput.getValue());
} catch (oException) {
sValueState = "Error";
bValidationError = true;
}
oInput.setValueState(sValueState);
return bValidationError;
},
/**
* Event handler for the continue button
*/
onContinue : function () {
// collect input controls
var that = this;
var oView = this.getView();
var aInputs =oView.byId("nameInput");
var bValidationError = false;
// check that inputs are not empty
// this does not happen during data binding as this is only triggered by changes
jQuery.each(aInputs, function (i, oInput) {
bValidationError = that._validateInput(oInput) || bValidationError;
});
// output result
if (!bValidationError) {
MessageToast.show("The input is validated. You could now continue to the next screen");
} else {
MessageBox.alert("A validation error has occured. Complete your input first");
}
},
// onChange update valueState of input
onChange: function(oEvent) {
var oInput = oEvent.getSource();
this._validateInput(oInput);
},有人能给我解释一下我怎么能设定模型吗?
发布于 2020-01-21 08:53:58
你的模型很好,而且绑定正确。代码中的问题在这里,在onContinue函数中
jQuery.each(aInputs, function (i, oInput) {
bValidationError = that._validateInput(oInput) || bValidationError;
});aInput不是数组,所以您的代码不会迭代数组元素。要快速解决这个问题,您可以在声明周围加上括号,如下所示:
var aInputs = [
oView.byId("nameInput")
];另外,您可以删除_validateInput方法的前两行,因为它们是无用的.
发布于 2020-01-20 15:29:34
通常,在加载视图之后,而不是在值被更改时,我们会设置模型。例如,如果您想设置一个名为“JSONModel”的previewModel,您可以按照下面提到的那样做。
注意,在初始化控制器时会调用onInit。如果按如下方式正确绑定模型,那么oEvent.getSource().getBinding(" value ")将返回预期值。
onInit: function(){
var oView = this.getView().byId("nameInput");
oView.setModel(new sap.ui.model.json.JSONModel({
name : "HELLO"
}), "previewModel");
},
onChange: function(oEvent) {
var oInput = oEvent.getSource();
this._validateInput(oInput);
},
...此外,为了验证输入文本,可以执行以下操作:
_validateInput: function(oInput) {
var oBinding = oInput.getBinding("value");
var sValueState = "None";
var sValueStateText = "";
var bValidationError = false;
if(oBinding.getValue().length === 0){
sValueState = "Error";
sValueStateText = "Custom Error"
}
oInput.setValueState(sValueState);
if(sValueState === "Error"){
oInput.setValueStateText(sValueStateText);
}
return bValidationError;
},请注意,上面的代码并不是高质量和可生产的,因为这是对这个帖子的快速响应:)
https://stackoverflow.com/questions/59824627
复制相似问题