Articles in this section
Category / Section

How to change the tooltip for custom toolbar items in JS Grid?

4 mins read

In Grid, tooltip values for the default toolbar items can be changed using the ej.Grid.Locale and its locale key words like “Add”, “Edit”, “ExcelExport” as tabulated in the following Help Document.

https://help.syncfusion.com/js/grid/globalizationandlocalization

But custom toolbar items and its tooltip cannot be changed in the same way using ej.Grid.Locale.

 

Solution:

Tooltip for the custom toolbar item can be changed using the data-content attribute. Initially, this attribute has been set as its respective item name which is the default behavior. Later, it can be changed in the databound and actionCompelete events of the Grid.

 

HTML:

 
<div id="Grid"></div>
 

 

JavaScript:

<script type="text/javascript">
    $(function () {
        $("#Grid").ejGrid({
            dataSource: window.gridData,
            allowPaging: true,
            pageSettings: { pageSize: 8 },
            groupSettings: {
                groupedColumns: ["EmployeeID"]
            },
            allowGrouping: true,
            editSettings: { allowAdding: true, allowEditing: true, allowDeleting: true },
            toolbarSettings: {
                showToolbar: true,
                toolbarItems: ["add", "edit", "delete", "update", "cancel"],
                customToolbarItems: ["Expand", "Collapse", { templateID: "#Refresh" }]
            },
            dataBound: "onDatabound",
            actionComplete: "onActionComplete",
            //do custom action needed for
            //required custom tools
            //in toolbarClick event
            toolbarClick: "onToolBarClick",
            columns: [
                          { field: "OrderID", headerText: "Order ID", textAlign: ej.TextAlign.Right },
                          { field: "CustomerID", headerText: "Customer ID" },
                          { field: "EmployeeID", headerText: "Employee ID", textAlign: ej.TextAlign.Right },
                          { field: "Freight", headerText: "Freight", textAlign: ej.TextAlign.Right, format: "{0:C}" },
                          { field: "ShipCity", headerText: "Ship City" }
            ]
        });
    });
</script>

 

Razor:

@(Html.EJ().Grid<object>("Grid")
    .AllowPaging()
    .AllowGrouping()
    .GroupSettings(grp=>grp.GroupedColumns(g=>g.Add("EmployeeID")))
    .ToolbarSettings(toolbar =>
        {
            toolbar.ShowToolbar().ToolbarItems(items =>
            {
                items.AddTool(ToolBarItems.Add);
                items.AddTool(ToolBarItems.Edit);
                items.AddTool(ToolBarItems.Delete);
                items.AddTool(ToolBarItems.Update);
                items.AddTool(ToolBarItems.Cancel);
            })
            .CustomToolbarItems(new List<object>() {  
            "Expand", "Collapse",  
            new Syncfusion.JavaScript.Models.CustomToolbarItem() { 
                TemplateID = "#Refresh" 
            }  
        });
    }) 
    .EditSettings(edit=>edit.AllowAdding().AllowDeleting().AllowEditing())
    .ClientSideEvents(e => {
        e.ActionComplete("onActionComplete");
        e.DataBound("onDatabound");
        //do custom action needed for
        //required custom tools
        //in toolbarClick event
        e.ToolbarClick("onToolBarClick");
    })
    .Columns(col =>
    {
        col.Field("OrderID").HeaderText("Order ID").TextAlign(TextAlign.Right).Add();
        col.Field("CustomerID").HeaderText("Customer ID").Add();
        col.Field("EmployeeID").HeaderText("EmployeeID").TextAlign(TextAlign.Right).Add();
        col.Field("Freight").Format("{0:C}").HeaderText("Freight").Add();
        col.Field("ShipCity").HeaderText("Ship City").Add();
    })
)
 

 

Controller:

namespace MvcApplication66.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.dataSource = OrderRepository.GetAllRecords();
            return View();
        }
 
    }
}

 

WebForms:

    <ej:Grid ID="Grid" runat="server" AllowPaging="true" AllowGrouping="true">
        <GroupSettings GroupedColumns="EmployeeID">
        </GroupSettings>
        <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings>
        <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel">
            <CustomToolbarItem>
                <ej:CustomToolbarItem Text="Expand" />
                <ej:CustomToolbarItem Text="Collapse" />
                <ej:CustomToolbarItem TemplateID="#Refresh" />
            </CustomToolbarItem>
        </ToolbarSettings>
        <Columns>
            <ej:Column Field="OrderID" HeaderText="Order ID" TextAlign="Right" />
            <ej:Column Field="CustomerID" HeaderText="Customer ID" />
            <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" />
            <ej:Column Field="Freight" Format ="{0:C}" TextAlign="Right"/>
            <ej:Column Field="ShipCity" HeaderText="Ship City" />
        </Columns>
        <ClientSideEvents ToolbarClick="onToolBarClick" DataBound="onDatabound" ActionComplete="onActionComplete"/>
    </ej:Grid>

 

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Grid.DataSource = order;
            this.Grid.DataBind();
        }

 

.Net Core

