Skip to main content

Operations with projects

Change the active project

To change the currently active project, use the setProject() method. The method takes the project id as a parameter. The example below switches the active project after initialization:

const projects = [
{ id: "first", label: "First project" },
{ id: "second", label: "Second project" },
{ id: "third", label: "Third project" },
];

const list = new ToDo("#root", {
projects,
activeProject: "second"
});

const toolbar = new Toolbar("#toolbar", {
api: list.api,
});

list.setProject({ id: "first" });

Add a new project

To create a new project, use the addProject() method. The snippet below adds a fourth project:

const projects = [
{ id: "first", label: "First project" },
{ id: "second", label: "Second project" },
{ id: "third", label: "Third project" },
];

const list = new ToDo("#root", { projects });

const toolbar = new Toolbar("#toolbar", {
api: list.api,
});

list.addProject({
id: "fourth",
project: {
label: "Fourth project"
}
});

Update a project

To update parameters of a project dynamically, use the updateProject() method. The example below renames a project label:

const projects = [
{ id: "first", label: "First project" },
{ id: "second", label: "Second project" },
{ id: "third", label: "Third project" },
];

const list = new ToDo("#root", { projects });

const toolbar = new Toolbar("#toolbar", {
api: list.api,
});

list.updateProject({
id: "second",
project: {
label: "Project 2"
}
});

Delete a project

To remove a project, use the deleteProject() method. The snippet below deletes a project:

const projects = [
{ id: "first", label: "First project" },
{ id: "second", label: "Second project" },
{ id: "third", label: "Third project" },
];

const tasks = [
{ id: "1", text: "Task 1", project: "first" },
];

const list = new ToDo("#root", { projects, tasks });

const toolbar = new Toolbar("#toolbar", {
api: list.api,
});

list.deleteProject({ id: "first" });
note

Tasks linked to the project are not removed. The tasks move to the "No project" section.

console.log(list.getTask({id: "1"})); // -> {id: '1', text: 'Task 1', project: null}