kendo-uikendo-treeviewkendo-template

Using a parameter in Kendo ui template of checkboxes in TreeViewOptions


I am trying to create a class that create a generic kendo TreeView that the tree can have items with checkbox and items without checkbox. So, I created a class with the flowing c'tor:

    constructor(checkable: boolean = false) {

    // Create the treeview options
    const treeViewOptions: kendo.ui.TreeViewOptions = {
        checkboxes: {
            checkChildren: true,
            template: "# if (item.level() > 0) { #" +
                "<input type='checkbox' #= item.checked ? 'checked' : '' #>" +
                "# } #" 

            }, 
    // ... The rest of the treeViewOptions ...

    }

Now, all items that their item.level==0 are without checkbox. I want that if the parameter "checkable" of the c'tor is false, than all the items in the tree will not have checkboxes. I didn't know how to pass the "checkable" parameter into the template. I wanted something like this:

        checkboxes: {
        checkChildren: true,
        template: "# if (checkable && item.level() > 0) { #" +
            "<input type='checkbox' #= item.checked ? 'checked' : '' #>" +
            "# } #" 

        }, 

Please help me with that and if you think that there is more elegant way to do that I will be happy to hear. Thanks


Solution

  • You could make the template be an anonymous function and have it emit different template strings depending on constructor argument.

    template: function () {
      if (checkable) {
        return ... template string that allows checkboxes at item level > 0 ...
      } else {
        return ... simpler template string that has no checkboxes anywhere ...
      }
    }