javascript 在 KENDO UI Multiselect 中选择默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17894659/
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-10-27 10:01:40  来源:igfitidea点击:

Selecting default values in KENDO UI Multiselect

javascripthtmlkendo-ui

提问by ckv

I have a kendo UI multiselect input. I am populating the values with a JSON object. I want the first value to be selected. Based on the documenation I have given as below but the value is still not selected.

我有一个剑道 UI 多选输入。我正在用 JSON 对象填充值。我想选择第一个值。根据我给出的文档如下,但仍未选择该值。

$("#days").kendoMultiSelect({
                dataTextField: "text",
                dataValueField: "value",
                dataSource: days,
                filter: "contains",
                value: [
                { text: "First", value: "1" },

            ]
            });

var days = [
    { text: "First", value: "1" },
    { text: "Second", value: "2" },
    { text: "Third", value: "3" },
    { text: "Fourth", value: "4" },
    { text: "Fifth", value: "5" }

            ];

采纳答案by nemesv

Because you have configured the dataValueField: "value"in the valuearray you need to provide the valueproperty values of the days objects.

因为您已经dataValueField: "value"value数组中配置了,所以您需要提供valuedays 对象的属性值。

So you just need to write value: [ "1" ]:

所以你只需要写value: [ "1" ]

$("#days").kendoMultiSelect({
                dataTextField: "text",
                dataValueField: "value",
                dataSource: days,
                filter: "contains",
                value: [ "1" ]
});

Demo JSFiddle.

演示JSFiddle。

回答by LawMan

In case you are using server side binding, you can do this...

如果您使用服务器端绑定,您可以这样做...

 @(Html.Kendo().MultiSelect()
               .Name("RolesVisibleToMultiSelect")
               .Placeholder("Select Roles...")
               .DataValueField("RoleId")
               .DataTextField("RoleName")
               .BindTo(Model.RequestDiscussion.RolesVisibleTo)
               .Value(Model.RequestDiscussion.RolesVisibleTo.Select(r => r.RoleId).ToArray()))