Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在“设置文本字段值状态”;“sap.ui.model.SimpleType.extend”;_Javascript_Sapui5 - Fatal编程技术网

Javascript 在“设置文本字段值状态”;“sap.ui.model.SimpleType.extend”;

Javascript 在“设置文本字段值状态”;“sap.ui.model.SimpleType.extend”;,javascript,sapui5,Javascript,Sapui5,我正在尝试使用正则表达式为表中的列添加时间验证。首先我添加了一列,请查找下面的代码: oTableFileContents.addColumn( new sap.ui.table.Column("Time") .setTemplate( new sap.ui.commons.TextField({ editable:true, valueState : sap.ui.core.ValueState.Error,

我正在尝试使用正则表达式为表中的列添加时间验证。首先我添加了一列,请查找下面的代码:

oTableFileContents.addColumn(
    new sap.ui.table.Column("Time")
    .setTemplate(
        new sap.ui.commons.TextField({
            editable:true, 
            valueState : sap.ui.core.ValueState.Error, 
            tooltip:""
        })
        .bindProperty("value","Time",new sap.myproject.Time())
    )
    .setLabel(
        new sap.ui.commons.Label({
            text : "Time"
        })
    )
);
在这里,默认情况下,我将每个文本字段的ValueState设置为“Error”,以便每个空白条目都处于错误状态。在分配模型时,我必须将每个有效条目设置为“成功”。因此,我的扩展类定义为:

sap.ui.model.SimpleType.extend("sap.myproject.Time", {
    formatValue: function(oValue) {
        var re = /^([0-1]\d):([0-5]\d)\s?(?:AM|PM)?$/i;
        if(oValue!=null){
            if(re.test(oValue)) {
                // this.setEditable(false);    //ERROR
                //this.setValueState(sap.ui.core.ValueState.Success);   //ERROR
                alert("Success");
            }
        }

        return oValue;
    },

    parseValue: function(oValue) {
        return oValue;
    },

    validateValue: function(oValue) {}
});

但是在上面的代码中,我无法将TextField的状态设置为“Success”。可能是我无法获取对我的文本字段的引用。有人能帮我设置这个值吗?如有任何帮助/建议,将不胜感激。谢谢,Kunal。

请不要尝试从格式化程序中引用UI控件,永远!:-)

只需抛出一个
sap.ui.model.FormatException
,框架就会为您完成剩下的工作:

// ...snip...
formatValue: function(oValue) {
    var re = /^([0-1]\d):([0-5]\d)\s?(?:AM|PM)?$/i;
    if(oValue!=null){
        if(!re.test(oValue)) {
            throw new sap.ui.model.FormatException("Value " + oValue + " is not in a valid Date format");
        }
    }
    //optional, if you don't want empty/null dates
    else {
        throw new sap.ui.model.FormatException("Date cannot be null or empty");
    }

    return oValue;
},
// ...snip...
编辑:为了获得格式或验证错误的视觉线索,您应该附加到控制器的
onInit
事件处理程序中的格式和验证事件处理程序:

sap.ui.getCore().attachFormatError(函数(oEvent){
oEvent.getParameter(“元素”).setValueState(sap.ui.core.ValueState.Error);
});
sap.ui.getCore().attachValidationError(函数(oEvent){
oEvent.getParameter(“元素”).setValueState(sap.ui.core.ValueState.Error);
});
sap.ui.getCore().attachValidationSuccess(函数(oEvent){
oEvent.getParameter(“元素”).setValueState(sap.ui.core.ValueState.None);
});

Hello Robin,感谢您提供详细信息:),但是通过这种方式,我无法显示任何错误或消息。所有细胞看起来都一样。是否有任何方法可以至少突出显示无效单元格或显示工具提示?您好,Kunal,您应该附加到控制器的onInit方法中的格式、解析和验证处理程序,以相应地设置控件的状态。我已经更新了我的答案,罗宾,这正是我想要的。例如,我试图寻找扩展“SimpleType”的方法,但发现很少。希望这对其他人也有用。:)