1

我有一个具有两个值的剑道组合框:主要、次要。我还有一个链接到数据源的 Kendo 多选。默认情况下,组合框显示“Main”,多选显示“Parent1”、“Parent2”,即数据源的“Name”字段。

如果用户从组合框中选择“辅助”,我想将多选的 dataTextField 动态更改为 Name2,同样,当用户从下拉列表中选择“主”时,多选应链接到“名称”作为其 dataTextField。

这是小提琴

HTML

<select id="CategoryOption" style="width: 100px;">
<option>Main</option>
<option>Secondary</option>
</select> <br><br><br><br><br><br>
<select id="MainCategory" style="width: 90%;"></select> 

Java 脚本

createCategoryOption("#CategoryOption");
createMainCategory("#MainCategory", "Name", "ID");
function createCategoryOption(divID1)
{
$(divID1).kendoComboBox({
        change: function (e) {
            SetSelectServicesText();
        }
    });
}
function createMainCategory(usersDiv, textField, valueField) {
 $("#MainCategory").kendoMultiSelect({
    dataSource: [
        { Name: "Parent1", Id: 1, Name2: "Child1" },
        { Name: "Parent2", Id: 2, Name2: "Child2" }
    ],
    dataTextField: textField,
    dataValueField: valueField
});
}
function SetSelectServicesText()
{
        if($("#CategoryOption").data("kendoComboBox").value() == "Main")
        {
            $("#MainCategory").destroy();
        createMainCategory("#MainCategory", "Name", "ID");
        }
        else
        {
            $("#MainCategory").destroy();
        createMainCategory("#MainCategory", "Name2", "ID");
        }
}

外部来源

<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.408/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.408/styles/kendo.rtl.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.408/styles/kendo.default.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.408/styles/kendo.dataviz.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.408/styles/kendo.dataviz.default.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2015.1.408/styles/kendo.mobile.all.min.css">

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2015.1.408/js/kendo.all.min.js"></script>
4

1 回答 1

1

Kendo 小部件通常会在目标元素周围创建一个包装结构,该结构不会被方法破坏destroy()(我认为这是一件坏事)。

所以销毁(或从 DOM 中删除)一个小部件并不是那么简单。一探究竟:

function createMainCategory(usersDiv, textField, valueField, remove) 
{
    var mc = $("#MainCategory");

    if (remove)
    {
        // Destroys the widget
        mc.data("kendoMultiSelect").destroy();

        // Get the widget wrapper element structure
        var p = mc.closest(".k-widget");

        // Detache the #MainCategory from the wrapper structure
        mc = mc.empty().detach();

        // Remove the wrapper structure
        p.remove();

        // Append the #MainCategory to the body again
        $('body').append(mc);
    }

    $("#MainCategory").kendoMultiSelect({
        dataSource: [
            { Name: "Parent1", Id: 1, Name2: "Child1" },
            { Name: "Parent2", Id: 2, Name2: "Child2" }
        ],
        dataTextField: textField,
        dataValueField: valueField
    });
}

如您所见,在删除块中...

  • 使用内置方法销毁小部件destroy()
  • 然后它选择包含所有结构的主要包装元素;
  • 在删除它之前,它选择并分离外部#MainCategory 包装器(detach()删除但返回其引用以进一步操作元素);
  • 因此,使用#MainCategory保险箱,它从 DOM中移除了包装器的整个结构;
  • 最后#MainCategory再次添加到主体上以用于托管新的小部件。

小提琴

于 2015-04-30T18:55:17.843 回答