Call function from within template

1 Answer 6431 Views
Templates
Matt
Top achievements
Rank 1
Matt asked on 26 Aug 2016, 07:17 PM

We provide our users with the ability to define templates for their columns in the KendoTreeList and KendoGrid.  Users also want to be able to execute JavaScript functions from within the template function.  We construct each column dynamically and apply each template (if defined) for each column.

Unfortunately, I'm having difficulty getting it to work.  If I enter the following as the template, I get an error in the kendoOutpot (e.g., "formatFileSize" function doesn't exist):

#= formatFileSize(OV_FILE_VERSION) #

(formatFileSize is a function defined in the same script file in which we create the KendoTreeList and works fine if I call it during document.ready with dummy values.)

Likewise, if I do something like this:

01.#= var formatSize; if (isNan(OV_FILE_SIZE ) == false) {
02.    if OV_FILE_SIZE > 1073741824){
03.        formatSize = parseFloat(OV_FILE_SIZE / 1073741824).toFixed(2) + "GB";
04.    }
05.    else if (OV_FILE_SIZE > 1048576) {
06.        formatSize = parseFloat(OV_FILE_SIZE / 1048576).toFixed(2) + "MB";
07.    }
08.    else if (OV_FILE_SIZE > 1024) {
09.        formatSize = parseFloat(OV_FILE_SIZE / 1024).toFixed(2) + "KB";
10.    }
11.    else
12.        formatSize = OV_FILE_SIZE + "Bytes";
13.}
14.else
15.{
16.    formatSize = OV_FILE_SIZE ;
17.}
18.#
19.  
20.#= formatSize #

I get a JavaScript error (0x800a139e - JavaScript runtime error: Invalid template) in kendo.web.min.js.

What I'm trying to do is format the value to display the correct file size (Unfortunately, I don't have control over the data source so it's got to be formatted on the front end.)

Any ideas what I'm missing?

1 Answer, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 30 Aug 2016, 01:43 PM

Hello Matt,

Please refer to the Templates Overview article where is demonstrated how to use a function inside a template. 

Regards,
Boyan Dimitrov
Telerik by Progress
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
Tags
Templates
Asked by
Matt
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Share this question
or