<ej-grid id="Grid" allow-paging="true" allow-grouping="true" toolbar-click="onToolBarClick" databound="onDatabound" action-complete="onActionComplete" datasource="ViewBag.datasource">
    <e-toolbar-settings show-toolbar="true" toolbar-items=@(new List<string>() { "excelExport" })>
        <e-custom-toolbar-items>
            <e-custom-toolbar-item text="Expand"></e-custom-toolbar-item>
            <e-custom-toolbar-item text="Collapse"></e-custom-toolbar-item>
            <e-custom-toolbar-item template-id="#Refresh"></e-custom-toolbar-item>
        </e-custom-toolbar-items>
    </e-toolbar-settings>
    <e-group-settings grouped-columns=@(new List<string>(){ "EmployeeID" })></e-group-settings>
    <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true"></e-edit-settings>
    <e-columns>
        <e-column field="OrderID" header-text="Order ID" text-align="Right"></e-column>
        <e-column field="EmployeeID" header-text="Employee ID" text-align="Right"></e-column>
        <e-column field="CustomerID" header-text="Customer ID"></e-column>
        <e-column field="Freight" text-align="Right" format="{0:C}"></e-column>
        <e-column field="ShipCity" header-text="Ship City"></e-column>
    </e-columns>
</ej-grid>

 

namespace core1.Controllers
{
    public partial class GridController : Controller
    {
        public static List<Orders> order = new List<Orders>();
        public ActionResult GridFeatures()
        {
            ViewBag.dataSource = order;
            return View();
        }
    }
}
 

 

Angular2:

<ej-grid id="Grid" #grid [allowPaging]="true" [allowGrouping]="true" [dataSource]="datasrc"  [editSettings]="edit" [groupsettings]="groupSettings" (dataBound)="databound($event)" (actionComplete)="onActionComplete($event)" (toolbarClick)="onToolbarClick($event)"  [toolbarSettings]="toolbarsettings" >
    <e-columns>
        <e-column field="OrderID" headerText="Order ID" textAlign="right" ></e-column>
        <e-column field="CustomerID" headerText="Customer ID"></e-column>
        <e-column field="EmployeeID" headerText="Employee Name" textAlign="right"></e-column>
        <e-column field="Freight" format="{0:C}" textAlign="right"></e-column>
        <e-column field="ShipCity" headerText="Ship City"></e-column>
    </e-columns>
</ej-grid>

 

TypeScript:

import { Component, OnInit, Input, ElementRef, ViewChild } from '@angular/core';
import { EJComponents } from "ej-angular2/src/ej/core";
 
@Component({
    selector: 'ej-app',
    templateUrl: 'src/grid/grid.component.html',
})
export class GridComponent {
    constructor() {
        this.toolbarsettings = {
            showToolbar: true,
            toolbarItems: ["add", "edit", "delete", "update", "cancel"],
            customToolbarItems: [ "Expand", "Collapse",
                { templateID: "<a  class='e-toolbaricons e-icon refresh' />" }
            ]
        };
        this.groupSettings = {
              groupedColumns: ["EmployeeID"]
        }
        this.edit = {
            allowEditing: true,
            allowAdding: true,
            allowDeleting: true
        }
        this.datasrc = data;
    }
    public datasrc: any;
    public toolbarsettings: any; 
    public groupSettings: any;
    public edit: any;
 
}

 

Template and CSS:

<script id="Refresh" type="text/x-jsrender">
    <a class="e-toolbaricons e-icon refresh" />
</script>

 

<style type="text/css" class="cssStyles">
    .Expand:before {
        content: "\e627";
    }
 
    .Collapse:before {
        content: "\e625";
    }
 
    .refresh:before {
        content: "\e677";
    }
</style>

 

Define the databound and actionComplete events with the update for the data-content attribute for all the custom toolbar items.

<script type="text/javascript">
    function onDatabound(args) {
        updateTooltip(this)
    }
    function onActionComplete(args) {
        if (!this.initialRender)
            updateTooltip(this)
    }
    function updateTooltip(proxy) {
        $("#" + proxy.element.attr("id") + "_Refresh").attr("data-content", "Refresh Grid");
        $("#" + proxy.element.attr("id") + "_Collapse").attr("data-content", "Collapse Content");
        $("#" + proxy.element.attr("id") + "_Expand").attr("data-content", "Expand Content");
    }
</script>

 

TypeScript:

import { Component, OnInit, Input, ElementRef, ViewChild } from '@angular/core';
import { EJComponents } from "ej-angular2/src/ej/core";
 
@Component({
    selector: 'ej-app',
    templateUrl: 'src/grid/grid.component.html',
})
export class GridComponent {
    constructor() {
    }
    @ViewChild('grid') Grid: EJComponents<any, any>;
 
    databound(e) {
        setTimeout($.proxy(function (args) {
           this.updateTooltip()
        }, this), 0)
    }
    updateTooltip() {
        $("#" + this.Grid.widget.element.attr("id") + "_Refresh").attr("data-content", "Refresh Grid");
        $("#" + this.Grid.widget.element.attr("id") + "_Collapse").attr("data-content", "Collapse Content");
        $("#" + this.Grid.widget.element.attr("id") + "_Expand").attr("data-content", "Expand Content");
    }
    onActionComplete(e) {
        if (!ej.isNullOrUndefined(this.Grid.widget) && !this.Grid.widget.initialRender)
            this.updateTooltip();
    }
}

 

Figure 1. Grid custom toolbar with updated tooltip.

Conclusion

I hope you enjoyed learning about how to change the tooltip for custom toolbar items in JS Grid.

You can refer to our JavaScript Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.  You can also explore our JavaScript  Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied