M.Parent
M.Parent

Reputation: 183

How to include javascript in template block

I'm using the ej2 javascript Grid by Syncfusion. The following is a simplification of the actual code. In the grid definition I have a column having a template, like this :

{ field: "Quantity", headerText: "Quantity", template: "#colQuantity" },

<script id="colQuantity" type="text/x-template">
    ${quantity}
</script>

What I want to achieve here is to have a format "N4" in the quantity field, so the value is prettier. Is there a way to add a .toFixed(4) or a format("N4") somewhere in the template, or a way to rewrite this template so the format is applied?

Upvotes: 0

Views: 341

Answers (2)

M.Parent
M.Parent

Reputation: 183

I found that a template can call a javascript function and pass in data. The following code made it work.

<script>
    function formatNumber(args) {
        return args.quantity.toFixed(4);
    }
</script>

<script id="colQuantity" type="text/x-template">
    ${formatNumber(data)}
</script>

Upvotes: 0

MilletSoftware
MilletSoftware

Reputation: 4026

In the columns definition for the grid, in the definition for that column, use something like:

...
, columns: [ ...
         { field: 'Quantity', headerText: 'Quantity', width: 120, clipMode: 'EllipsisWithTooltip', type: 'string', format: 'N4', textAlign: 'Right' },...
        ], ...

Upvotes: 0

Related Questions