首页 > 解决方案 > 在 TreeViewOptions 中使用复选框的 Kendo ui 模板中的参数

问题描述

我正在尝试创建一个创建通用剑道 TreeView 的类,该树可以包含带有复选框的项目和没有复选框的项目。所以,我用流动的 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 ...

    }

现在,所有 item.level==0 的项目都没有复选框。我希望如果 c'tor 的参数“checkable”为 false,那么树中的所有项目都没有复选框。我不知道如何将“checkable”参数传递到模板中。我想要这样的东西:

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

        }, 

请帮助我,如果您认为有更优雅的方式可以做到这一点,我会很高兴听到。谢谢

标签: kendo-uikendo-treeviewkendo-template

解决方案


您可以使模板成为匿名函数,并让它根据构造函数参数发出不同的模板字符串。

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

推荐阅读