Skip to main content

Group tasks

PRO

The functionality is available in PRO Edition only

You can group tasks by any field to let end users view them by responsible person, status, priority, or any other property. Only one grouping field is active at a time.

Group by resource​

Call the group-tasks action with field: "resource" to place tasks under their assigned resources:

api.exec("group-tasks", { field: "resource" });

Tasks appear under their assigned leaf resources (those with no children) only. Parent resources, such as teams, don't have their own tasks and are shown only with resourceHierarchy as containers for the groups of their members. Resources without assigned tasks aren't shown.

To apply grouping on initialization, set the groupBy property:

<Gantt
{tasks}
{resources}
{assignments}
groupBy={{ field: "resource" }}
/>

Group by task field​

Pass any task field name to group by that property. Each group takes its label from the corresponding field values in the data:

api.exec("group-tasks", { field: "priority" });
api.exec("group-tasks", { field: "status" });

Clear grouping​

To remove the active grouping and restore the default task tree, call group-tasks with field: null:

api.exec("group-tasks", { field: null });

Grouping options​

You can control how groups are built and where tasks appear with these options. Pass them to the group-tasks action. You can combine the options in a single call, except for resourceHierarchy and multipleResources, which can't be used together:

api.exec("group-tasks", {
field: "resource",
multipleResources: true, // task appears once under combined group
taskHierarchy: true, // parent/child task structure within groups
ungrouped: "top", // unassigned tasks at the top
});

Tasks assigned to multiple resources​

By default, a task assigned to multiple resources appears under each resource separately. Set multipleResources to true to show it once under a combined group of the resources assigned to the task instead:

// Default — task appears under each assigned resource
api.exec("group-tasks", { field: "resource" });

// Combined — task appears once under a group of Alice and Bob
api.exec("group-tasks", {
field: "resource",
multipleResources: true,
});

Resource hierarchy​

By default, resources are displayed as a flat list when grouping by resource. Set resourceHierarchy to true to display them as a tree (teams and members) instead:

// Default — "Alice" and "Bob" groups in a flat list
api.exec("group-tasks", { field: "resource" });

// Tree — "Alice" and "Bob" groups nested under "Dev team"
api.exec("group-tasks", {
field: "resource",
resourceHierarchy: true,
});

This option applies only when field is set to "resource".

Task hierarchy​

By default, tasks appear as a flat list within each group. Set taskHierarchy to true to preserve the parent/child task structure:

api.exec("group-tasks", {
field: "resource",
taskHierarchy: true,
});

Ungrouped tasks​

Tasks with no value for the grouping field appear in a separate section at the bottom by default. The section is labeled "Unassigned" when grouping by resource and "Ungrouped" for other fields. Use ungrouped to move them to the top or hide them:

api.exec("group-tasks", {
field: "priority",
ungrouped: "top", // "top" | "bottom" (default) | false
});

Custom value resolver​

To extract a group value with custom logic instead of reading task[field] directly, pass a resolver function. For example, to group owner names case-insensitively:

api.exec("group-tasks", {
field: "owner",
resolver: task => task.owner?.toLowerCase(),
});

When users drag a task to another group, Gantt writes the group value to task[field]. That's why the resolver should read the same field and return values that are valid for it.

This option doesn't apply when field is set to "resource".

Customizing grouped cell content​

Default behavior​

When grouping is active, group header rows are rendered by the built-in cell component. Its output depends on the grouping field:

  • Resource grouping — shows the resource avatar and name; with multipleResources: true, combined groups show stacked avatars without names
  • Other fields — shows the raw field value as a string (e.g., "1" for priority: 1). To show readable labels instead (e.g., "Low"), add a custom cell component
  • Tasks with no value — shows "Unassigned" for resource grouping and "Ungrouped" for other fields

Custom cell component​

To override the default cell rendering, pass a custom Svelte component to the cell property of a column. Inside the component, use row.$group to detect group header rows and row.$groupValue to read the group value. The active grouping config is available through the "gantt-store" context:

GroupCell.svelte
<script>
import { getContext } from "svelte";

const api = getContext("gantt-store");
const { groupBy } = api.getReactiveState();

let { row } = $props();

const isGroup = $derived($groupBy?.field && row.$group);
</script>

{#if isGroup}
{#if row.$groupValue === "$ungrouped"}
Ungrouped
{:else}
{$groupBy.field}: {row.$groupValue}
{/if}
{:else}
{row.text}
{/if}

Apply the component to a column via the cell property:

<script>
import { Gantt } from "@svar/svelte-gantt";
import GroupCell from "./GroupCell.svelte";

const columns = [
{ id: "text", header: "Task name", flexgrow: 1, cell: GroupCell },
];
</script>

<Gantt {tasks} {links} {scales} {columns} groupBy={{ field: "priority" }} />

Mapping raw values to labels​

When grouping by a numeric or coded field, the raw value is rarely the right display text. Define a map of raw values to readable labels and use a $derived value to resolve the label before rendering:

GroupCell.svelte
<script>
import { getContext } from "svelte";

const api = getContext("gantt-store");
const { groupBy } = api.getReactiveState();

let { row } = $props();

const priorityMap = { 1: "Low", 2: "Medium", 3: "High" };

const isGroup = $derived($groupBy?.field && row.$group);

const label = $derived.by(() => {
const value = row.$groupValue;
if ($groupBy?.field === "priority") return priorityMap[value] ?? value;
return value;
});
</script>

{#if isGroup}
{#if row.$groupValue === "$ungrouped"}
Ungrouped
{:else}
{label}
{/if}
{:else}
{row.text}
{/if}

Moving tasks between groups​

Users can drag a task to another group. Gantt doesn't move the task in the original task tree. It only updates the task data so that the task belongs to the new group:

  • when grouping by a task field, the field gets the value of the new group. If the task is dropped into the "Ungrouped" section, the field is cleared.
  • when grouping by resource, the resource of the old group is replaced with the resource of the new group. Other resources stay assigned to the task. For example, if a task is assigned to Alice and Bob and you drag it from Bob's group to Carol's group, the task becomes assigned to Alice and Carol. If the task is dropped into the "Unassigned" section, only the resource of the old group is removed. With multipleResources: true, the task gets exactly the resources of the new group, and dropping it into the "Unassigned" section removes all its resources.

To move a task to another group from code, update the task data instead of calling move-task:

Limitations in grouped view​

When grouping is active:

  • tasks are arranged by groups, so their order differs from the original task tree. To keep parent/child relations within groups, use taskHierarchy
  • many editing actions are unavailable and disabled in the built-in menu and toolbar
  • only tasks without child tasks can be dragged, and they can't be dropped as children of other tasks

If you customize the menu or toolbar using getMenuOptions or getToolbarButtons, pass { group: true } to get only the options that are valid in a grouped view:

const menu = getMenuOptions({ group: true });
const toolbar = getToolbarButtons({ group: true });

Related sample:

Related articles: