How to Add JavaScript Subreports to Your Web Apps
Tags: webdev, devops, javascript, tutorial
Published: 2024-02-02 19:22:25

---
canonical_url: https://developer.mescius.com/blogs/how-to-add-javascript-subreports-to-your-web-apps
description: Learn how to add JavaScript subreports to your web apps. See more from ActiveReportsJS today.
---
The ability to implement subreports allows report authors to create independent, self-contained report components that represent specific data sets or analytical insights. These modular subreports can be easily integrated into various master reports, avoiding redundancy and ensuring consistency across all of your operation’s reporting. This approach, modeled after reusable components, streamlines the development process, allowing for efficient updates, modifications, and expansions, contributing to a more scalable reporting infrastructure.

Thankfully, the ActiveReportsJS JavaScript Report Designer makes it easy for your report authors to build subreports and bind them to master reports. Along with subreports, ActiveReportsJS supports the use of nested subreports, increasing your ability to create a more modular report structure.

This article will show how you can use ActiveReportsJS to create nested subreports to display a list of cars for sale. We’ll be covering the following topics:

*   Creating a Subreport
*   Implementing a Subreport in Another Report
*   Loading Nested Subreports into a Master Report
*   Viewing the Master Report in a JavaScript Application

This article assumes that you know the basics of adding data sources and controls to a report. For more information on getting started with ActiveReportsJS, check out our [<span>Getting Started</span>](https://developer.mescius.com/activereportsjs/docs/v4.2/ReportAuthorGuide/QuickStart/Get-Started-With-Designer-App "https://developer.mescius.com/activereportsjs/docs/v4.2/ReportAuthorGuide/QuickStart/Get-Started-With-Designer-App") page.

If you’d like to follow along, you can get the completed reports and project [<span>here</span>](https://github.com/joel-p-parks/ARJS-Subreports-JavaScript "https://github.com/joel-p-parks/ARJS-Subreports-JavaScript").

## Creating a Subreport

The first thing to do is create the first subreport. Subreports are simply ActiveReportsJS reports that are loaded into other reports, so the process for creating them is the same as a standard report: load some data, add controls to the report, and bind the data to the controls.

For this report, we will use some basic data about car manufacturers. Here’s a snippet of what the data looks like:

```
    {
      "manufacturerId": 962,
      "manufacturerName": "Toyota Motor North America",
      "address": "6565 Headquarters Drive",
      "city": "Washington",
      "state": "Texas",
      "postalCode": "75024",
      "country": "United States",
      "email": "contact@toyota.com",
      "phone": "757-254-2810",
      "fax": "1-408-999 7777"
    },
```

The entirety of the data is available in the **Manufacturers.json** file, which we’ll be embedding in the report as our data source:

![Manufacturer Data Source](//cdn.mescius.io/umb/media/dcun45ve/manufacturer_data_source.png)

Then, we’re going to use the data source we just created to add our data set:

![Manufacturer Data Set](//cdn.mescius.io/umb/media/tbsnzmt4/manufacturer_data_set.png)

Now that we’ve got some data to work with, it’s time to add a control to the report that we can use to display the data.

Drag a **Table** control onto the report area; we will make some modifications so that the table layout fits our needs. First, delete the header and footer rows and add three more detail rows and one more column. Then, merge all of the cells of the first row. When complete, your table should look something like this:

![Manufacturer Table Empty](//cdn.mescius.io/umb/media/rzbfjlzl/manufacturer_table_empty.png)

Now, we’re going to populate our table with a combination of references to our data, as well as a few icons:

![Manufacturer Table Full](//cdn.mescius.io/umb/media/ibhawovc/manufacturer_table_full.png)

To load the icons, you simply drag and drop an **Image** control into the desired cell and select an image from your machine to embed into the report.

Now, if we preview the report, we should see our data being displayed:

![Manufacturer Table Error](//cdn.mescius.io/umb/media/csjlgrvj/manufacturer_info_table_error.png)

Now, one more thing we need to do before our report can be properly utilized as a subreport: add parameters.

Parameters are a way for report authors to allow users to make modifications at runtime; in this case, we’re modifying what data this subreport will display based on the value of the parameter passed to it.

For this report, we’re going to be adding a single parameter, which we will call **ManufacturerId**:

![Manufacturer Parameter](//cdn.mescius.io/umb/media/bzlopd5k/manufacturer_parameter.png)

As you can see, we’re setting the **Hidden** parameter to **True** and providing a default value to the parameter. By hiding the parameter, we can pass this value between master reports and subreports in the background. Setting a default value of **1002** will allow us to test to ensure that the parameter is working because when we preview the report, the only data that should display is the data relating to the manufacturer with that ID.

The final step in implementing the parameter in this report is to add a filter to the table. This filter will make sure that we’re only displaying the data with matching manufacturer IDs:

![Manufacturer Table Filter](//cdn.mescius.io/umb/media/3kokxkzt/manufacturer_table_filter.png)

Now, when we preview the report, we’ll only see the manufacturer with the ID that matches the default value that we set in the parameter:

![Manufacturer Table Final](//cdn.mescius.io/umb/media/ujuawzlz/manufacturer_table_final.png)

And with that, we’ve created our first subreport! Suppose another report we create has access to manufacturer IDs. In that case, we can load this subreport into that report instead of recreating this report again, increasing the modularity of our reporting infrastructure.

In the next section, we will show how you can load this subreport into another report.

## Implementing a Subreport in Another Report

Now that we’ve created a subreport, it’s time to create another report that can be loaded into. For this report, we will display some basic information relating to a vehicle from each of the manufacturers. Here is a snippet of what the data looks like:

```
    {
      "bodyClass": "Wagon",
      "make": "TOYOTA",
      "model": "FJ Cruiser",
      "modelYear": "2008",
      "engineModel": "1GR-FE",
      "vin": "jtezu11f88k007763",
      "manufacturerId": "962"
    },
```

As you can see, this data also has a **manufacturerId** property, which will be used to determine which data to display in the subreport.

The entirety of the data is available in the **Vehicles.json** file, which we’ll be embedding in the report as our data source:

![Vehicles Data Source](//cdn.mescius.io/umb/media/k1rpgts0/vehicles_data_source.png)

Then, we’re going to use the data source we just created to add our data set:

![Vehicles Data Set](//cdn.mescius.io/umb/media/nginfvi4/vehicles_data_set.png)

Now that we’ve got some data to work with, it’s time to add a control to the report that we can use to display the data.

Drag a **Table** control onto the report area; we will make some modifications so that the table layout fits our needs. First, delete the header and footer rows and add five more detail rows. Then, merge the first two cells in the first row and cells two through six in the last column. When complete, your table should look something like this:

![Vehicles Table Empty](//cdn.mescius.io/umb/media/bfphmeg0/vehicles_table_empty.png?rmode=max&width=705&height=232)

Now, we’re going to populate our table with data from the data set, as well as apply some styling to the table:

![Vehicles Table Partial](//cdn.mescius.io/umb/media/yv4h3mbv/vehicles_table_partial.png?rmode=max&width=691&height=221)

To complete the table, we will add our subreport to the large, empty cell. Drag and drop a **Subreport** control into the cell.

In the properties panel, we will set the **Report Name** and **Report Parameters** properties. For Report Name, we will use the report that we previously created: **Manufacturers.rdlx-json**. Then, for the Report Parameters, we will set the **NAME** to **ManufacturerId** and **VALUE** to **{manufacturerId}**.

![Vehicles Subreport Parameters](//cdn.mescius.io/umb/media/ke1bq2ll/vehicles_subreport_parameters.png)

Two things to note: the first is that, when binding a subreport, the subreport must be in the same location as the report it is being loaded into. If your report is not showing in the Report Name dropdown, try saving your report.

Second, we need to be sure, when adding a Report Parameter, that the NAME value that we give matches the name of the parameter that we created in the subreport; in this case, ManufacturerId.

Now, the table should look something like this:

![Vehicles Table Full](//cdn.mescius.io/umb/media/4y5bsf1z/vehicles_table_full.png?rmode=max&width=675&height=219)

And when we preview the report, we should see the following:

![Vehicle Info Table Error](//cdn.mescius.io/umb/media/bt2dxvnm/vehicle_info_table_error.png)

And with that, we’ve added a subreport to another report! We’re not done, though. With ActiveReportsJS, you can nest subreports in a master report, and that’s exactly what we will do.

Like with the first subreport, we will need to add a parameter to the report and a filter to the table to set this report up to be used as a subreport. First, we’re going to add a parameter named **VIN**:

![Vehicles Parameters](//cdn.mescius.io/umb/media/qcwdc5hc/vehicles_parameter.png)

Like before, we’re setting the **Hidden** property to **True** and providing a default value for the parameter.

The final step in implementing the parameter in this report is to add a filter to the table. This filter will make sure that we’re only displaying the data with matching VINs:

![Vehicles Table Filter](//cdn.mescius.io/umb/media/5cxpokul/vehicles_table_filter.png)

Now, when we preview the report, we’ll only see the vehicles with the VIN that matches the default value that we set in the parameter:

![Vehicles Table Final](//cdn.mescius.io/umb/media/p3vkuwvb/vehicles_table_final.png)

Now, not only have we implemented a subreport in this report, but we’ve made it capable of being used as a subreport itself! In the next section, we’ll add these nested subreports into a master report.

## Loading Nested Subreports into a Master Report

With our nested subreports created, we can load them into a master report and set everything up to dynamically display all of our data. For this report, we will be displaying information on each of the car listings. Here is a snippet of what the data looks like:

```
    {
      "price": 6300,
      "mileage": 274117,
      "color": "blue",
      "vin": "jtezu11f88k007763",
      "condition": "Grade 3"
    },
```

As you can see, this data also has a **VIN** property, which will be used to determine which data to display in the nested subreport.

The entirety of the data is available in the **CarListings.json** file, which we’ll be embedding in the report as our data source:

![Listings Data Source](//cdn.mescius.io/umb/media/r4ipi0f5/listings_data_source.png?rmode=max&width=557&height=474)

Then, we’re going to use the data source we just created to add our data set:

![Listings Data Set](//cdn.mescius.io/umb/media/mddd51dy/listings_data_set.png?rmode=max&width=546&height=414)

Now, before we start loading data into components, since this is the master report, we will add a **Page Header** to it. For this report, we’re going to add a **TextBox** and **Image** control to the header:

![Listings Header](//cdn.mescius.io/umb/media/z5ufalc1/listings_header.png?rmode=max&width=716&height=112)

Now the page header is set up, and we’ve got some data to work with, it’s time to add a control to the report that we can use to display the data.

Drag a **Table** control onto the report area; we will make some modifications so that the table layout fits our needs. First, delete the header and footer rows and add three more detail rows and one more column. Then, merge all of the cells in the second row. When complete, your table should look something like this:

![Listings Table Empty](//cdn.mescius.io/umb/media/c0xewwed/listings_table_empty.png?rmode=max&width=727&height=228)

Now, we’re going to populate our table with data from the data set, as well as apply some styling to the table:

![Listings Table Partial](//cdn.mescius.io/umb/media/m4vlgpxn/listings_table_partial.png?rmode=max&width=735&height=208)

To complete the table, we will add our subreport to the large, empty cell. Drag and drop a **Subreport** control into the cell.

In the properties panel, we will set the **Report Name** and **Report Parameters** properties. For Report Name, we will use the report we previously created: **Vehicles.rdlx-json**. Then, for the Report Parameters, we will set the **NAME** to **VIN** and **VALUE** to **{vin}**.

![Listings Subreport Parameters](//cdn.mescius.io/umb/media/imqi3cn5/listings_subreport_parameters.png)

Now, the table should look something like this:

![Listings Table Full](//cdn.mescius.io/umb/media/4n3jsnan/listings_table_full.png?rmode=max&width=726&height=207)

And if we preview the report, we’ll see the data from this report, as well as all of our data, correctly displayed with the associated content from the nested subreports:

![Listings Table Final](//cdn.mescius.io/umb/media/ib0geu2e/listings_table_final.png)

Now that we have our master report, the next section will show how you can view it in a JavaScript application.

## Viewing the Master Report in a JavaScript Application

Now that our reports have been set up, we must load them into a JavaScript application. ActiveReportsJS provides a JavaScript Report Viewer, which can be easily loaded into your application to display the master report.

First, we need to add the ActiveReportsJS library to our application. For this, I’ll be using the CDN. Simply add the following **<script>** tags to your **<head>** tag:

```
    <link rel="stylesheet" href="https://cdn.mescius.com/activereportsjs/4.latest/styles/ar-js-ui.css" type="text/css" />
    <link rel="stylesheet" href="https://cdn.mescius.com/activereportsjs/4.latest/styles/ar-js-viewer.css" type="text/css" />
    <script src="https://cdn.mescius.com/activereportsjs/4.latest/dist/ar-js-core.js"></script>
    <script src="https://cdn.mescius.com/activereportsjs/4.latest/dist/ar-js-viewer.js"></script>
    <script src="https://cdn.mescius.com/activereportsjs/4.latest/dist/ar-js-pdf.js"></script>
    <script src="https://cdn.mescius.com/activereportsjs/4.latest/dist/ar-js-tabular-data.js"></script>
    <script src="https://cdn.mescius.com/activereportsjs/4.latest/dist/ar-js-html.js"></script>
```

This will load ActiveReportsJS’s CSS files, as well as all of the required JavaScript files for the JavaScript Report Viewer and its export settings.

Next, beneath the **<script>** tags add the following:

```
    <style>
      #viewer-host {
        width: 100%;
        height: 100vh;
      }
    </style>
```

And inside of the **<body>** tags, add the following markup:

```
    <div id="viewer-host"></div>
```

This creates a div with a large area for the JavaScript Report Viewer to sit in.

Now, it's time to add the Report Viewer control to the application. Add the following beneath the **<body>** tags:

```
    <script>
      var viewer = new ActiveReports.Viewer(#viewer-host);
      viewer.open(CarListings.rdlx-json);
    </script>
```

This will load the control into the div we created and open the CarListings.rdlx-json report we created in the previous step. Be sure that the report files are in the same location as the HTML file loading them or that you are properly denoting the path to your report. You will need to ensure that your subreports are also there.

Now, we can run the application and see the master report in the browser. We’ll use the Live Server plugin for Visual Studio Code to run our application. When the application boots up, if everything has been set up correctly, you should see the following:

![Report Viewer](//cdn.mescius.io/umb/media/lqkduaub/report_viewer.png?rmode=max&width=721&height=415)

## Conclusion

And with that, we’ve come to the end of the article. We’ve covered a lot of topics in this piece, from creating and binding subreports to nesting them in a master report and even loading that into a JavaScript Report Viewer.

Hopefully, this article has shown you the benefits of using subreports: by breaking apart your reports into subreports, you increase the modularity of your report infrastructure, allowing you to spend less time on report creation.

If you’d like to download the reports, data, and applications created in this article, you can find them [<span>here</span>](https://github.com/joel-p-parks/ARJS-Subreports-JavaScript "https://github.com/joel-p-parks/ARJS-Subreports-JavaScript").

Happy coding!