Customizing the Close Preview Button in Bold Reports Angular Report Designer
This guide explains how to customize the Close Preview button action in the Bold Reports Angular Report Designer using the previewReport property. By defining a custom function, you can initiate specific actions when the preview is closed, thereby enhancing interactivity and adapting workflows to meet user requirements. This functionality is especially beneficial for dynamic reporting applications developed with Angular.
Project Setup
- Create a new Angular application (
ng new my-report-app) or use an existing one. - Install Bold Reports Angular package:
npm install @boldreports/angular-reporting-components --save - Configure the Report Designer in your Angular module (
app.module.ts):
Snapshot:import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { BoldReportDesignerModule } from '@boldreports/angular-reporting-components'; import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, BoldReportDesignerModule], providers: [], bootstrap: [AppComponent] }) export class AppModule { }app.module.tscustomization code.
- Include jQuery (required by Bold Reports):
Add the following tonpm install jquery --saveangular.jsonunderscripts:"scripts": ["node_modules/jquery/dist/jquery.min.js"] - Navigate to the
src/appfolder to modifyapp.component.tsandapp.component.html.
Steps to Customize the “Close Preview” Button
Step 1: Update app.component.ts
-
Open
src/app/app.component.tsin your code editor. -
Define the
reportControlIdproperty to reference the designer’s ID. -
Add a
serviceUrlproperty for the Bold Reports API. -
Create a
reBindClickmethod to attach an event listener to the Close Preview button. -
Implement a
customActionsmethod for custom logic (e.g., alerts, logging). -
Ensure jQuery is imported for DOM manipulation.
-
Here’s the complete example:
import { Component } from '@angular/core'; declare var $: any; // Declare jQuery @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { reportControlId = 'designer'; serviceUrl = 'https://demos.boldreports.com/services/api/ReportingAPI'; reBindClick(event: any) { const button = document.querySelector(`#${this.reportControlId}_toolbar_btn_closePreview`); if (button) { button.addEventListener('click', (event: any) => { event.preventDefault(); // Prevent default behavior const designer = $(`#${this.reportControlId}`).data('boldReportDesigner'); if (designer) { designer.showDesign(); // Switch to design mode this.customActions(); // Execute custom actions } else { console.error('Report Designer instance not found'); } }); } else { console.error('Close Preview button not found'); } } customActions() { alert('Preview closed. Performing custom actions.'); console.log('Custom actions executed at ' + new Date().toISOString()); // Example: Save state, call API, or update UI } }Snapshot: Code for
app.component.tswith Close Preview customization.
Step 2: Update app.component.html
-
Open
src/app/app.component.htmlin your code editor. -
Add the
<bold-reportdesigner>component withid,serviceUrl, andpreviewReportbindings. -
Set the component’s dimensions for proper rendering.
-
Here is the example code:
<bold-reportdesigner id="designer" [serviceUrl]="serviceUrl" (previewReport)="reBindClick($event)" style="position: absolute; height: 550px; width: 1250px;"> </bold-reportdesigner>Snapshot: Configuration of the Report Designer in
app.component.html.
Step 3: Test the Close Preview Functionality
-
Save all changes to
app.component.tsandapp.component.html. -
Run the Angular application:
ng serve -
Open the application in a browser (default URL is:
http://localhost:4200). -
Load a report in the Report Designer, switch to preview mode, and click the Close Preview button.
-
Verify that:
- The designer returns to design mode.
- The
customActionsfunction executes (e.g., an alert appears). - No errors are present in the browser’s console.
Snapshot: Testing the “Close Preview” button in the Angular application.
Troubleshooting
- Button not found:
- Verify the button’s ID (
#designer_toolbar_btn_closePreview) matches the expected structure. Note that the article’s provided ID (#designer_designButton) is incorrect; use the correct ID for the Close Preview button. - Ensure that
reportControlIdmatches the<bold-reportdesigner>ID (designer).
- Verify the button’s ID (
- Designer instance not found:
- Confirm that jQuery is loaded and the Bold Reports scripts are correctly included.
- Check that the Report Designer version supports the
previewReportproperty (refer to documentation).
- CORS or service URL issues:
- For a demo service URL, confirm internet connectivity and absence of CORS restrictions.
- For local APIs, configure
serviceUrlto a valid Reporting API endpoint with the correct CORS settings.
- TypeScript errors:
- Ensure
declare var $: any;is added to avoid jQuery type errors. - Install
@types/jqueryfor better TypeScript support:npm install @types/jquery --save-dev
- Ensure
- Cross-browser issues:
- Test in different browsers like Chrome, Firefox, and Edge to ensure consistent event listener functionality.
- Use modern event handling (
addEventListener) for compatibility.
Additional Resources
- Sample Application: A sample application is available here for reference.
- Documentation: Link to relevant Bold Reports documentation for further details:
Conclusion
Customizing the Close Preview button in the Bold Reports Angular Report Designer boosts application interactivity by embedding specific functionalities into the reporting workflow. This method enables developers to create dynamic, user-responsive reporting experiences with minimal code modifications.