kendo-uikendo-gridkendo-template

Kendo UI grid column template with nested evaluations - how to?


Kendo UI v2015.2.805

I have a KendoGrid with a template column that does a conditional to determine if a set of buttons should be added, if so additional evaluations are needed, and I can't figure out how to nest them.

The below works but does not have the required additional evaluation:

{ field: "Served", title: "Served", 
template: "<div>" + 
"#= (Kind==0 || Kind==7) ? '" + 
"<button type=\"button\" data-id=\"12345\">Yes</button>" + 
"<button type=\"button\" data-id=\"54321\">No</button>" + 
"' : " +  
"'NO BUTTON HERE'" +  
"#</div>"

I multi-lined it to try to get it to look good, which it does not. The idea is that if the Kind = 0 or 7 then show two buttons otherwise do not. Works great.

However I need to evaluate the data-id to #= ID #, so I try:

" <button type=\"button\" data-id=\"' #= ID # '\">Yes</button>"

I know I need to 'drop out' of the quoted string to get the evaluation to work and since I have used double quotes for the whole expression I am returning the button in the conditional as a single quoted string, and as such escaping the button attributes, but I can't get it to evaluate the #=.

I've tried so many different combinations I've lost track.

So what is the 'right-way' to do this?

A SOLUTION:

Accepting David's answer with a modification to use template evaluation in the function:

{ field: "Served", title: "Served", 
  template: function (data) {
    switch (data.Kind) {
      case 0:
      case 7:
        var template = kendo.template("<button type='button' data-id='#= ID #' >Yes</button><button type='button' data-id='#= ID #'>No</button>");
        return template(data);
      default:
        return '';
    }
}

Having the function perform the initial test removes one level and allows 'normal' evaluation to occur.


Solution

  • You can use a function instead I Beleive it will would make things so much easier for you.

    your template can be "#= buildButtons(data) #"

    function buildButtons(model) {
        if (model.Kind == 0 || model.Kind == 7) {
            return "hello world";
        }
    
        return "";
    }
    

    here is a code sample https://dojo.telerik.com/UQuqAfuv

    <div id="grid"></div>
      <script>
      var people = [
        { id: 1, firstName: 'David', lastName: 'Lebee' },
        { id: 2, firstName: 'John', lastName: 'Doe' }
      ];
    
      $('#grid').kendoGrid({ 
            dataSource: {
            transport: {
                read: function(options) {
                options.success(people);
              }
            }
          },
            columns: [
            { field: 'firstName', title: 'First Name'  },
            { field: 'lastName', title: 'Last Name' },
            { title: 'Actions', template: '#= buildActions(data) #'}
          ]
      });
    
    
      function buildActions(model) {
            if (model.firstName == "David") {
            return 'Hello David';
          }
    
          return '';
      }
      </script>