Call function from within template

2 posts, 0 answers
  1. Matt
    Matt avatar
    87 posts
    Member since:
    Mar 2009

    Posted 26 Aug 2016 Link to this post

    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";
    16.    formatSize = OV_FILE_SIZE ;
    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?

  2. Boyan Dimitrov
    Boyan Dimitrov avatar
    2062 posts

    Posted 30 Aug 2016 Link to this post

    Hello Matt,

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

    Boyan Dimitrov
    Telerik by Progress
    Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
Back to Top