Template error on undefined item.

2 posts, 0 answers
  1. Lyndsy
    Lyndsy avatar
    1 posts
    Member since:
    Aug 2012

    Posted 23 Jun 2013 Link to this post

    I am having an issue with the template system

    my datasource is volatile by design to allow for maximum flexibility. Everything works great except I have one problem. If an item is undefined it ceases to render any other items if a template is applied. I will use a grid object to explain.

                    dataSource: dataSource,
                    groupable: true,
                    sortable: true,
                    pageable: {                   
                        pageSizes: true
                    columns: [{
                        field: "FirstName",                   
                        title: "First Name"                   
                        field: "LastName",                   
                        title: "Last Name"
                        field: "City"
                        field: "Title"
                        field: "BirthDate",
                        title: "Birth Date",
                        template: '#: FormatDate(BirthDate,"dd MMMM yyyy") #'
                        field: "Age"

    Incoming Data

    If the template is not set it works as expected non existing members are not shown and everything is hunky dory. But with the template an error gets thrown when it can find BirthDate and stops rendering the grid.
    ReferenceError: BirthDate is not defined
    kendo.all.js Line 281 : return new Function(argumentName, functionBody);

    I've tried doing an if statement in the template to return null if the object is undefined but that didn't seem to make a difference.

    Any thoughts on possible workarounds?
  2. Kiril Nikolov
    Kiril Nikolov avatar
    2543 posts

    Posted 25 Jun 2013 Link to this post

    Hello Lyndsy,

     The problems comes from the fact that when you do not have "BirthDate" field in your DataSource, then it is evaluated as "undefined". When using Kendo UI Templates you are allowed to write JavaScript expressions in order to format the data in a proper way. This is why I would suggest you the following template, where you check if you have BirthDate and return an empty string if it is undefined:

    template:  "#= (data.BirthDate) ? kendo.toString(BirthDate, 'dd MMMM yyyy') : '' #"
    Kiril Nikolov
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top