How to show the ToolBar items based on the JavaScript Grid?
This Knowledge base explains the way of how to show or hide the toolbar items in grid based on selected row index.
Solution
This can be achieved using rowSelected event of ejGrid. The rowSelected event will get trigger when selecting the rows of the grid and based on the rowIndex (argument in rowSelected event) value the toolbar items in grid can be show or hide.
The following code example demonstrates how to show or hide the toolbar items based on selected row index:
- Render the grid control
JS
<div id="Grid"></div> <script type="text/javascript"> $(function () { $("#Grid").ejGrid({ dataSource: window.gridData, allowPaging: true, editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true }, toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Update, ej.Grid.ToolBarItems.Cancel] }, columns: [ { field: "OrderID", headerText: "Order ID", isPrimaryKey: true}, { field: "CustomerID", headerText: "Customer ID" }, { field: "EmployeeID", headerText: "Employee ID" }, { field: "ShipCity", headerText: "Ship City" }, { field: "ShipCountry", headerText: "Ship Country" }, ], create:"create", rowSelected: "rowselected", }); }); </script>
RAZOR
@(Html.EJ().Grid<object("Grid") .Datasource((IEnumerable<Object>)ViewBag.datasource) .AllowPaging() .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); }) .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); }); }) .Columns(col => { col.Field("OrderID").HeaderText("Order ID"). IsPrimaryKey(true).Add(); col.Field("CustomerID").HeaderText("Customer ID").Add(); col.Field("EmployeeID").HeaderText("Employee ID").Add(); col.Field("ShipCity").HeaderText("Ship City").Add(); col.Field("ShipCountry").HeaderText("Ship Country").Add(); }) .ClientSideEvents(eve => eve.RowSelected("rowselected").Create(“create”)) )
C#
namespace Sample.Controllers { public class GridController : Controller { public ActionResult GridFeatures() { ViewBag.datasource = new NorthwindDataContext().OrdersViews.ToList(); return View(); } } }
ASPX
<ej:Grid id="Grid" runat="server" AllowPaging="true" AllowSorting="true"> <Columns> <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true”/> <ej:Column Field="CustomerID" HeaderText="Customer ID" /> <ej:Column Field="EmployeeID" HeaderText="Employee ID" /> <ej:Column Field="ShipCity" HeaderText="Ship City" /> <ej:Column Field="ShipCountry" HeaderText="Ship Country" /> </Columns> <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings> <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings> <ClientSideEvents RowSelected="rowselected" Create="create" /> </ej:Grid>
ASP.NET CORE
<ej-grid id="Grid" allow-paging="true" datasource="ViewBag.datasource" create="create" row-selected="rowselected"> <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true"></e-edit-settings> <e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","update","cancel" })"></e-toolbar-settings> <e-columns> <e-column field="OrderID" header-text="Order ID" is-primary-key="true"></e-column> <e-column field="CustomerID" header-text="Customer ID" ></e-column> <e-column field="EmployeeID" header-text="Employee ID"></e-column> <e-column field="ShipCity" header-text="Ship City"></e-column> <e-column field="ShipCountry" header-text="Ship Country"></e-column> </e-columns> </ej-grid>
Angular
<ej-grid #grid id="Grid" [dataSource]="gridData" allowPaging="true" (create)="onCreate($event)" (rowSelected)="onRowSelected($event)" [toolbarSettings]="toolbarItems" [editSettings]="editSettings"> <e-columns> <e-column field="OrderID" [isPrimaryKey]="true" headerText="Order ID" ></e-column> <e-column field="CustomerID" headerText="Customer ID"></e-column> <e-column field="EmployeeID" headerText="EmployeeID"></e-column> <e-column field="ShipCity" headerText="Ship City"></e-column> <e-column field="ShipCountry" headerText="Ship Country"></e-column> </e-columns> </ej-grid>
- Using create event we have hide the toolbar items initially.
TS file
@ViewChild('grid') GridModel: EJComponents<any, any>; onCreate(e:any){ var toolbar1 = $("#” + this.GridModel.widget._id +“_toolbarItems").data("ejToolbar"); $(toolbar1.items).hide(); } onRowSelected(e:any){ var gridID = this.GridModel.widget._id;//Get the corresponding Grid ID var gridObj = $("#"+ gridID).data("ejGrid"); toolbar = $("#"+ gridID + "_toolbarItems").data("ejToolbar"); if (e.rowIndex <= 5) { $("#" + gridID + "_edit").show(); $("#" + gridID + "_add").hide(); $("#" + gridID + "_delete").hide(); } else if (e.rowIndex > 5 && e.rowIndex <= 7) { $("#" + gridID + "_edit").hide(); $("#" + gridID + "_add").show(); $("#" + gridID + "_delete").hide(); } else { $("#" + gridID + "_edit").hide(); $("#" + gridID + "_add").show(); $("#" + gridID + "_delete").show(); } }
- Using rowSelected event in grid the required toolbar items can be show or hide based on rowIndex value.
JS
<script type="text/javascript"> function create(args){ var toolbar1 = $("#Grid_toolbarItems").data("ejToolbar"); $(toolbar1.items).hide(); } function rowselected(args) { var gridObj = $("#"+ this._id).data("ejGrid"); toolbar = $("#"+ this._id + "_toolbarItems").data("ejToolbar"); if (args.rowIndex <= 5) { $("#" + this._id + "_edit").show(); $("#" + this._id + "_add").hide(); $("#" + this._id + "_delete").hide(); } else if (args.rowIndex > 5 && args.rowIndex <= 7) { $("#" + this._id + "_edit").hide(); $("#" + this._id + "_add").show(); $("#" + this._id + "_delete").hide(); } else { $("#" + this._id + "_edit").hide(); $("#" + this._id + "_add").show(); $("#" + this._id + "_delete").show(); } } </script>
Figure 1: shows the edit icon in toolbar when the selected row index is less than 5
Figure 2: shows the add icon in toolbar when the selected row index is greater than 5 and less than 7.
Figure 3: shows add and delete icon in toolbar items when selected row index is greater than 7.
Conclusion
I hope you enjoyed learning about how to show the ToolBar items based on the JavaScript 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!