Search results

Search

You can search records in a TreeGrid, by using the search method with search key as a parameter. This also provides an option to integrate search text box in treegrid’s toolbar by adding search item to the Toolbar.

razor
defaultSearching.cs
@Html.EJS().TreeGrid("TreeGrid").Height(270).Toolbar(new List<string>() { "Search" }).DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col =>
{
   col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
   col.Field("StartDate").HeaderText("Start Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Width(210).Add();
   col.Field("Duration").HeaderText("Duration").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(190).Add();

}).ChildMapping("Children").AllowPaging().TreeColumnIndex(1).Render()
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

To apply search at initial rendering, set the fields, operator, key, and ignoreCase in the SearchSettings.

razor
@Html.EJS().TreeGrid("TreeGrid").Height(270).Toolbar(new List<string>() { "Search" }).DataSource((IEnumerable<object>)ViewBag.datasource).SearchSettings(search =>
{
   search.Fields(new string[] { "TaskName" })
         .Operators("contains")
         .Key("plan")
         .IgnoreCase(true);
    }).Columns(col =>
    {
       col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
       col.Field("StartDate").HeaderText("Start Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Width(210).Add();
       col.Field("Duration").HeaderText("Duration").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(190).Add();

     }).ChildMapping("Children").TreeColumnIndex(1).AllowPaging().Render()

By default, treegrid searches all the bound column values. To customize this behavior define the SearchSettings.Fields property.

Search operators

The search operator can be defined in the Operators property of SearchSettings to configure specific searching.

The following operators are supported in searching:

Operator  Description
startsWith  Checks whether a value begins with the specified value.
endsWith  Checks whether a value ends with the specified value.
contains  Checks whether a value contains the specified value.
equal  Checks whether a value is equal to the specified value.
notEqual  Checks for values not equal to the specified value.

By default, the Operators value is contains.

Search by external button

To search treegrid records from an external button, invoke the search method.

razor
searchExternal.cs
@Html.TextBox("searchText")

@Html.EJS().Button("Search").Content("Search").CssClass("e-primary").Render()

@Html.EJS().TreeGrid("TreeGrid").Height(270).DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col =>
{
   col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
   col.Field("StartDate").HeaderText("Start Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Width(210).Add();
   col.Field("Duration").HeaderText("Duration").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(190).Add();

}).ChildMapping("Children").AllowPaging().TreeColumnIndex(1).Render()

<script>
    document.getElementById('Search').addEventListener('click', function (args) {
        var val = document.getElementById('searchText').value;
        var treegrid = document.getElementById('TreeGrid').ej2_instances[0];
        treegrid.search(val);
    });
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Search specific columns

By default, treegrid searches all visible columns. You can search specific columns by defining the specific column’s field names in the Fields property of SearchSettings.

razor
searchColumns.cs
@Html.EJS().TreeGrid("TreeGrid").Height(270).Toolbar(new List<string>() { "Search" }).DataSource((IEnumerable<object>)ViewBag.datasource).SearchSettings(search =>
{
  search.Fields(new string[] { "TaskName", "Duration" });

}).Columns(col =>
{
   col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
   col.Field("StartDate").HeaderText("Start Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Width(210).Add();
   col.Field("Duration").HeaderText("Duration").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(190).Add();

}).ChildMapping("Children").AllowPaging().TreeColumnIndex(1).Render()
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}