Skip to content

Commit c4e5888

Browse files
committed
chore(grid): update export events
1 parent 8bff13d commit c4e5888

File tree

1 file changed

+206
-4
lines changed

1 file changed

+206
-4
lines changed

components/grid/export/events.md

Lines changed: 206 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ To export a hidden Grid column that has its `Visible` parameter set to `false`,
4141

4242
* `Data` - `IEnumerable<object>` - assign a custom collection of data to be exported to Excel, [for example only the selected items in the Grid]({%slug grid-kb-export-selected-rows%}).
4343

44-
* `isCancelled` - `bool` - cancel the OnBeforeExcel event by setting the `isCancelled` property to `true`.
44+
* `isCancelled` - `bool` - cancel the `OnBeforeExcel` event by setting the `isCancelled` property to `true`.
4545

4646
>caption Using the Grid OnBeforeExport with Excel export
4747
@@ -253,9 +253,134 @@ To export a hidden Grid column that has its `Visible` parameter set to `false`,
253253
}
254254
````
255255

256-
### For Pdf Export
256+
### For PDF Export
257+
258+
* `Columns` - `List<GridPdfExportColumn>` - a collection of all exportable columns in the Grid. These are all visible `GridColumn` instances. You can customize the following attributes of the Grid column before exporting it into PDF:
259+
260+
* `Width` - define the width of the column **in pixels**.
261+
* `Title` - define the column title to be shown in the Excel file header.
262+
* `NumberFormat` - provide an PDF-compatible number/date format
263+
* `Field` - set the data bound field of the column.
264+
265+
To export a hidden Grid column that has its `Visible` parameter set to `false`, you can manually define an instance of the `GridPdfExportColumn` in the handler for the `OnBeforeExport` event and add that column to the `args.Columns` collection.
266+
267+
268+
* `Data` - `IEnumerable<object>` - assign a custom collection of data to be exported to Excel, [for example only the selected items in the Grid]({%slug grid-kb-export-selected-rows%}).
269+
270+
* `isCancelled` - `bool` - cancel the `OnBeforeExcel` event by setting the `isCancelled` property to `true`.
271+
272+
>caption Using the Grid OnBeforeExport with PDF export
273+
274+
````RAZOR
275+
@* This example shows the capabilities of the OnBeforeExport event when exporting the Grid to PDF. *@
276+
277+
@* Required by BuiltInNumberFormats in the OnBeforePDFExport handler *@
278+
@using Telerik.Documents.SpreadsheetStreaming
279+
280+
@* Required by GridPdfExportColumn in the OnBeforePDFExport handler *@
281+
@using Telerik.Blazor.Components.Grid
282+
283+
<TelerikGrid Data="@GridData"
284+
Pageable="true"
285+
Sortable="true"
286+
@bind-SelectedItems="@SelectedItems"
287+
SelectionMode="@GridSelectionMode.Multiple"
288+
Resizable="true"
289+
Reorderable="true"
290+
FilterMode="@GridFilterMode.FilterRow"
291+
Groupable="true"
292+
Width="700px">
293+
294+
<GridToolBarTemplate>
295+
<GridCommandButton Command="PdfExport" Icon="@SvgIcon.FilePdf">Export to PDF</GridCommandButton>
296+
<label class="k-checkbox-label"><TelerikCheckBox @bind-Value="@ExportAllPages" />Export All Pages</label>
297+
<label class="k-checkbox-label"><TelerikCheckBox @bind-Value="@ExportSelectedItemsOnly" />Export Selected Items Only</label>
298+
</GridToolBarTemplate>
299+
300+
<GridExport>
301+
<GridPdfExport FileName="telerik-grid-export" AllPages="@ExportAllPages" OnBeforeExport="@OnBeforePDFExport" />
302+
</GridExport>
303+
304+
<GridColumns>
305+
<GridColumn Field="@nameof(SampleData.ProductId)" Title="ID" Visible="false" />
306+
<GridColumn Field="@nameof(SampleData.ProductName)" Title="Product Name" Width="150px" />
307+
<GridColumn Field="@nameof(SampleData.UnitsInStock)" Title="In stock" Width="100px" />
308+
<GridColumn Field="@nameof(SampleData.Price)" Title="Unit Price" Width="100px" />
309+
<GridColumn Field="@nameof(SampleData.ReleaseDate)" Title="Release Date" Width="200px" />
310+
</GridColumns>
311+
</TelerikGrid>
312+
313+
@code {
314+
private List<SampleData> GridData { get; set; } = new();
257315
258-
xzxz
316+
private IEnumerable<object> SelectedItems = Enumerable.Empty<object>();
317+
318+
private bool ExportAllPages { get; set; }
319+
320+
private bool ExportSelectedItemsOnly { get; set; } = true;
321+
322+
private void OnBeforePDFExport(GridBeforePdfExportEventArgs args)
323+
{
324+
// Export the hidden ProductId column that has Visible="false"
325+
var exportableHiddenColumn = new GridPdfExportColumn()
326+
{
327+
Title = "Product Id",
328+
Field = nameof(SampleData.ProductId)
329+
};
330+
args.Columns.Insert(0, exportableHiddenColumn);
331+
332+
// Customize the Width of the first exported column
333+
args.Columns[0].Width = "100px";
334+
335+
// Customize the Title of the first exported column
336+
args.Columns[0].Title = "Product Id";
337+
338+
// Change the format of the Price column
339+
// BuiltInNumberFormats is part of the Telerik.Documents.SpreadsheetStreaming namespace
340+
args.Columns[3].NumberFormat = BuiltInNumberFormats.GetCurrency2();
341+
342+
// Change the format of the ReleaseDate column
343+
args.Columns[4].NumberFormat = BuiltInNumberFormats.GetShortDate();
344+
345+
// Export only the first 4 columns in the Grid
346+
//args.Columns = (args.Columns.Take(4)).ToList();
347+
348+
if (ExportSelectedItemsOnly)
349+
{
350+
// Export only the SelectedItems instead of the Grid data
351+
args.Data = SelectedItems;
352+
}
353+
354+
// Set IsCancelled to true if you want to prevent exporting
355+
//args.IsCancelled = false;
356+
}
357+
358+
protected override void OnInitialized()
359+
{
360+
GridData = Enumerable.Range(1, 50).Select(x => new SampleData
361+
{
362+
ProductId = x,
363+
ProductName = $"Product {x}",
364+
UnitsInStock = x * 2,
365+
Price = 3.14159m * x,
366+
Discontinued = x % 4 == 0,
367+
ReleaseDate = DateTime.Now.AddDays(-x)
368+
}).ToList();
369+
370+
SelectedItems = GridData.Take(5);
371+
}
372+
373+
public class SampleData
374+
{
375+
public int ProductId { get; set; }
376+
public string ProductName { get; set; }
377+
public int UnitsInStock { get; set; }
378+
public decimal Price { get; set; }
379+
public bool Discontinued { get; set; }
380+
public DateTime ReleaseDate { get; set; }
381+
}
382+
}
383+
````
259384

