This is a migrated thread and some comments may be shown as answers.

Invalid template when trying to use data with a hyphen/dash in the name

3 Answers 282 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 23 Nov 2016, 11:21 AM

Hi 

been trying to find a solution to a problem i have trying to solve over the last few days and beginning to wonder if its even possible.

I have set up a "Dojo" with all the code snippets which can be found here - http://dojo.telerik.com/aBuVul/7

The issue stems from the need to be able to reference data with a name that may contain hyphen in a template.

an example can be seen below, more details examples can be seen on the Dojo link above

var DashFirstName = "first-name",
        dashData = new kendo.data.DataSource({
        data: [
          {"first-name": "Joe", "country": "Germany"}, 
          {"first-name": "Jane", "country": "UK"},
          {"first-name": "Maria", "country": "Spain"}
        ]
      });
      $("#grid2").kendoGrid({
        dataSource:dashData,
        columns: [{
          field: "[\"first-name\"]",
          test: "22",
          title: "First Name",
          template: "<input type='radio' name='#= data." + DashFirstName + "#'>#= data." + DashFirstName + "#",
          width: 200
        }, {
          field: "country",
          title: "Country"
        }]
      });

I've tried various escapes to no avail... Any ideas would be greatly appreciated.

 

3 Answers, 1 is accepted

Sort by
0
Stephen
Top achievements
Rank 2
answered on 23 Nov 2016, 02:27 PM

Here's a demo of a working syntax: http://dojo.telerik.com/@Stephen/AXIja

 

The problem is that to use "-" in your field names, you must us bracket notation instead of dot notation to access the fields.

So you need to change your template to

template: "<input type='radio' name='#= data[\"first-name\"]#'>#= data[\"first-name\"]#",

 

This uses data["first-name"] to access the field.

 

0
Accepted
Stephen
Top achievements
Rank 2
answered on 23 Nov 2016, 02:30 PM

Sorry, here is the template using your DashFirstName variable:

template: "<input type='radio' name='#= data['" + DashFirstName + "']#'>#= data['" + DashFirstName + "']#",
0
Andrew
Top achievements
Rank 1
answered on 23 Nov 2016, 03:34 PM

Hi Stephen

Just implemented it and it works, thank you for you help :)

Tags
Grid
Asked by
Andrew
Top achievements
Rank 1
Answers by
Stephen
Top achievements
Rank 2
Andrew
Top achievements
Rank 1
Share this question
or