Selection
1 Mar 202220 minutes to read
Selection provides an option to highlight a row or a cell. It can be done using arrow keys or by scrolling down the mouse. To disable selection in the Gantt control, set the AllowSelection
to false.
The Gantt control supports two types of selection that can be set by using the SelectionSettings.Type
property. They are:
-
Single
: Sets a single value by default and allows only selection of a single row or a cell. -
Multiple
: Allows you to select multiple rows or cells. To perform the multi-selection, press and hold the CTRL key and click the desired rows or cells.
Selection mode
The Gantt control supports three types of selection modes that can be set by using the SelectionSettings.Mode
. They are:
-
Row
: Allows you to select only rows, and the row value is set by default. -
Cell
: Allows you to select only cells. -
Both
: Allows you to select rows and cells at the same time.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionsettings mode="Both"></e-gantt-selectionsettings>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Row selection
The row selection in the Gantt control can be enabled or disabled using the AllowSelection
property. You can get the selected row object using the getSelectedRecords
method. The following code example shows how to disable the row selection in Gantt.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="false">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Row
selection is the default type of Gantt selection mode.
Selecting a row on initial load
You can select a row at the time of loading by setting the index of the row to the SelectedRowIndex
property. Find the following code example for details.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true" selectedRowIndex="3">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Selecting a row dynamically
You can also select a row dynamically using the selectRow
method. The following code demonstrates how to select a row dynamically by clicking the custom button.
<ejs-button id="selectRow" content="Select Row" cssClass="e-primary"></ejs-button>
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
</ejs-gantt>
<script>
document.getElementById('selectRow').addEventListener('click', function (args) {
var ganttObj = document.getElementById('Gantt').ej2_instances[0];
ganttObj.selectionModule.selectRow(2); // passing the record index to select the row
});
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Multiple row selection
You can select multiple rows by setting the SelectionSettings.Type
property to Multiple
. You can select more than one row by holding down the CTRL key while selecting multiple rows. The following code example explains how to enable multiple selection in Gantt.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionSettings mode="Row" type="Multiple"></e-gantt-selectionSettings>
</ejs-gantt>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Selecting multiple rows dynamically
You can also select rows dynamically using the selectRows
method. The following code demonstrates how to select rows dynamically by clicking the custom button.
<ejs-button id="selectRows" content="Select Rows" cssClass="e-primary"></ejs-button>
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionsettings mode="Row" type="Multiple"></e-gantt-selectionsettings>
</ejs-gantt>
<script>
document.getElementById('selectRows').addEventListener('click', function (args) {
var ganttObj = document.getElementById('Gantt').ej2_instances[0];
ganttObj.selectionModule.selectRows([1, 2, 3]); // passing the record index to select the row
});
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Customize row selection action
While selecting a row in Gantt, the RowSelecting
and RowSelected
events will be triggered. The the RowSelecting
event will be triggered on initialization of row selection, and you can get the previously selected row and current selecting row’s information, which is used to prevent selection of a particular row. The RowSelected
event will be triggered on completion of row selection action, and you can get the current selected row’s information through this event. The following code example demonstrates how to prevent the selection of a row using the RowSelecting
event.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true" rowSelecting="rowSelecting">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
</ejs-gantt>
<script>
function rowSelecting(args) {
if (args.rowIndex == 3) {
args.cancel = true;
}
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
In the Gantt control, when you click an already selected row, selection will be cleared. While deselecting a row in Gantt, the RowDeselecting
and RowDeselected
events will occur. The RowDeselecting
event will occur on initialization of deselecting row, and you can get the current deselecting row’s information to prevent the deselection of particular row. The RowDeselected
event will occur on completion of row deselection action, and you can get the current deselected row’s information.
Toggle selection
The toggle selection allows you to select and deselect a specific row or cell. To enable toggle selection, set the enableToggle
property of the selectionSettings to true
. If you click the selected row or cell, then it will be deselected and vice versa. By default, the enableToggle
property is set to false
.
<ejs-button id="toggle" content="Disable Toggle" cssClass="e-primary"></ejs-button>
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionsettings mode="Row" type="Multiple" enableToggle="true"></e-gantt-selectionsettings>
</ejs-gantt>
<script>
document.getElementById('toggle').addEventListener('click', function (args) {
var ganttObj = document.getElementById('Gantt').ej2_instances[0];
ganttObj.selectionsettings.enableToggle = false;
});
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Clear selection
You can clear the selected cells and selected rows by using a method called clearSelection
. The following code example demonstrates how to clear the selected rows in Gantt Chart.
<ejs-button id="selectRows" content="Select Rows" cssClass="e-primary"></ejs-button>
<ejs-button id="clearSelection" content="Clear Selection" cssClass="e-primary"></ejs-button>
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionsettings mode="Row" type="Multiple"></e-gantt-selectionsettings>
</ejs-gantt>
<script>
document.getElementById('selectRows').addEventListener('click', function (args) {
var ganttObj = document.getElementById('Gantt').ej2_instances[0];
ganttObj.selectionModule.selectRows([1, 2, 3]); // passing the record index to select the row
});
document.getElementById('clearSelection').addEventListener('click', function (args) {
var ganttObj = document.getElementById('Gantt').ej2_instances[0];
ganttObj.clearSelection(); // Clear the selected rows
});
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Get selected row indexes and records
You can get the selected row indexes by using the getSelectedRowIndexes
method. And by using getSelectedRecords
method, you can get the selected record details.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true" rowSelected="rowSelected">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionSettings mode="Row" type="Multiple"></e-gantt-selectionSettings>
</ejs-gantt>
<script>
function rowSelected(args) {
var ganttObj = document.getElementById("Gantt").ej2_instances[0];
var selectedrowindex = ganttObj.selectionModule.getSelectedRowIndexes(); // get the selected row indexes.
alert(selectedrowindex); // to alert the selected row indexes.
var selectedrecords = ganttObj.selectionModule.getSelectedRecords(); // get the selected records.
console.log(selectedrecords); // to print the selected records in console window.
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}
Multiple Selection based on condition
You can select multiple rows based on condition by using the selectRows
method.
In the following code, the rows which contains TaskId
value as 3 and 4 are selected at initial rendering.
<ejs-gantt id='Gantt' dataSource="ViewBag.dataSource" height="450px" allowSelection="true" dataBound="dataBound">
<e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate"
endDate="EndDate" duration="Duration" progress="Progress" child="SubTasks">
</e-gantt-taskfields>
<e-gantt-selectionSettings mode="Row" type="Multiple"></e-gantt-selectionSettings>
</ejs-gantt>
<script>
function dataBound(args) {
var ganttObj = document.getElementById("Gantt").ej2_instances[0];
var rowIndexes = [];
ganttObj.treeGrid.grid.dataSource.forEach((data, index) => {
if (data.TaskId === 3 || data.TaskId === 4) {
rowIndexes.push(index);
}
});
ganttObj.selectionModule.selectRows(rowIndexes);
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = GanttData.ProjectNewData();
return View();
}