Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 107 additions & 165 deletions components/grid/toolbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,208 +10,150 @@ position: 45

# Grid Toolbar

The grid provides a toolbar where you can add a variety of actions that are not tied to a concrete row.
The [Blazor Grid](https://demos.telerik.com/blazor-ui/grid/overview) toolbar can render built-in and custom tools. This article describes the built-in tools and shows how to add [custom tools](#custom-tools) or create a [custom toolbar](#custom-toolbar-configuration).

To use a toolbar, define the `GridToolBarTemplate` tag of the grid. In it, you can use arbitrary HTML and components to get the desired layout, and also `GridCommandButton` instances in (you can read more about the features available in those buttons in the [Command Column](slug://components/grid/columns/command) article).
## Built-in Tools

>note The toolbar is not associated with an item from the data source. The `Item` field on the click event handler argument of a `GridCommandButton` will always be `null` and the `Edit`, `Update`, `Cancel` commands do not work with it.
By default, the [Blazor Grid](https://demos.telerik.com/blazor-ui/grid/overview) does not automatically render all built-in tools when a toolbar is added. To include specific tools in a [toolbar configuration](#toolbar-tools-configuration), you need to explicitly define them using the tool tags below.

In this article, you will learn how to use:
### Command Tools

* [Built-in Commands](#built-in-commands)
* [Custom Commands](#custom-commands)
* [Custom Layout](#custom-layout)
@[template](/_contentTemplates/common/parameters-table-styles.md#table-layout)

| Tool Name | Tool Tag | Description |
| --- | --- | --- |
| Add | `GridToolBarAddTool` | An add command that fires the [`OnAdd` event](slug://components/grid/editing/overview#events). |
| CsvExport | `GridToolBarCsvExportTool` | An export command for CSV files that fires the [`OnBeforeExport` event](slug://grid-export-events#onbeforeexport). |
| ExcelExport | `GridToolBarExcelExportTool` | An export command for Excel files that fires the [`OnBeforeExport` event](slug://grid-export-events#onbeforeexport). |
| SearchBox | `GridToolBarSearchBoxTool` | A searchbox that filters multiple Grid columns simultaneously. |

## Built-in Commands
### Layout Tools

The grid offers built-in commands that you can invoke through its toolbar. To use them, set the `Command` property of the button to the command name. The built-in command names are:
| Tool Name | Tool Tag | Description |
| --- | --- | --- |
| Spacer | `GridToolBarSpacerTool` | Consumes the available empty space and pushes the rest of the tools next to one another. |

* `Add` - starts [inserting a new item in the grid](slug://components/grid/editing/overview).
## Custom Tools

* `ExcelExport` - starts an [Excel export of the grid data](slug://grid-export-excel).
In addition to built-in tools, the Grid also supports custom tools. Use the `<GridToolBarCustomTool>` tag, which is a standard Blazor `RenderFragment`. See the example below.

* `CsvExport` - starts an [CSV export of the grid data](slug://grid-export-csv).
## Toolbar Tools Configuration

Add a `<GridToolBar>` tag inside `<TelerikGrid>` to configure a toolbar, for example:

>caption How to insert a new item in the grid
* Arrange the Grid tools in a specific order;
* Remove some of the built-in tools;
* Add custom tools.

````RAZOR
@result
>important When configuring the Toolbar, you can use either the `<GridToolBar>` or the `<GridToolBarTemplate>`. Note that both cannot be used together.

<TelerikGrid Data=@MyData Pageable="true" PageSize="15" EditMode="@GridEditMode.Inline" Height="500px"
OnUpdate="@UpdateHandler" OnCreate="@CreateHandler">
<GridToolBarTemplate>
<GridCommandButton Command="Add" Icon="@SvgIcon.Plus">Add Employee</GridCommandButton>
</GridToolBarTemplate>
>caption Grid Toolbar Tools

````RAZOR
<TelerikGrid Data=@GridData
EditMode="@GridEditMode.Inline"
Pageable="true"
OnUpdate=@UpdateItem
OnCreate=@CreateItem>
<GridToolBar>
<GridToolBarCustomTool>
<TelerikButton OnClick="@OnToolbarCustomClick">Custom Grid Tool</TelerikButton>
</GridToolBarCustomTool>

<GridToolBarAddTool>
Add a product
</GridToolBarAddTool>

<GridToolBarCsvExportTool>
Export to CSV
</GridToolBarCsvExportTool>

<GridToolBarExcelExportTool>
Export to Excel
</GridToolBarExcelExportTool>

<GridToolBarSpacerTool />

<GridToolBarSearchBoxTool />
</GridToolBar>
<GridColumns>
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="Employee ID" />
<GridColumn Field=@nameof(SampleData.Name) Title="Employee Name" />
<GridColumn Field=@nameof(SampleData.HireDate) Title="Hire Date" />
<GridCommandColumn>
<GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil">Edit</GridCommandButton>
<GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true">Save</GridCommandButton>
<GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">Cancel</GridCommandButton>
<GridColumn Field=@nameof(Person.EmployeeId) Editable="false" Visible="true" Width="200px" />
<GridColumn Field=@nameof(Person.Name) Width="200px" />
<GridColumn Field=@nameof(Person.AgeInYears) Title="Age" Visible="false" Width="240px" />
<GridColumn Field=@nameof(Person.HireDate) Title="Hire Date" Width="230px" />
<GridCommandColumn Width="200px">
<GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil"></GridCommandButton>
<GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true"></GridCommandButton>
</GridCommandColumn>
</GridColumns>
</TelerikGrid>

@code {
string result;
public List<SampleData> MyData { get; set; }
private List<Person> GridData { get; set; }

private async Task UpdateHandler(GridCommandEventArgs args)
private void OnToolbarCustomClick()
{
SampleData item = args.Item as SampleData;

// perform actual data source operations here through your service
SampleData updatedItem = await MyService.Update(item);

// update the local view-model data with the service data
await GetGridData();

result = string.Format("Employee with ID {0} now has name {1} and hire date {2}", updatedItem.ID, updatedItem.Name, updatedItem.HireDate);
Console.WriteLine("Custom Grid Toolbar tool clicked!");
}

private async Task CreateHandler(GridCommandEventArgs args)
protected override void OnInitialized()
{
SampleData item = args.Item as SampleData;
var data = new List<Person>();
var rand = new Random();

// perform actual data source operations here through your service
SampleData insertedItem = await MyService.Create(item);

// update the local view-model data with the service data
await GetGridData();

result = string.Format("On {2} you added the employee {0} who was hired on {1}.", insertedItem.Name, insertedItem.HireDate, DateTime.Now);
for (int i = 0; i < 50; i++)
{
data.Add(new Person()
{
EmployeeId = i,
Name = "Employee " + i.ToString(),
HireDate = DateTime.Now.Date.AddDays(-(i * 2)),
AgeInYears = 20
});
}
GridData = new List<Person>(data);
}

//in a real case, keep the models in dedicated locations, this is just an easy to copy and see example
public class SampleData
private void CreateItem(GridCommandEventArgs args)
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime HireDate { get; set; }
}
var argsItem = args.Item as Person;

async Task GetGridData()
{
MyData = await MyService.Read();
}
argsItem.EmployeeId = GridData.Count + 1;

protected override async Task OnInitializedAsync()
{
await GetGridData();
GridData.Insert(0, argsItem);
}

// the following static class mimics an actual data service that handles the actual data source
// replace it with your actual service through the DI, this only mimics how the API can look like and works for this standalone page
public static class MyService
private void UpdateItem(GridCommandEventArgs args)
{
private static List<SampleData> _data { get; set; } = new List<SampleData>();
var argsItem = args.Item as Person;
var itemForEdit = GridData.FirstOrDefault(i => i.EmployeeId == argsItem.EmployeeId);

public static async Task<SampleData> Create(SampleData itemToInsert)
if (itemForEdit != null)
{
itemToInsert.ID = _data.Count + 1;
_data.Insert(0, itemToInsert);

return await Task.FromResult(itemToInsert);
}

public static async Task<List<SampleData>> Read()
{
if (_data.Count < 1)
{
for (int i = 1; i < 50; i++)
{
_data.Add(new SampleData()
{
ID = i,
Name = "Name " + i.ToString(),
HireDate = DateTime.Now.AddDays(-i)
});
}
}

return await Task.FromResult(_data);
}

public static async Task<SampleData> Update(SampleData itemToUpdate)
{
var index = _data.FindIndex(i => i.ID == itemToUpdate.ID);
if (index != -1)
{
_data[index] = itemToUpdate;
return await Task.FromResult(_data[index]);
}

throw new Exception("no item to update");
itemForEdit.AgeInYears = argsItem.AgeInYears;
itemForEdit.HireDate = argsItem.HireDate;
itemForEdit.Name = argsItem.Name;
}
}
}
````

>caption The result from the code snippet above, after the built-in Create button in the toolbar was clicked

![Blazor Create Toolbar Button](images/create-toolbar-button.png)

## Custom Commands

You can use the toolbar to add buttons that invoke actions specific to your application.

>caption How to define a custom command in the grid toolbar
public class Person
{
public int? EmployeeId { get; set; }

````RAZOR
@result
public string Name { get; set; }

<TelerikGrid Data=@MyData Pageable="true" PageSize="15">
<GridToolBarTemplate>
<GridCommandButton Command="MyToolbarCommand" OnClick="@MyCommandFromToolbar" Icon="@SvgIcon.InfoCircle">Fire My Command</GridCommandButton>
</GridToolBarTemplate>
<GridColumns>
<GridColumn Field=@nameof(SampleData.Name) Title="Employee Name" />
<GridColumn Field=@nameof(SampleData.HireDate) Title="Hire Date" />
</GridColumns>
</TelerikGrid>
public int? AgeInYears { get; set; }

@code {
string result;

private void MyCommandFromToolbar(GridCommandEventArgs args)
{
//note - the args.Item object is null because the command item is not associated with an item

result = "my custom toolbar command fired at " + DateTime.Now.ToString();

StateHasChanged();
}

//in a real case, keep the models in dedicated locations, this is just an easy to copy and see example
public class SampleData
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime HireDate { get; set; }
}

public IEnumerable<SampleData> MyData = Enumerable.Range(1, 50).Select(x => new SampleData
{
ID = x,
Name = "name " + x,
HireDate = DateTime.Now.AddDays(-x)
});
public DateTime HireDate { get; set; }
}
}
````

>caption The result from the code snippet above, after the custom command button in the toolbar was clicked

![Blazor Custom Command Toolbar](images/custom-command-toolbar.png)
## Custom Toolbar Configuration

## Custom Layout
Add a `<GridToolBarTemplate>` tag inside `<TelerikGrid>` to configure a custom toolbar. You can add your own HTML and components to create a more complex layout in the grid header to match your business needs and also `GridCommandButton` instances (read more about the features available in those buttons in the [Command Column](slug://components/grid/columns/command) article).

You can add your own HTML and components to create a more complex layout in the grid header to match your business needs. You can still use the grid command buttons, as well as other components and logic.

>caption Custom Grid Toolbar Layout
>caption Custom Grid Toolbar

````RAZOR
@result
Expand Down Expand Up @@ -252,7 +194,7 @@ You can add your own HTML and components to create a more complex layout in the
</TelerikGrid>

@code {
string result;
private string result;

private void CreateHandler(GridCommandEventArgs args)
{
Expand All @@ -273,19 +215,19 @@ You can add your own HTML and components to create a more complex layout in the

public List<SampleData> MyData = Enumerable.Range(1, 50).Select(
x => new SampleData
{
ID = x,
Name = "name " + x,
HireDate = DateTime.Now.AddDays(-x)
}).ToList();
{
ID = x,
Name = "name " + x,
HireDate = DateTime.Now.AddDays(-x)
}).ToList();
}
````

>caption The result from the code snippet above, after adding a row, changing the dropdown and clicking the custom button.
## Next Steps

![Blazor Custom Toolbar Layout](images/custom-toolbar-layout.png)
* [Handle Grid events](slug://grid-events)

## See Also

* [Live Demo: Grid Toolbar](https://demos.telerik.com/blazor-ui/grid/editing-inline)
* [Blazor Grid](slug://grid-overview)
* [Grid Live Demo](https://demos.telerik.com/blazor-ui/grid/overview)
* [Grid API](/blazor-ui/api/Telerik.Blazor.Components.TelerikGrid)
Loading