Toolbar in Gantt control

21 Dec 202212 minutes to read

The Gantt control provides toolbar support to handle Gantt actions. The Toolbar property accepts the collection of built-in toolbar items and ItemModel objects for custom toolbar items.

Built-in toolbar items

Built-in toolbar items execute standard actions of the Gantt control, and these items can be added to toolbar by defining the Toolbar as a collection of built-in items. It renders the button with icon and text.

The following table shows built-in toolbar items and its actions.

Built-in Toolbar Items Actions
Add Adds a new record.
Cancel Cancels the edit state.
CollapseAll Collapses all the rows.
Delete Deletes the selected record.
Edit Edits the selected record.
Indent Indent the selected record to one level.
Outdent Outdent the elected record to one level.
ExpandAll Expands all the rows.
NextTimeSpan Navigate the Gantt timeline to next time span.
PrevTimeSpan Navigate the Gantt timeline to previous time span.
Search Searches the records by the given key.
Update Updates the edited record.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" toolbar="@(new List<string>() { "Add", "Cancel", "CollapseAll", "Delete", "Edit", "ExpandAll", "NextTimeSpan", "PrevTimeSpan", "Search", "Update" })">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
                    <e-gantt-editsettings allowAdding="true" allowEditing="true" allowDeleting="true"></e-gantt-editsettings>
                </ejs-gantt>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

NOTE

The Toolbar has options to define both built-in and custom toolbar items.

Custom toolbar items

Custom toolbar items can be added to the toolbar by defining the Toolbar property as a collection of ItemModels. Actions for this customized toolbar items are defined in the ToolbarClick event.

By default, the custom toolbar items are at left position. You can change the position by using the Align property. In the following sample, the Quick Filter toolbar item is positioned at right.

@{
                    List<object> toolbarItems = new List<object>();
                    toolbarItems.Add(new { text = "Quick Filter", tooltipText = "Quick Filter", id = "toolbarfilter", align = "Right", prefixIcon: "e-quickfilter" });
                   }
				
				<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" toolbar=toolbarItems allowFiltering="true" toolbarClick="toolbarClick">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                          endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
                </ejs-gantt>
				
	<script>	
		function toolbarClick(args) {
            var ganttObj = document.getElementById("Gantt").ej2_instances[0];
            ganttObj.filterByColumn("TaskName", "startswith", "Perform");
        }
    </script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

NOTE

  • The Toolbar has options to define both built-in and custom toolbar items.

    * If a toolbar item does not match the built-in items, it will be treated as a custom toolbar item.

Built-in and custom items in toolbar

The Gantt control has an option to use both built-in and custom toolbar items at the same time.

In the following example, the ExpandAll and CollapseAll are built-in toolbar items and Test is the custom toolbar item.

@{
                    List<object> toolbarItems = new List<object>();
                    toolbarItems.Add("ExpandAll");
                    toolbarItems.Add("CollapseAll");
                    toolbarItems.Add(new { text = "Test", tooltipText = "Test", id = "Test" });
                }


                <ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" toolbar=toolbarItems toolbarClick="toolbarClick">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                        endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
			    </ejs-gantt>
				 
				 <script>
				  function toolbarClick(args) {
            
                    if (args.item.text === 'Test') {
                        alert("Custom toolbar click...");
                     }
                }
				</script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Enable/disable toolbar items

You can enable or disable the toolbar items by using the enableItems method.

@{
                    List<object> toolbarItems = new List<object>();
                    toolbarItems.Add("QuickFilter");
                    toolbarItems.Add("ClearFilter");
                }

                <ejs-button id="enable" content="Enable"></ejs-button>
                <ejs-button id="disable" content="Disable"></ejs-button>

                <ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" toolbar=toolbarItems allowFiltering="true" toolbarClick="toolbarClick">
                    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
                         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
                    </e-gantt-taskfields>
			    </ejs-gantt>
				
	 <script>
        function toolbarClick(args) {
            if (args.item.text === 'QuickFilter') {
                var ganttObj = document.getElementById("Gantt").ej2_instances[0];
                ganttObj.filterByColumn("TaskName", "startswith", "Perform");
            }
            if (args.item.text === 'ClearFilter') {
                var ganttObj = document.getElementById("Gantt").ej2_instances[0];
                ganttObj.clearFiltering();
            }
        }

        document.getElementById('enable').onclick = () => {
            var ganttObj = document.getElementById("Gantt").ej2_instances[0];
            ganttObj.toolbarModule.enableItems([ganttObj.element.id + '_QuickFilter', ganttObj.element.id + '_ClearFilter'], true);// enable toolbar items.
        };
        document.getElementById('disable').onclick = () => {
            var ganttObj = document.getElementById("Gantt").ej2_instances[0];
            ganttObj.toolbarModule.enableItems([ganttObj.element.id + '_QuickFilter', ganttObj.element.id + '_ClearFilter'], false);// disable toolbar items.
        };
    </script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Add input elements to toolbar

In the Gantt toolbar, you can add EJ2 editor elements like a numeric text box, a drop-down list, and date picker controls. The following code snippets demonstrate how to add EJ2 editors to the Gantt toolbar:

<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" toolbar="@(new List<string>() { "Add", "Cancel", "CollapseAll", "Delete", "Edit", "ExpandAll", "NextTimeSpan", "PrevTimeSpan", "Search", "Update", {type: 'Input', template: new NumericTextBox({ format: 'c2', value:1, width:150 })} })">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
         endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
    <e-gantt-editsettings allowAdding="true" allowEditing="true" allowDeleting="true"></e-gantt-editsettings>
</ejs-gantt>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}