Passing URL Parameters to your HTML5 Reportviewer - How To

Thread is closed for posting
2 posts, 0 answers
  1. 7C3598C0-4322-4B14-8B97-6221F660DE15
    7C3598C0-4322-4B14-8B97-6221F660DE15 avatar
    6 posts
    Member since:
    Mar 2018

    Posted 29 May 2018 Link to this post

    Requirements

    Telerik Product and Version

    Telerik Reporting 2018 R2

    Supported Browsers and Platforms


    Components/Widgets used (JS frameworks, etc.)



    PROJECT DESCRIPTION 
    (I first posted this as a regular thread under Reporting and after a few inquiries I decided maybe it needed to be in the code library. Original post is here: https://www.telerik.com/forums/passing-url-parameters-to-your-report---how-to#XTddIZ6VLEGagTmuJZaZ_A ) I looked for  a long time for this information. I watched YouTube videos, went to blogs, and contacted Telerik support. I decided when I figured this out I was going to post the code so that someone else would not have to struggle with figuring this out like I did. Support helped and pointed me in the right direction to put this together but no easy example to go by. Below is the code for the HTML5 Report Viewer that I use and seems to work exactly like I wanted. It is javascript that parses the URL string and then is fed to the parameters of your report. The only part of this that you should have to change is the the section that is called "reportSource:" the parameters section. In the example below change the first part "TransMonth: " to the name of the parameter you used in your report and change the name at the end to what your URL parameter is called " getParameterByName('TransMonth') " Mine were named the same for simplicity. See the entire code below. Also some of the js and css files have custom paths in the example below. Adjust yours accordingly. I'm far from an expert at this but I know it took me a while to get this working and I thought if I could save someone else this headache I would. Also there are some attached images of setting your parameters on your datasource and your report. They must all be set for this to work.

     

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Form My Report</title>
        
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
     
        <script src="ReportViewer/js/jquery-1.9.1.min.js"></script>
     
        <link href="ReportViewer/CSS/kendo.common.min.css" rel="stylesheet" />
        <link href="ReportViewer/CSS/kendo.blueopal.min.custom.css" rel="stylesheet" />
     
        <!--kendo.web.min.js or kendo.all.min.js can be used as well instead of the following custom Kendo UI-->
        <script src="ReportViewer/js/telerikReportViewer.kendo-12.0.18.416.min.js"></script>
     
        <script src="ReportViewer/js/telerikReportViewer-12.0.18.416.min.js"></script>
     
      <style>
            #reportViewer1 {
                position: absolute;
                left: 5px;
                right: 5px;
                top: 5px;
                bottom: 5px;
                overflow: hidden;
                font-family: Verdana, Arial;
            }
        </style>
    </head>
    <body>
     
        <div id="reportViewer1">
            loading...
        </div>
     
        <script type="text/javascript">
            function getParameterByName(name, url) {
                if (!url) url = window.location.href;
                name = name.replace(/[\[\]]/g, "\\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                    results = regex.exec(url);
                if (!results) return null;
                if (!results[2]) return '';
                return decodeURIComponent(results[2].replace(/\+/g, " "));
            }
            $(document).ready(function () {
                $("#reportViewer1")
                    .telerik_ReportViewer({
     
                        // The URL of the service which will serve reports.
                        // The URL corresponds to the name of the controller class (ReportsController).
                        // For more information on how to configure the service please check http://www.telerik.com/help/reporting/telerik-reporting-rest-conception.html.
                        serviceUrl: "api/reports",
     
                        // The URL for custom report viewer template. The template can be edited -
                        // new functionalities can be added and unneeded ones can be removed.
                        // For more information please check http://www.telerik.com/help/reporting/html5-report-viewer-templates.html.
                        templateUrl: 'ReportViewer/templates/telerikReportViewerTemplate_custom.html',
     
                        //ReportSource - report description
                        reportSource: {
                            // The report can be set to a report file name (trdx report definition)
                            // or CLR type name (report class definition).
                            report: "ReportApplication.FormMyReport, ReportApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", parameters: {
                                TransMonth: getParameterByName('TransMonth'),
                                TransYear: getParameterByName('TransYear'),
                                PrevMonth: getParameterByName('PrevMonth'),
                                ItemName: getParameterByName('ItemName')
                        },
     
                        // Specifies whether the viewer is in interactive or print preview mode.
                        // PRINT_PREVIEW - Displays the paginated report as if it is printed on paper. Interactivity is not enabled.
                        // INTERACTIVE - Displays the report in its original width and height without paging. Additionally interactivity is enabled.
                        viewMode: telerikReportViewer.ViewModes.INTERACTIVE,
     
                        // Sets the scale mode of the viewer.
                        // Three modes exist currently:
                        // FIT_PAGE - The whole report will fit on the page (will zoom in or out), regardless of its width and height.
                        // FIT_PAGE_WIDTH - The report will be zoomed in or out so that the width of the screen and the width of the report match.
                        // SPECIFIC - Uses the scale to zoom in and out the report.
                        scaleMode: telerikReportViewer.ScaleModes.SPECIFIC,
     
                        // Zoom in and out the report using the scale
                        // 1.0 is equal to 100%, i.e. the original size of the report
                        scale: 1.0,
                        enableAccessibility: false,
     
                        ready: function () {
                            //this.refreshReport();
                        },
                        }
                    });
            });
        </script>

    </body>
    </html>


  2. 5708EBAB-7AB2-409B-AFE9-B7DFAA66AF5C
    5708EBAB-7AB2-409B-AFE9-B7DFAA66AF5C avatar
    1094 posts
    Member since:
    Dec 2022

    Posted 01 Jun 2018 Link to this post

    Hello John,

    Thank you for preparing this great example and sharing it. Your Telerik points are updated.

    Regards,
    Nasko
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.