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 To Do List is compatible with Svelte. We have prepared code examples on how to use DHTMLX To Do List with Svelte. For more information, refer to the corresponding Example on GitHub.
Creating a project
There are several ways of creating a Svelte project:
- you can use the SvelteKit
or
- you can also use Svelte with Vite (but without SvelteKit):
npm create vite@latest
Check the details in the related article.
Installation of dependencies
Let's name the project as my-svelte-todo-app and go to the app directory:
cd my-svelte-todo-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
- 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 To Do List
Now you should get the DHTMLX To Do List source code. First of all, stop the app and proceed with installing the To Do List package.
Step 1. Package installation
Download the trial To Do List package and follow steps mentioned in the README file. Note that trial To Do List is available 30 days only.
Step 2. Component creation
Now you need to create a Svelte component, to add To Do List with Toolbar into the application. Let's create a new file in the src/ directory and name it ToDo.svelte.
Import source file
Open the ToDo.svelte file and import To Do List source files. Note that:
- if you use PRO version and install the To Do List package from a local folder, the import paths look like this:
<script>
import { ToDo, Toolbar } from 'dhx-todolist-package';
import 'dhx-todolist-package/dist/todo.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 todo.min.css.
- if you use the trial version of To Do List, specify the following paths:
<script>
import { ToDo, Toolbar } from '@dhx/trial-todolist';
import '@dhx/trial-todolist/dist/todo.css';
</script>
In this tutorial you can see how to configure the trial version of To Do List.
Setting containers and adding To Do List with Toolbar
To display To Do List with Toolbar on the page, you need to create containers for To Do List and Toolbar, and initialize these components using the corresponding constructors:
<script>
import { onMount, onDestroy } from "svelte";
import { ToDo, Toolbar } from "@dhx/trial-todolist";
import "@dhx/trial-todolist/dist/todo.css";
let toolbar_container, todo_container; // initialize containers for To Do List and Toolbar
let todo, toolbar;
onMount(() => {
// initialize the To Do List component
todo = new ToDo(todo_container, {})
// initialize the Toolbar component
toolbar = new Toolbar(toolbar_container, {
api: todo.api, // provide To Do List inner API
// other configuration properties
})
});
onDestroy(() => {
todo.destructor(); // destruct To Do List
toolbar.destructor(); // destruct Toolbar
});
</script>
<div class="component_container">
<div bind:this={toolbar_container}></div>
<div bind:this={todo_container} style="height: calc(100% - 56px);"></div>
</div>
Loading data
To add data into the To Do List, we need to provide a data set. You can create the data.js file in the src/ directory and add some data into it:
export function getData() {
const tasks = [
{
id: "temp://1652991560212",
project: "introduction",
text: "Greetings, everyone! \u{1F44B} \nI'm DHTMLX To Do List.",
priority: 1
},
{
id: "1652374122964",
project: "introduction",
text: "You can assign task performers and due dates using the menu.",
assigned: ["user_4", "user_1", "user_2", "user_3"],
due_date: "2033-03-08T21:00:00.000Z",
priority: 2
},
// ...
];
const users = [
{
id: "user_1",
label: "Don Smith",
avatar:
"https://snippet.dhtmlx.com/codebase/data/common/img/02/avatar_61.jpg"
},
// ...
];
const projects = [
{
id: "introduction",
label: "Introduction to DHTMLX To Do List"
},
{
id: "widgets",
label: "Our widgets"
}
];
return { tasks, users, projects };
}
Then open the App.svelte file, import data, and pass it into the new created <ToDo/>
components as props:
<script>
import ToDo from "./ToDo.svelte";
import { getData } from "./data.js";
const { users, tasks, projects } = getData();
</script>
<ToDo {users} {tasks} {projects} />
Go to the ToDo.svelte file and apply the passed props to the To Do List configuration object:
<script>
import { onMount, onDestroy } from "svelte";
import { ToDo, Toolbar } from "@dhx/trial-todolist";
import "@dhx/trial-todolist/dist/todo.css";
export let users;
export let tasks;
export let projects;
let toolbar_container, todo_container;
let todo, toolbar;
onMount(() => {
todo = new ToDo(todo_container, {
users, // apply user data
tasks, // apply task data
projects, // apply project data
// other configuration properties
})
toolbar = new Toolbar(toolbar_container, {
api: todo.api,
// other configuration properties
})
});
onDestroy(() => {
todo.destructor();
toolbar.destructor();
});
</script>
<div class="component_container">
<div bind:this={toolbar_container}></div>
<div bind:this={todo_container} style="height: calc(100% - 56px);"></div>
</div>
You can also use the parse()
method inside the onMount()
method of Svelte to load data into To Do List:
<script>
import { onMount, onDestroy } from "svelte";
import { ToDo, Toolbar } from "@dhx/trial-todolist";
import "@dhx/trial-todolist/dist/todo.css";
export let users;
export let tasks;
export let projects;
let toolbar_container, todo_container;
let todo, toolbar;
onMount(() => {
todo = new ToDo(todo_container, {})
toolbar = new Toolbar(toolbar_container, {
api: todo.api,
// other configuration properties
})
todo.parse({ tasks, users, projects });
});
onDestroy(() => {
todo.destructor();
toolbar.destructor();
});
</script>
<div class="component_container">
<div bind:this={toolbar_container}></div>
<div bind:this={todo_container} style="height: calc(100% - 56px);"></div>
</div>
The parse(data)
method provides data reloading on each applied change.
Now the To Do List component is ready to use. When the element will be added to the page, it will initialize the To Do List with data. You can provide necessary configuration settings as well. Visit our To Do List API docs to check the full list of available properties.
Handling events
When a user makes some action in the To Do List, it invokes an event. You can use these events to detect the action and run the desired code for it. See the full list of events.
Open ToDo.svelte and complete the onMount()
method in the following way:
<script>
// ...
let todo;
onMount(() => {
todo = new ToDo(todo_container, {});
todo.api.on("add-task", (obj) => {
console.log("A new task is added", obj);
});
});
onDestroy(() => {
todo.destructor();
});
</script>
// ...
Step 3. Adding To Do List into the app
To add the component into the app, open the App.svelte file and replace the default code with the following one:
<script>
import ToDo from "./ToDo.svelte";
import { getData } from "./data.js";
const { users, tasks, projects } = getData();
</script>
<ToDo {users} {tasks} {projects} />
After that, you can start the app to see To Do List loaded with data on a page.
Now you know how to integrate DHTMLX To Do List with Svelte. You can customize the code according to your specific requirements. The final example you can find on GitHub.