Search results

ToolBar

The Grid provides ToolBar support to handle grid actions. The Toolbar property accepts either the collection of built-in toolbar items and ItemModel objects for custom toolbar items or HTML element ID for toolbar template.

Built-in toolbar items

Built-in toolbar items execute standard actions of the grid, and it can be added 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.
Edit Edits the selected record.
Update Updates the edited record.
Delete Deletes the selected record.
Cancel Cancels the edit state.
Search Searches the records by the given key.
Print Prints the grid.
ExcelExport Exports the grid to Excel.
PdfExport Exports the grid to PDF.
WordExport Exports the grid to Word.
razor
toolbar.cs
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col =>
   {

       col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
       col.Field("Freight").HeaderText("Freight").Width("120").EditType("numericedit").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
       col.Field("ShipCountry").HeaderText("Ship Country").Width("150").EditType("dropdownedit").Add();

   }).AllowPaging().PageSettings(page => page.PageCount(2)).Toolbar(new List<string>() { "Print", "Search" }).Render()
public IActionResult Index()
        {
            var orders =OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }
  • The Toolbar has options to define both built-in and custom toolbar items.

Custom toolbar items

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

By default, Custom toolbar items are in position Left. You can change the position by using the align property. In the below sample, we have applied position Right for the Collapse All toolbar item.

razor
custom-toolbar-item.cs
@{
        List<object> toolbarItems = new List<object>();
        toolbarItems.Add(new { text = "Expand All", tooltipText = "Expand All", prefixIcon = "e-expand", id = "expandall" });
        toolbarItems.Add(new { text = "Collapse All", tooltipText = "Collapse All", prefixIcon = "e-collapse", id = "collapseall", align = "Right" });
    }
    @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).ToolbarClick("toolbarClick").Columns(col =>
   {

       col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
       col.Field("Freight").HeaderText("Freight").Width("120").EditType("numericedit").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
       col.Field("ShipCountry").HeaderText("Ship Country").Width("150").EditType("dropdownedit").Add();

   }).AllowPaging().PageSettings(page => page.PageCount(2)).Toolbar(toolbarItems).Render()

    <script>
function toolbarClick(args){

    if (args.item.id === 'expandall') {
        this.groupModule.expandAll();
    }
    if (args.item.id === "collapseall") {
        this.groupModule.collapseAll();
    }

}
    </script>
public IActionResult Index()
        {
            var orders = orderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }
  • 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.

Enable/disable toolbar items

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

razor
enable-disable.cs
@Html.EJS().Button("disable").Content("Disable").Render()
    @Html.EJS().Button("enable").Content("Enable").Render()

    @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).ToolbarClick("toolbarClick").Columns(col =>
{

    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
    col.Field("Freight").HeaderText("Freight").Width("120").EditType("numericedit").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
    col.Field("ShipCountry").HeaderText("Ship Country").Width("150").EditType("dropdownedit").Add();

}).AllowPaging().PageSettings(page => page.PageCount(2)).AllowGrouping().GroupSettings(group => group.Columns(new string[] { "CustomerID" })).Toolbar(new List<string>() { "Expand", "Collapse" }).Render()

    <script>
   document.getElementById("enable").addEventListener('click', () => {

            var grid= document.getElementById("Grid").ej2_instances[0];
            grid.toolbarModule.enableItems(['Grid_Collapse', 'Grid_Expand'], true);// enable toolbar items.
        });

  document.getElementById("disable").addEventListener('click', () => {

            var grid = document.getElementById("Grid").ej2_instances[0];
            grid.toolbarModule.enableItems(['Grid_Collapse', 'Grid_Expand'], false);// disable toolbar items.
   });

function toolbarClick(args){

    if (args.item.id === 'Grid_Collapse') { // grid_Collapse is component id + '_' + toolbar item name.
        this.groupModule.collapseAll();
    }
    if (args.item.id === 'Grid_Expand') {
        this.groupModule.expandAll();
    }

}
    </script>
public IActionResult Index()
        {
            var orders = orderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }