kendo-ui - Kendo UI TreeView : Add only nodes not exisisting in the view

标签 kendo-ui treeview kendo-asp.net-mvc kendo-treeview

我在从 ComboBox 和多选中获取数据的页面上有 Treeview 。 ComboBox 包含每个成分的名称,多选包含可能的数量类型,然后用作所有子节点的名称。

这棵树看起来像这样:

  • 成分 1
  • 100mg
  • 200mg
  • 成分 2
  • 50mg
  • 100mg

  • 一切正常,除非我可以添加相同的值两次,因为我无法验证节点是否已经存在。

    这是我用来添加新元素的函数:
    var addElement = function () {
    
        var treeview = $("#ingredientTree").data("kendoTreeView");
        var multiselect = $("#ingredientAmount").data("kendoMultiSelect");
        var ingredientToAdd= $("#ingredient").val();
    
        // I allways get an empty array at this point.
        var exinstingIngredient= treeview.findByText(ingredientToAdd);
    
        var children = new Array();
        var amount = multiselect.value();
    
        for (var j = 0; j < amount.length; j++) {
    
            children.push({ text: amount[j] });
        }
    
        // it allways adds the items because the length is allways 0
        if (exinstingIngredient.length === 0) {
            treeview.append({
                text: ingredientToAdd,
                items: children
            });
        }
    }
    

    我不明白为什么即使我将其名称设置为文本并搜索此文本,它也找不到现有元素。

    编辑:

    这里我们有 Treeview :
    @(Html.Kendo().TreeView().TemplateId("treeview-template").Name("ingredientTree"))
    

    这是成分的来源,它只处理普通字符串:
    @(Html.Kendo().ComboBox()
         .Name("ingredient")
         .DataSource(source => source.Read(r => r.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "InternationalIngredients" }))))
         .Events(events => events.Change("onIngredientChanged"))
    )
    

    在您之后找到数量的来源,它将字符串处理为:
    @(Html.Kendo().MultiSelect()
       .Name("ingredientAmount")
       .DataSource(source => source.Read(read => read.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "InternationalIngredientAmount" })).Data("getIngredient")).ServerFiltering(true)))
    

    这是一个确定服务调用所选成分的函数:
    function getIngredient() {
        return { ingredient: $("#ingredient").val() }
    }
    

    最佳答案

    我现在找到了问题的原因。 findByText 似乎使用类“k-in”检查节点跨度的内容。不幸的是,当您按照 here 的描述添加模板时,会修改此内容。因此,如果您想查找带有模板的元素,则应使用 findById 或以可以使用 jQuery 的方式定义模板。

    关于kendo-ui - Kendo UI TreeView : Add only nodes not exisisting in the view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36404781/

    相关文章:

    c# - 使用 incell 编辑在网格中解析 Kendo 日期,ASP.NET MVC

    asp.net-mvc - 如何重用和参数 Kendo UI 网格 EditorTemplate(使用 ASP MVC)?

    asp.net-mvc - kendo multiselect,将选定的值发送到 Controller

    javascript - 文件类型的 Kendo UI 图像上传限制不起作用

    asp.net-mvc - Kendo Grid 内联编辑中的多选列表

    kendo-ui - MVVM 绑定(bind)到 Kendo Grid 非常慢?

    javascript - js中的树形 View (纯HTML/CSS/JS)问题

    java - 指示 Java FX TreeItem getChildren 延迟执行期间的进程

    c# - 没有集合的 TreeView 递归

    asp.net-mvc - 如何在 _Layout 页面中仅加载一次 TreeView 数据