Toolbar in Gantt control
21 Dec 20229 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. |
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").Toolbar(new List<string>()
{ "Add", "Cancel", "CollapseAll", "Delete", "Edit", "ExpandAll", "NextTimeSpan", "PrevTimeSpan", "Search", "Update" }).TaskFields(ts =>
ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration").Progress("Progress").Child("SubTasks")
).EditSettings(es=>es.AllowEditing(true).AllowAdding(true).AllowDeleting(true)).Render()
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" });
}
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").ToolbarClick(
"toolbarClick").Toolbar(toolbarItems).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate"
).EndDate("EndDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Render()
<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"});
}
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").ToolbarClick(
"onToolbarClick").Toolbar(toolbarItems).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate"
).EndDate("EndDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Render()
<script>
function onToolbarClick(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");
}
@Html.EJS().Button("enable").Content("Enable").Render()
@Html.EJS().Button("disable").Content("Disable").Render()
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").ToolbarClick("onToolbarClick"
).Toolbar(toolbarItems).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration"
).Progress("Progress").Child("SubTasks")).Render()
<script>
document.getElementById("enable").addEventListener('click', () => {
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").addEventListener('click', () => {
var ganttObj = document.getElementById("Gantt").ej2_instances[0];
ganttObj.toolbarModule.enableItems([ganttObj.element.id + '_QuickFilter', ganttObj.element.id + '_ClearFilter'], false);// disable toolbar items.
});
function onToolbarClick(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();
}
}
</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:
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)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 })} }).TaskFields(ts =>
ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration").Progress("Progress").Child("SubTasks")
).EditSettings(es=>es.AllowEditing(true).AllowAdding(true).AllowDeleting(true)).Render()
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}