javascriptej2-syncfusion

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?


Solution

  • 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>