Toolbar
The ToolBar()
configuration option of the Grid allows you to add command buttons and allow the user to invoke built-in Grid functionalities. You can also define custom commands or use templates to customize the Toolbar of the Telerik UI for ASP.NET MVC Grid.
Built-In Commands
You can configure the Toolbar and include any of the built-in commands:
.ToolBar(toolbar=> {
toolbar.Columns();
toolbar.Create();
toolbar.Save();
toolbar.Paste();
toolbar.Pdf();
toolbar.Excel();
toolbar.Search();
toolbar.Spacer();
toolbar.Separator();
})
Command | Description | Resources |
---|---|---|
Columns | Displays a global column menu. | Column menu documentation |
Create | Adds an empty data item to the grid. | Editing functionality documentation |
Save | Persists any data changes done by the end user. | Editing functionality documentation |
Paste | Enables the built-in paste operations. | Clipboard documentation |
Exports the grid data in PDF format. | PDF Export documentation | |
Excel | Exports the grid data in MS Excel format. | Excel Export documentation |
Search | Adds the built-in search panel for the Grid. | Search Panel documentation |
Spacer | Moves the tools that are declared after it to the right side of the ToolBar. | |
Separator | Acts as a delimiter between the ToolBar commands. |
Custom Commands
The Toolbar of the Grid component supports custom commands.
The following example demonstrates how to add a custom command to the Toolbar:
.ToolBar(toolbar=> {
toolbar.Custom().Text("Click me").HtmlAttributes(new { id = "customCommand" });
})
<script>
$(document).ready(function(){
$("#customCommand").on("click", function(event) {
alert('Custom command is clicked.');
// Add the custom command logic here.
});
})
</script>
Toolbar Template
The Grid also supports using a template for the Toolbar. You can define the template as a string or return its content using a JavaScript function. For more information on the available template options, refer to the ToolBar()
API.
The following example shows how to create a template for the Toolbar using the ClientTemplateHandler()
option, which returns an external Kendo UI template.
.ToolBar(toolbar => {
toolbar.ClientTemplateHandler("getToolbarTemplate");
})
<script id="GridToolbarTemplate" type="text/x-kendo-template">
<button class="k-button k-button-md k-rounded-md k-button-solid k-button-solid-base">Custom command</button>
</script>
<script>
function getToolbarTemplate(data) {
var template = $("#GridToolbarTemplate").html();
return template;
}
</script>
Built-In and Custom Commands in the Toolbar Template
To use the built-in commands in the Toolbar template, add the HTML
markup of the respective command.
The following example demonstrates how to add the built-in Pdf
and Search
commands together with custom commands to the Toolbar template.
<script id="GridToolbarTemplate" type="text/x-kendo-template">
<div class="refreshBtnContainer">
@(Html.Kendo().Button()
.Name("refresh")
.Icon("arrow-rotate-cw")
.ToClientTemplate()
)
</div>
<a role="button" class="k-button k-button-solid-base k-button-solid k-button-md k-rounded-md k-button-icontext k-grid-pdf" href="\\#"><span class="k-icon k-i-file-pdf"></span>Export to PDF</a>
<div class="toolbar">
<label class="category-label" for="category">Show products by category:</label>
@(Html.Kendo().DropDownList()
.Name("categories")
.OptionLabel("All")
.DataTextField("CategoryName")
.DataValueField("CategoryID")
.AutoBind(false)
.Events(e => e.Change("categoriesChange"))
.HtmlAttributes(new { style = "width: 150px;" })
.DataSource(ds =>
{
ds.Read("ToolbarTemplate_Categories", "Grid");
})
.ToClientTemplate()
)
</div>
<span class="k-textbox k-grid-search k-display-flex">
<input autocomplete="off" placeholder="Search..." title="Search..." class="k-input">
<span class="k-input-icon"><span class="k-icon k-i-search"></span></span>
</span>
</script>
Starting with version R3 2023 SP1, you can use the Template component to define custom Toolbar commands alongside the default ones.
The following example demonstrates how you can add Button and DropDownList components to the Grid's Toolbar, along with the default Excel
command. For a live example, visit the Toolbar Template Demo of the Grid.
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
.Name("grid")
.ToolBar(toolbar=> {
toolbar.Custom().ClientTemplate(
Html.Kendo().Template().AddComponent(c=>c
.Button()
.Name("refresh")
.Icon("arrow-rotate-cw")
.HtmlAttributes(new {title="Refresh"})
.Events(ev=>ev.Click("refresh"))
));
toolbar.Spacer();
toolbar.Custom().ClientTemplate(
Html.Kendo().Template()
.AddHtml("<label class=\"category-label\" for=\"category\">Show products by category:</label>")
.AddComponent(c => c
.DropDownList()
.Name("categories")
.OptionLabel("All")
.DataTextField("CategoryName")
.DataValueField("CategoryID")
.AutoBind(false)
.Events(e => e.Change("categoriesChange"))
.HtmlAttributes(new { style = "width: 150px;" })
.DataSource(ds =>
{
ds.Read("ToolbarTemplate_Categories", "Grid");
})
));
toolbar.Separator();
toolbar.Excel();
})
)
<script>
function refresh() {
var grid = $("#grid").data("kendoGrid");
grid.dataSource.read();
}
function categoriesChange() {
var value = this.value(),
grid = $("#grid").data("kendoGrid");
if (value) {
grid.dataSource.filter({ field: "CategoryID", operator: "eq", value: parseInt(value) });
} else {
grid.dataSource.filter({});
}
}
</script>
Server-Side Rendering of the Toolbar Template
Rendering of the Toolbar on the server is supported by using the Template()
configuration option.
The following example demonstrates how to define a server-side Toolbar template.
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
.Name("grid")
.ToolBar(toolbar =>
{
toolbar.Template(@<text>
<div class="refreshBtnContainer">
<a href="\\#" class="k-pager-refresh k-link k-button k-button-solid-base k-button-solid k-button-md k-rounded-md" title="Refresh"><span class="k-button-icon k-icon k-i-reload"></span></a>
</div>
<div class="toolbar">
<label class="category-label" for="category">Show products by category:</label>
@(Html.Kendo().DropDownList()
.Name("categories")
.OptionLabel("All")
.DataTextField("CategoryName")
.DataValueField("CategoryID")
.AutoBind(false)
.Events(e => e.Change("categoriesChange"))
.HtmlAttributes(new { style = "width: 150px;" })
.DataSource(ds =>
{
ds.Read("ToolbarTemplate_Categories", "Grid");
})
)
</div>
</text>);
})
... // Additional configuration.
)
<script>
$(document).ready(function() {
var grid = $("#grid");
grid.find(".k-grid-toolbar").on("click", ".k-pager-refresh", function (e) {
e.preventDefault();
grid.data("kendoGrid").dataSource.read();
});
});
function categoriesChange() {
var value = this.value(),
grid = $("#grid").data("kendoGrid");
if(value) {
grid.dataSource.filter({ field: "CategoryID", operator: "eq", value: parseInt(value) });
} else {
grid.dataSource.filter({});
}
}
</script>
<style>
#grid .k-grid-toolbar
{
padding: .6em 1.3em .6em .4em;
}
.category-label
{
vertical-align: middle;
padding-right: .5em;
}
#category
{
vertical-align: middle;
}
.refreshBtnContainer {
display: inline-block;
}
.k-grid .toolbar {
margin-left: auto;
margin-right: 0;
}
</style>