-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathCsvMapperExample1.razor
More file actions
59 lines (52 loc) · 2.25 KB
/
CsvMapperExample1.razor
File metadata and controls
59 lines (52 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@namespace MudExtensions.Docs.Examples
@inject ISnackbar Snackbar
@using MudExtensions.Utilities
<MudGrid Class="cursor-default">
<MudItem xs="12">
<MudSwitch @bind-Value="_customLocalization" Color="Color.Primary" Label="Custom Localization (German)" />
</MudItem>
<MudItem xs="12">
<MudCsvMapper @ref="_csvMapper" Delimiter="," Class="pa-4" AllowCreateExpectedHeaders="true" NormalizeHeaders="true" ShowIncludeUnmappedData="true" ExpectedHeaders="_expectedFields" OnImported="Upload" LocalizedStrings="GetLocalizedStrings()"></MudCsvMapper>
</MudItem>
</MudGrid>
@code {
MudCsvMapper? _csvMapper;
bool _customLocalization = false;
List<MudExpectedHeader> _expectedFields = new() {
new MudExpectedHeader("Email", true),
new MudExpectedHeader("First Name"),
new MudExpectedHeader("Last Name"),
new MudExpectedHeader("Phone"),
new MudExpectedHeader("Date", false, true),
new MudExpectedHeader("Source", true, true),
};
private async Task<bool> Upload()
{
await Task.Delay(1); // Replace with real world example
var file = _csvMapper?.CsvFile; //IBrowserFile / .csv that was uploaded
byte[]? fileContent = _csvMapper?.FileContentByte; // file with headers updated to correct names
//This is where you do the work with the renamed headers using the the fileContent.
//Send the CSV file to MudTable or to the Database to be parsed
Snackbar.Add($"CSV Mapped!", Severity.Info);
return true;
}
private CsvMapperLocalizedStrings GetLocalizedStrings()
{
if (_customLocalization == false)
{
return new CsvMapperLocalizedStrings();
}
else
{
return new CsvMapperLocalizedStrings()
{
ChooseFile = "Wählen Sie eine .csv-Datei aus",
OrDragAndDrop = " oder Ziehen Sie hier eine CSV-Datei per Drag & Drop.",
ExpectedHeaders = "Erwartete Header",
DragHere = "Hierher ziehen",
Import = "Importieren",
DefineHeaders = "Bitte definieren Sie Ihre MudFieldHeaders, die erwarteten Spalten in Ihrem Code"
};
}
}
}