jQuery 清除剑道多选的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19835676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:41:54  来源:igfitidea点击:

Clear selected value(s) for Kendo multi select

jquerykendo-uikendo-multiselect

提问by Nitish

I have Kendo multiSelect control which is working perfectly fine. However I am facing issue to reset its selected value. The following is what I have tried so far :

我有 Kendo multiSelect 控件,它工作得非常好。但是,我面临重置其选定值的问题。以下是我迄今为止尝试过的:

$("#Department option:selected").removeAttr("selected");  

And

var departmentMultiselect = $('#Department').data("kendoMultiSelect");
var subtract = $('#department').val();
                var values = departmentmultiselect.value().slice();
                values = $.grep(values, function (a) {
                 return $.inarray(a, subtract) == -1;
                });
                departmentmultiselect.datasource.filter({});
                departmentmultiselect.value(values);  

In the second code, control bypasses the following code

在第二个代码中,控制绕过了下面的代码

values = $.grep(values, function (a) {
                     return $.inarray(a, subtract) == -1;
                    });  

How can I reset this control?

如何重置此控件?

回答by Mekanik

To unselect all values in kendo multiselect:

要取消选择剑道多选中的所有值:

var multiSelect = $('#Department').data("kendoMultiSelect");
multiSelect.value([]);

回答by Warr1611

I had this same question, but wanted to remove only one value from the multiselect rather than everything. This is what I came up with:

我有同样的问题,但只想从多选中删除一个值而不是所有值。这就是我想出的:

var multiselect = $('#multiSelectId').data("kendoMultiSelect");
var values = multiselect.value().slice();

// optionId is the value I want to remove
var index = $.inArray(optionId, values);
if (index > -1) {
    values.splice(index, 1);
}

multiselect.dataSource.filter({});
multiselect.value(values);

回答by PHenry

I tried the above solution, except for me, it just ended up duplicated the while multiselect right below the current existing one, without any selections. Kind of what I wanted but a duplicate. Is there a better way? Thanks.

我尝试了上述解决方案,除了我之外,它只是在当前现有的多选正下方复制了 while 多选,没有任何选择。我想要的那种,但重复。有没有更好的办法?谢谢。

Heres my declaration in case it helps
    function createUsernameMultiSelect() {
        $('#usernamesFilter').kendoMultiSelect({
            autoClose: true,
            dataTextField: "Username",
            dataValueField: "UsernameValue",
            dataSource: {
                transport: {
                    read: {
                        type: "POST",
                        async: false,
                        url: "/Dashboard/GetDashboardData",
                        dataType: "json",
                        data: {
                            dashboardCode: "12",
                        }
                    }
                },
            },
            change: onUsernameChange,
            select: onUsernameSelect,

    });