260385
## OnAfterExport
261386

@@ -413,12 +538,89 @@ The `OnAfterExport` event fires after [OnBeforeExport](#onbeforeexport) and befo
413538

414539
### For Pdf Export
415540

416-
xzxz
541+
* `Stream` - `MemoryStream` - The output of the PDF export as a memory stream. The stream itself is finalized, so that the resource does not leak. To read and work with the stream, clone its available binary data to a new `MemoryStream` instance.
542+
543+
````RAZOR
544+
@* Get the output of the PDF export as a MemoryStream *@
545+
546+
@using System.IO
547+
548+
<TelerikGrid Data="@GridData"
549+
Pageable="true"
550+
Sortable="true"
551+
@bind-SelectedItems="@SelectedItems"
552+
SelectionMode="@GridSelectionMode.Multiple"
553+
Resizable="true"
554+
Reorderable="true"
555+
FilterMode="@GridFilterMode.FilterMenu"
556+
Groupable="true"
557+
Width="700px">
558+
559+
<GridToolBarTemplate>
560+
<GridCommandButton Command="PdfExport" Icon="@SvgIcon.FilePdf">Export to PDF</GridCommandButton>
561+
<label class="k-checkbox-label"><TelerikCheckBox @bind-Value="@ExportAllPages" />Export All Pages</label>
562+
</GridToolBarTemplate>
563+
564+
<GridExport>
565+
<GridPdfExport FileName="telerik-grid-export"
566+
AllPages="@ExportAllPages"
567+
OnAfterExport="@OnAfterPDFExport" />
568+
</GridExport>
569+
570+
<GridColumns>
571+
<GridColumn Field="@nameof(SampleData.ProductId)" Title="ID" Width="50px" />
572+
<GridColumn Field="@nameof(SampleData.ProductName)" Title="Product Name" Width="150px" />
573+
<GridColumn Field="@nameof(SampleData.UnitsInStock)" Title="In stock" Width="100px" />
574+
<GridColumn Field="@nameof(SampleData.Price)" Title="Unit Price" Width="100px" />
575+
<GridColumn Field="@nameof(SampleData.ReleaseDate)" Title="Release Date" Width="200px" />
576+
</GridColumns>
577+
</TelerikGrid>
578+
579+
@code {
580+
private async Task OnAfterPDFExport(GridAfterPdfExportEventArgs args)
581+
{
582+
var bytes = args.Stream.ToArray();
583+
var pdfStream = new MemoryStream(bytes);
584+
}
585+
586+
private MemoryStream pdfStream { get; set; }
587+
588+
private IEnumerable<object> SelectedItems = Enumerable.Empty<object>();
589+
590+
private List<SampleData> GridData { get; set; }
591+
592+
private bool ExportAllPages { get; set; }
593+
594+
protected override void OnInitialized()
595+
{
596+
GridData = Enumerable.Range(1, 100).Select(x => new SampleData
597+
{
598+
ProductId = x,
599+
ProductName = $"Product {x}",
600+
UnitsInStock = x * 2,
601+
Price = 3.14159m * x,
602+
Discontinued = x % 4 == 0,
603+
ReleaseDate = DateTime.Now.AddDays(-x)
604+
}).ToList();
605+
}
606+
607+
public class SampleData
608+
{
609+
public int ProductId { get; set; }
610+
public string ProductName { get; set; }
611+
public int UnitsInStock { get; set; }
612+
public decimal Price { get; set; }
613+
public bool Discontinued { get; set; }
614+
public DateTime ReleaseDate { get; set; }
615+
}
616+
}
617+
````
417618

418619
## See Also
419620

420621
* [Grid Excel Export]({%slug grid-export-excel%})
421622
* [Grid CSV Export]({%slug grid-export-csv%})
623+
* [Grid PDF Export](slug:grid-export-pdf)
422624
* [Custom cell formatting of the exported file with RadSpreadProcessing]({%slug grid-kb-custom-cell-formatting-with-radspreadprocessing%})
423625
* [Custom cell formatting of the exported file with RadSpreadStreamProcessing]({%slug grid-kb-custom-cell-formatting-with-radspreadstreamprocessing%})
424626
* [Format numbers and dates in the exported CSV file from the Grid]({%slug grid-kb-number-formatting-of-the-csv-export%})

0 commit comments

Comments
 (0)