DHTMLX Pivot. Svelte demo example
Demo of DHTMLX Pivot integration with Svelte using the official GitHub demo. Shows Svelte 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 Svelte app with DHTMLX Pivot in seconds. No local setup required.
Svelte's compile-time reactivity model makes it a natural fit for integrating third-party widgets like DHTMLX Pivot. Unlike virtual-DOM frameworks, Svelte components compile to efficient imperative code, so wrapping an external widget requires careful attention to lifecycle hooks - initialize the pivot when the component mounts and destroy it when the component is removed from the DOM.
The demo component creates a Pivot instance in onMount, passing a container element bound via bind:this as the mount point along with configuration for fields, data, headerShape, tableShape, columnShape, and config (rows, columns, values). The onDestroy hook calls table.destructor() for proper resource management. Props fields and dataset are exported from the component using export let, keeping data management in Svelte's layer.
This example links to the official DHTMLX Pivot Svelte 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 bind:this container binding, onMount initialization, and cleanup on component destruction.
Solution overview
To integrate DHTMLX Pivot with Svelte, follow the standard widget lifecycle pattern:
- Bind the container using
bind:this={container}on a<div>element - Svelte resolves the binding beforeonMountruns, so the DOM element is available for initialization. - Initialize in
onMount- callnew Pivot(container, config)after Svelte renders the component, passingfields,data, and pivot configuration (headerShape,tableShape,columnShape,configwith rows, columns, values). - Pass data via props - the parent
App.svelteprovidesfieldsanddatasetas props, whilePivot.sveltedeclares them with Svelte'sexport letsyntax, keeping data loading separate from the widget lifecycle. - Clean up in
onDestroyby callingtable.destructor()to remove all event listeners and internal state, preventing memory leaks during route navigation.
Key points
bind:thisfor container binding: Svelte's element binding provides a direct DOM reference without wrapper objects, passed directly to thePivotconstructor- Initialization in
onMount: Creates thePivotinstance after the component is first rendered to the DOM, with full configuration for fields, data, header/table/column shapes, and row/column/value definitions - Mandatory
destructor()inonDestroy: Callstable.destructor()to prevent memory leaks, especially in single-page applications with SvelteKit routing where components mount and unmount during navigation - GitHub Codespaces support: The official demo repository supports one-click launch via "Open in Codespaces", providing a fully configured development environment without local setup