Articles in this section
Category / Section

How to move the custom tool items in front position of ASP.NET MVC Grid toolbar list?

4 mins read

In ASP.NET MVC Grid, items added under the custom toolbar items will displayed after the default toolbar items. This is the default behavior. This can be rearranged and move the custom tools prior to the default toolbar items in the dataBound event of the Grid which has been discussed in this KB.

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: "databound",
            //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 => {
        //rearrange the tools
        e.DataBound("databound");
        //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="databound" />
    </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="databound" 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();
        }
    }
}
 

 

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>

 

Grid toolbar separated the default items and custom items under two different ul elements. In the databound event, collect all the elements from second ul element (or custom item list). Prepend the required items to the first ul or to the default items.

<script type="text/javascript">
    function databound(args) {
        // custom items
        var liElem = this.element.find(".e-gridtoolbar ul:eq('1') li");
        //default items
        var firstUl = this.element.find(".e-gridtoolbar ul:eq(0)");
        for (var l = 0 ; l < 2; l++) {
            //moving only two icons to front
            //leaving the last icon
            firstUl.prepend($(liElem[l]).detach());
        }
    }
</script>

 

Angular2:

<ej-grid id="Grid" #grid [allowPaging]="true" [editSettings]="edit" (dataBound)="databound($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.edit = {
            allowEditing: true,
            allowAdding: true,
            allowDeleting: true
        }
        this.datasrc = data;
    }
    public datasrc: any;
    public toolbarsettings: any;
    public edit: any;
    @ViewChild('grid') Grid: EJComponents<any, any>;//
 
    databound(e) {
        setTimeout($.proxy(function (args) {
            // custom items
            var liElem = this.Grid.widget.element.find(".e-gridtoolbar ul:eq('1') li");
            //default items
            var firstUl = this.Grid.widget.element.find(".e-gridtoolbar ul:eq(0)");
            for (var l = 0; l < 2; l++) {
                //moving only two icons to front
                //leaving the last icon
                firstUl.prepend($(liElem[l]).detach());
            }
        },this), 0)
    }
}

 

Grid with rearranged toolbar items.

Figure 1. Grid with rearranged toolbar items.


Conclusion

I hope you enjoyed learning about how to items in front position of ASP.NET MVC Grid.

You can refer to our  ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our  ASP.NET MVC Grid examples to understand how to present and manipulate data.

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

If you have any queries or require clarifications, please let us know in comments 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