将文本文件加载到浏览器中的表中,可以对其进行编辑,然后替换原始文件



这是通过Blazor服务器应用程序实现的。

我有一个文本文件,看起来像这样:

TEXT00
Some title
TEXT10
8
DATA
110,4,2
110,0,6
110,0,32
110,4,16
110,0,16
110,4,3
110,0,2
...
...

我想完成两件事:

首先,我想把这样一个文件加载到一个可编辑的表中,DATA行下的数字应该放在每个可编辑的单元格中。

插图:

时间长度
11042
11006
110032

要实现这一点,您需要使用绑定元素。例如:

<input @bind="YourProperty" type="text" />

这需要对每一列和每一行进行。最好的解决方案是这样的:

List<WhateverItem> items = new(); // This list should hold all your objects from your text file
public class WhateverItem
{
public string Tempo { get; set; }
public string Length { get; set; }
public string Secs { get; set; }
}

然后是剃刀

@foreach (var item in items) 
{
<tr>
<td>
<input @bind="item.Tempo" type="text" />
</td>
<td>
<input @bind="item.Length" type="text" />
</td>
<td>
<input @bind="item.Secs" type="text" />
</td>
</tr>
}

保存时,需要使用StringBuilder从对象中重新创建内容。

如果要验证字段,可以考虑使用EditForm。请记住,除了字符串之外,您还可以绑定到其他类型。然后您需要更改输入类型。

将文件解析为对象的示例:

List<WhateverItem> items = new();
foreach (string line in System.IO.File.ReadLines(@"c:test.txt"))
{
string[] values = line.Split(',');
if(values.Length == 3)
{
// Keep in mind you can also convert to values right here if you want
items.Add(new WhateverItem
{
Tempo = values[0],
Length = values[1],
Secs = values[2]
});
}
}

最新更新