Integration with Svelte
You should be familiar with the basic concepts and patterns of Svelte before reading this documentation. To refresh your knowledge, please refer to the Svelte documentation.
DHTMLX Pivot is compatible with Svelte. We have prepared code examples on how to use DHTMLX Pivot with Svelte. For more information, refer to the corresponding Example on GitHub.
Creating a project
To create a Svelte JS project, run the following command:
npm create vite@latest
Let's name the project as my-svelte-pivot-app.
Installation of dependencies
Go to the app directory:
cd my-svelte-pivot-app
Install dependencies and start the dev server. For this, use a package manager:
- if you use yarn, run the following commands:
yarn
yarn start // or yarn dev
- if you use npm, run the following commands:
npm install
npm run dev
The app should run on a localhost (for instance http://localhost:3000
).
Creating Pivot
Now you should get the DHTMLX Pivot source code. First of all, stop the app and proceed with installing the Pivot package.
Step 1. Package installation
Download the trial Pivot package and follow steps mentioned in the README file. Note that trial Pivot is available 30 days only.
Step 2. Component creation
Now you need to create a Svelte component, to add Pivot into the application. Let's create a new file in the src/ directory and name it Pivot.svelte.
Import source files
Open the Pivot.svelte file and import Pivot source files. Note that:
- if you use PRO version and install the Pivot package from a local folder, the import paths look like this:
<script>
import { Pivot } from 'dhx-pivot-package';
import 'dhx-pivot-package/dist/pivot.css';
</script>
Note that depending on the used package, the source files can be minified. In this case make sure that you are importing the CSS file as pivot.min.css.
- if you use the trial version of Pivot, specify the following paths:
<script>
import { Pivot } from '@dhx/trial-pivot';
import '@dhx/trial-pivot/dist/pivot.css';
</script>
In this tutorial you can see how to configure the trial version of Pivot.
Setting the container and adding Pivot
To display Pivot on the page, you need to create the container for Pivot, and initialize this component using the corresponding constructor:
<script>
import { onMount, onDestroy } from "svelte";
import { Pivot } from "@dhx/trial-pivot";
import "@dhx/trial-pivot/dist/pivot.css";
let container; // initialize container for Pivot
let table;
onMount(() => {
// initialize the Pivot component
table = new Pivot(container, {})
});
onDestroy(() => {
table.destructor(); // destruct Pivot
});
</script>
<div bind:this={container} class="widget"></div>
Adding styles
To display Pivot correctly, you need to specify important styles for Pivot and its container in the main css file of the project:
/* specify styles for initial page */
html,
body,
#app { /* make sure that you use the #app root container */
height: 100%;
padding: 0;
margin: 0;
}
/* specify styles for the Pivot container */
.widget {
height: 100%;
width: 100%;
}