RadGrid and exporting just the NestedViewTemplate to a PDF

1 Answer 45 Views
Grid
SSirica
Top achievements
Rank 2
Iron
Iron
Veteran
SSirica asked on 28 Apr 2022, 03:30 PM
I have a RadGrid with like 40 columns, so the grid has a NestedViewTemplate that I use to display the selected row in sort of a form/table view.  That all works fine.  What I'm looking to do is to incorporate the ability for the user to export the NestedViewTemplate view to a PDF.  I guess in a way, it's a quick way to generate a single item report.  I've been thru all the articles with the hierarchical stuff, but that's not my scenario, so they were no help.  

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 03 May 2022, 12:24 PM | edited on 03 May 2022, 12:26 PM

Hi SSirica,

You can use the RadClientExportManager component and export only the elements that you specify.

Here is a full example:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="OrderID">
        <NestedViewTemplate>
            <div class="export-me">
                <telerik:RadCard ID="RadCard1" runat="server" Width="200px">
                    <telerik:CardTitleComponent runat="server">
                        OrderID #<%#  Eval("OrderID") %>
                    </telerik:CardTitleComponent>
                    <telerik:CardSubtitleComponent runat="server">
                        Ordered on <%#  Eval("OrderDate") %>
                    </telerik:CardSubtitleComponent>
                    <telerik:CardBodyComponent runat="server">
                        <p><strong>ShipName:</strong> <%#  Eval("ShipName") %></p>
                        <p><strong>ShipName:</strong> <%#  Eval("ShipCountry") %></p>
                    </telerik:CardBodyComponent>
                    <telerik:CardActionsContainerComponent runat="server" CardActionsAlignment="Stretched">
                        <span class="k-button k-flat k-primary exportToPdf">Export to PDF</span>
                    </telerik:CardActionsContainerComponent>
                </telerik:RadCard>
            </div>
        </NestedViewTemplate>
    </MasterTableView>
</telerik:RadGrid>

<telerik:RadClientExportManager runat="server" ID="RadClientExportManager1">
</telerik:RadClientExportManager>

 

C# code for data binding

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = Enumerable.Range(1, 10).Select(x => new
    {
        OrderID = x,
        OrderDate = DateTime.Now.Date.AddDays(x),
        Freight = x * .1,
        ShipName = "Name " + x,
        ShipCountry = "Country " + x
    });
}

 

RadGrid Figure

 

JavaScript to attach the click event handler to the action buttons and to call the ClientExportManager's Export method for a specific element

function pageLoadHandler() {

    $telerik.$('.RadGrid .rgMasterTable .export-me .k-button.exportToPdf').on('click', function (e) {

        var $elementToExport = $telerik.$(this).closest('.export-me');

        var exp = $find("<%= RadClientExportManager1.ClientID %>");
        exp.exportPDF($elementToExport);
    });

    // Sys.Application.remove_load(pageLoadHandler);  
}
Sys.Application.add_load(pageLoadHandler);

 

Some cosmetics to make the Expanded container look better

.RadGrid .rgMasterTable .export-me {
    padding: 10px;
}

 

Export Results

 

 

References:

 

I hope this will help achieve the desired scenario.

 

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
SSirica
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Attila Antal
Telerik team
Share this question
or