DHTMLX Pivot. Angular demo example
Demo of DHTMLX Pivot integration with Angular using the official GitHub demo. Shows Angular component initialization and cleanup for a Pivot instance.
View on GitHub - open the repository and click "Open in Codespaces" on the README page to launch a fully configured Angular app with DHTMLX Pivot in seconds. No local setup required.
Integrating third-party JavaScript widgets into Angular applications requires aligning widget initialization with Angular's component lifecycle. DHTMLX Pivot uses imperative DOM manipulation for initialization, which needs to align with Angular's template rendering. In this demo, the widget is initialized after resolving the container element and destroyed when the component is removed.
The demo component uses @ViewChild("container", { static: true }) so the container ElementRef is resolved before ngOnInit(), allowing the Pivot to initialize early in the lifecycle. The component sets ViewEncapsulation.None to ensure DHTMLX Pivot's internal styles are not scoped. Cleanup in ngOnDestroy() calls this._table.destructor() to prevent memory leaks during route navigation.
This example links to the official DHTMLX Pivot Angular demo on GitHub, which demonstrates the recommended integration pattern. You can launch the demo directly in GitHub Codespaces from the repository's README page. The demo covers @ViewChild container binding, ngOnInit() initialization, and cleanup on component destruction.
Solution overview
To integrate DHTMLX Pivot with Angular, follow the standard widget lifecycle pattern:
- Reference the container using
@ViewChild("container", { static: true })to get anElementRef-static: trueresolves the query beforengOnInit(), making the DOM element available early. - Initialize in
ngOnInit()- callnew Pivot(this.pivot_container.nativeElement, config)passingfields,data, and pivot configuration (headerShape,tableShape,configwith rows, columns, values). - Set
ViewEncapsulation.Noneon the component - this ensures DHTMLX Pivot's internal CSS is not scoped to the Angular component, allowing the widget to render correctly. - Clean up in
ngOnDestroy()by callingthis._table.destructor()to remove all event listeners and internal state, preventing memory leaks during route navigation.
Key points
@ViewChildwithstatic: true: Resolves the container element beforengOnInit(), unlikestatic: falsewhich defers resolution untilngAfterViewInit(). This allows initialization earlier in the component lifecycle.- Initialization in
ngOnInit(): Creates the Pivot instance usingthis.pivot_container.nativeElementas the mount point, with configuration for fields, data, header/table shapes, and row/column/value definitions - Mandatory
destructor()inngOnDestroy(): Callsthis._table.destructor()to prevent memory leaks, especially in single-page applications with route navigation where components mount and unmount frequently ViewEncapsulation.None: Required so that DHTMLX Pivot's internal styles apply globally rather than being scoped to the Angular component- GitHub Codespaces support: The official demo repository supports one-click launch via "Open in Codespaces", providing a fully configured development environment without local setup