blazor - 如果 EditForm 组件在 Blazor webassembly 中“脏”,如何捕获
问题描述
对于 Blazor Webassembly 中的 EditForm,是否存在与 Angular 中脏表单的概念等效的概念?我想显示一条文本“您已进行更改。任何未保存的更改都将丢失!” 指示用户尚未保存某些内容,并且应在离开前点击提交按钮。
解决方案
是的,有,但我们不使用脏话,我们使用修改或未修改。
EditContext 类提供以下内容:
/// <summary>
/// Determines whether any of the fields in this <see cref="EditContext"/> have been modified.
/// </summary>
/// <returns>True if any of the fields in this <see cref="EditContext"/> have been modified; otherwise false.</returns>
public bool IsModified()
{
// If necessary, we could consider caching the overall "is modified" state and only recomputing
// when there's a call to NotifyFieldModified/NotifyFieldUnmodified
foreach (var state in _fieldStates)
{
if (state.Value.IsModified)
{
return true;
}
}
return false;
}
/// <summary>
/// Determines whether the specified fields in this <see cref="EditContext"/> has been modified.
/// </summary>
/// <returns>True if the field has been modified; otherwise false.</returns>
public bool IsModified(in FieldIdentifier fieldIdentifier)
=> _fieldStates.TryGetValue(fieldIdentifier, out var state)
? state.IsModified
: false;
/// <summary>
/// Determines whether the specified fields in this <see cref="EditContext"/> has been modified.
/// </summary>
/// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
/// <returns>True if the field has been modified; otherwise false.</returns>
public bool IsModified(Expression<Func<object>> accessor)
=> IsModified(FieldIdentifier.Create(accessor));
推荐阅读
- javascript - 对 dom 元素反应 useEffect
- android - 带有连字符的 React Native / Android 换行符不起作用
- python - 检测图像中的椭圆 - opencv python
- paypal - 有没有办法用 PaypalAPI 冻结一笔钱?
- r - 用户定义的函数检查显示不正确输出的数据框中是否存在两个列名
- python - 使用 Python 发送电子邮件时,msg["Subject"] 变量的行为不正常
- mysql - 如何确保在 SQL 输出中仍然表示没有数据的类别
- javascript - 在提前返回之前或之后声明变量?
- c# - 如何更改 Syncfusion 自动完成的字体颜色(在选定行上)
- spring-boot - springboot中的application.properties文件在从另一个文件访问时给出空值