Blazor 渲染当前语言区小数点格式对应编辑组件
环境:
使用西班牙语文化,
组件: 表格
显示格式对的, 小数点西班牙这边是逗号(,) , 0.99 显示为 0,99
重现
编辑商品, 显示变成了0.99而不是正确的区域格式 0,99, 直接保存没事的
如果手动改了一下数字为 0.99 只要焦点变化了 就会格式化为 99 , 这个时候保存数据就出问题了
经过查询, 是 < input type="number" > 的限制性
所以, 渲染为文本框是最佳的办法了:
[AutoGenerateColumn(FormatString = "N2", Step = "0.01", Align = Alignment.Right, ComponentType = typeof(BootstrapInput
目前问题解决.
思考:
是不是可以做一个自动的机制, 非点号(.) 数字格式区域, 数字类型编辑自动渲染为文本框, 而不是数字框?
伪代码
/// <summary>
/// 通过指定类型生成组件类型
/// </summary>
/// <param name="item"></param>
private static Type GenerateComponentType(IEditorItem item)
{
...
string numberDecimalSeparator = CultureInfo.CurrentCulture?.NumberFormat?.NumberDecimalSeparator ?? ".";
...
else if (fieldType.IsNumber() && numberDecimalSeparator == ".")
{
ret = typeof(BootstrapInputNumber<>).MakeGenericType(fieldType);
}
...
}
后续:
经浏览器语言设定验证不过关,最终版本采用全部渲染为文本框解决问题
最终执行方案:
弄了个通用处理方法, 表格组件创建列的时候检查条件, 动态设置为对应的类型
/// <summary>
/// 获得/设置 列创建时渲染带小数格式字段为文本框组件, 默认 true
/// </summary>
[Parameter]
[NotNull]
public bool AutoRenderComponentWithLocaleFormat { get; set; } = true;
if (AutoRenderComponentWithLocaleFormat && OnColumnCreating == null)
{
OnColumnCreating += AutoRenderComponentLocaleFormat;
}
/// <summary>
/// 实现自动渲染组件类型
/// </summary>
/// <param name="columns"></param>
/// <returns></returns>
public static Task AutoRenderComponentLocaleFormat(List<ITableColumn> columns)
{
//经浏览器语言设定验证不过关,最终版本采用全部渲染为文本框解决问题
//if (NumberDecimalSeparator == ".")
//{
// return Task.CompletedTask;
//}
var items = columns.Where(i => i.ComponentType == null && IsNumberWithDecimalSeparator(i.PropertyType));
foreach (var item in items)
{
var type = Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType;
if (Nullable.GetUnderlyingType(item.PropertyType)!=null)
{
if (type == typeof(float))
{
item.ComponentType = typeof(BootstrapInput<float?>);
}
else if (type == typeof(double))
{
item.ComponentType = typeof(BootstrapInput<double?>);
}
else if (type == typeof(decimal))
{
item.ComponentType = typeof(BootstrapInput<decimal?>);
}
}
else
{
if (type == typeof(float))
{
item.ComponentType = typeof(BootstrapInput<float>);
}
else if (type == typeof(double))
{
item.ComponentType = typeof(BootstrapInput<double>);
}
else if (type == typeof(decimal))
{
item.ComponentType = typeof(BootstrapInput<decimal>);
}
}
}
return Task.CompletedTask;
}
后记
多多少少还是有不完美,取舍input number要根据下面资料自己决定。
小数基点格式
句点
以下国家/地区以句点(.)来表示基点:
澳洲、文莱、博茨瓦纳、加拿大(英语)、香港、澳门(中文)、中国[1]大陆地区、印度、爱尔兰、以色列、大韩民国、朝鲜民主主义人民共和国、马来西亚、墨西哥、新西兰、尼日利亚、巴基斯坦、菲律宾、新加坡、斯里兰卡、中华民国、泰国、英国、美国
逗号
以下国家/地区以逗号(,)来表示基点:
阿尔巴尼亚、安道尔、阿根廷、奥地利、阿塞拜疆、白俄罗斯、比利时、玻利维亚、波斯尼亚和黑塞哥维那、巴西、保加利亚、喀麦隆、加拿大(法语)、哥斯达黎加、克罗地亚、古巴、智利、哥伦比亚、塞浦路斯、捷克、丹麦、多米尼加共和国、厄瓜多尔、萨尔瓦多、爱沙尼亚、法罗、芬兰、法国、德国、希腊、格陵兰、危地马拉、洪都拉斯、匈牙利、印度尼西亚、冰岛、意大利、拉脱维亚、立陶宛、马其顿共和国、摩尔多瓦、荷兰、挪威、尼加拉瓜、巴拿马、巴拉圭、秘鲁、波兰、葡萄牙、罗马尼亚、俄国、塞尔维亚、斯洛伐克、南非、斯洛文尼亚、西班牙、瑞典、瑞士、土耳其、乌克兰、乌拉圭、委内瑞拉、津巴布韦、澳门(葡文)、越南
关联项目
FreeSql QQ群:4336577
BA & Blazor QQ群:795206915
Maui Blazor 中文社区 QQ群:645660665
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow(包含链接: https://github.com/densen2014 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
转载声明
本文来自博客园,作者:周创琳 AlexChow,转载请注明原文链接:https://www.cnblogs.com/densen2014/p/18638321
AlexChow
今日头条 | 博客园 | 知乎 | Gitee | GitHub