-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.razor
More file actions
105 lines (93 loc) · 3.45 KB
/
Copy pathIndex.razor
File metadata and controls
105 lines (93 loc) · 3.45 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
@page "/"
@rendermode InteractiveServer
@inject IJSRuntime JS
@using DevExpress.XtraReports.UI
@using GridMasterDetailExport.Data
@using GridMasterDetailExport.Reports
<DxGrid @ref="MasterGrid"
AllowColumnReorder="false"
AllowSort="false"
Data="DataProvider.GetUsers()"
KeyFieldName="UserID">
<ToolbarTemplate Context="ctx">
<DxToolbar>
<Items>
<DxToolbarItem Text="Export as PDF" Click="ExportToPdf" />
<DxToolbarItem Text="Export as XLSX" Click="ExportToXlsx" />
<DxToolbarItem Text="Export as CSV" Click="ExportToCsv" />
</Items>
</DxToolbar>
</ToolbarTemplate>
<Columns>
<DxGridDataColumn FieldName="UserID" />
<DxGridDataColumn FieldName="UserName" />
</Columns>
<DetailRowTemplate Context="ctx">
@{
User user = ctx.DataItem as User ?? new();
var userOrders =
DataProvider.GetOrders()
.Where(o => o.UserID == user.UserID);
}
<DxGrid @ref="DetailGrid"
AllowColumnReorder="false"
AllowSort="false"
Data="userOrders"
KeyFieldName="OrderID">
<Columns>
<DxGridDataColumn FieldName="OrderID" />
<DxGridDataColumn FieldName="OrderDate" />
<DxGridDataColumn FieldName="ProductName" />
</Columns>
</DxGrid>
</DetailRowTemplate>
</DxGrid>
@code {
public DxGrid? MasterGrid { get; set; }
public DxGrid? DetailGrid { get; set; }
private async Task ExportToPdf() {
using var report = GetReport();
using var stream = new MemoryStream();
await report.ExportToPdfAsync(stream);
await DownloadStreamAsync(stream, "exportResult.pdf");
}
private async Task ExportToXlsx() {
using var report = GetReport();
using var stream = new MemoryStream();
await report.ExportToXlsxAsync(stream);
await DownloadStreamAsync(stream, "exportResult.xlsx");
}
private async Task ExportToCsv() {
using var report = GetReport();
using var stream = new MemoryStream();
await report.ExportToCsvAsync(stream);
await DownloadStreamAsync(stream, "exportResult.csv");
}
private XtraReport GetReport() {
return ReportGenerationHelpers.GetReportFromDxGrid(
GetVisibleColumnNames(MasterGrid),
GetVisibleColumnNames(DetailGrid),
GetRowsExpandedStates()
);
}
private static string[] GetVisibleColumnNames(IGrid? grid) {
return grid?.GetVisibleColumns()
.OfType<DxGridDataColumn>()
.Select(c => c.FieldName ?? c.Name).ToArray() ?? [];
}
private Dictionary<string, bool> GetRowsExpandedStates() {
Dictionary<string, bool> masterRowExpandedStates = new();
for(int i = 0; i < MasterGrid?.GetVisibleRowCount(); i++) {
if(MasterGrid.GetDataItem(i) is User user) {
masterRowExpandedStates[user.UserID.ToString()]
= MasterGrid.IsDetailRowExpanded(i);
}
}
return masterRowExpandedStates;
}
async Task DownloadStreamAsync(Stream stream, string fileName) {
stream.Seek(0, SeekOrigin.Begin);
using var streamRef = new DotNetStreamReference(stream);
await JS.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
}
}