Articles in this section
Category / Section

How to set the format for the template column in JS Grid?

3 mins read

This Knowledge base explains that how to set the format for the template column in JavaScript Grid.

Solution:

By default, in template column the value will be rendered based on the templates that we specified in the template property.

For template manipulation i.e., to set the format for the template column we have to use templateRefresh event of ejGrid.

The following code example demonstrates how to set the format for the template column using templateRefresh event of ejGrid.

JS:

1. Render the Grid Control.

 
<div id="Grid"></div>
           
    <script type="text/javascript">
        $(function () {
            $("#Grid").ejGrid({
                // the datasource "window.gridData" is referred from jsondata.min.js
                dataSource: window.gridData,
                allowPaging: true,
                pageSettings: { pageSize: 4 },
                templateRefresh : "templateRefresh",
                columns: [
                    { headerText: "Total Values", template: '<span>{{:EmployeeID + Freight}}</span>', width: 110 },
                    { field: "OrderID", headerText: "Order ID", textAlign: ej.TextAlign.Right, width: 90 },
                    { field: "CustomerID", headerText: "Customer ID", width: 90 },
                ]
            });
        });
    </script> 
 

 

 

MVC:

 
@(Html.EJ().Grid<object>("HeaderTemplate")
     .Datasource((IEnumerable<object>)ViewBag.datasource)
     .AllowPaging()
     .PageSettings(page => { page.PageSize(4); })
     .ClientSideEvents(eve => eve.TemplateRefresh("templateRefresh"))
     .Columns(col =>
      {
        col.HeaderText("Total Values").Template("<span>{{:EmployeeID + Freight}}</span>").Width(110).Add();
        col.Field("OrderID").HeaderText("Order ID").TextAlign(TextAlign.Right).Width(90).Add();
        col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
     })
)

 

ASP.NET:

 
<ej:Grid ID="EmployeesGrid" runat="server" AllowPaging="True">
            <PageSettings PageSize="4"></PageSettings>
            <ClientSideEvents TemplateRefresh="templateRefresh" />
            <Columns>
                <ej:Column HeaderText="Total Values" Template="<span>{{:EmployeeID + Freight}}</span>" TextAlign="Center" Width="110" />
                <ej:Column Field="OrderID" HeaderText="Order ID" TextAlign="Right" Width="90" />
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="80" />
            </Columns>
</ej:Grid>
 

 

 ASP.NET CORE:

 
<ej-grid id="Grid" datasource="(IEnumerable<object>)ViewBag.dataSource" allow-paging="true" template-refresh="templateRefresh">
    <e-page-settings page-size="4"></e-page-settings>
    <e-columns>
        <e-column header-text="Total Values" width="110" template='<span>{{:EmployeeID}} + {{:Freight}}</span>'></e-column>
        <e-column field="OrderID" header-text="Order ID" text-align="Right" width="90"></e-column>
        <e-column field="CustomerID" header-text="Customer ID" width="80"></e-column>
    </e-columns>
</ej-grid>
 

 

 

 

JS:

2. In templateRefresh event we apply for the template column using ej.format method.

 
function templateRefresh(args) {
    var value = parseFloat($(args.cell).text()); 
    $(args.cell).text(ej.format(value, "C2"))
 }

Result:

Formatted template column Grid

Conclusion

I hope you enjoyed learning about how to set the format for the template column 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