Skip to main content

Operations with projects

Changing the active project

It is possible to change the currently active project via the setProject() method. It takes the id of a project as a parameter:

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" });

Adding a new project

You may create a new project via the addProject() method:

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"
}
});

Updating a project

To dynamically update parameters of a project, apply the updateProject() method. For instance, you can update the label of the 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.updateProject({
id: "second",
project: {
label: "Project 2"
}
});

Deleting a project

To remove an unnecessary project, apply the deleteProject() method:

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

The tasks linked to the project won't be removed. They will be moved to the "No project" section

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