javascript kendo ui 在第一次加载时选择一个特定的索引/文本

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

kendo ui select a specifix index/text during first load

javascriptkendo-uikendo-comboboxkendo-dropdown

提问by NSS

The problem i am running into is that during the first load of the page i want to read the value from cookies if found, i want to change the theme that was stored in the cookie. not only want to change the them but i also want to select that item in the combo box so that it is in sync with the them that was applied.

我遇到的问题是,在页面的第一次加载期间,我想从 cookie 中读取值(如果找到),我想更改存储在 cookie 中的主题。不仅要更改它们,而且我还想在组合框中选择该项目,以便它与应用的它们同步。

How can i select a specific item during initial page load, when i am constructing the combobox ?

当我构建组合框时,如何在初始页面加载期间选择特定项目?

$(document).ready(function () {

   var initialized = false;
        // theme chooser drop-down
        var cmb=$(".themeChooser").kendoDropDownList({
            dataSource: [
                    { text: "Default" },
                    { text: "BlueOpal" },
                    { text: "Bootstrap" },
                    { text: "Silver" },
                    { text: "Uniform" },
                    { text: "Metro" },
                    { text: "Black" },
                    { text: "MetroBlack" },
                    { text: "HighContrast" },
                    { text: "Moonlight" }
            ],
            dataTextField: "text",
            dataValueField: "value",
            change: function (e) {

                $.cookie('selectedTheme', theme);
                changeTheme(theme);

            }
        });

        theme = ($.cookie('selectedTheme') || "default").toLowerCase();
        //Not sure how to trigger the select of combobox
        cmb.value(theme);  // no effect                       
});

回答by Abbas Galiyakotwala

Get a reference to the dropdown list

获取对下拉列表的引用

var dropdownlist = $("#Instrument").data("kendoDropDownList");

If you know the index you can use:

如果您知道可以使用的索引:

// selects by index
dropdownlist.select(1);

If not, use:

如果没有,请使用:

// selects item if its text is equal to "test" using predicate function
dropdownlist.select(function(dataItem) {
    return dataItem.symbol === "test";
});

check this http://jsfiddle.net/OnaBai/mRmNJ/

检查这个http://jsfiddle.net/OnaBai/mRmNJ/

回答by felickz

See Kendo Documentation

剑道文档

I believe in your case it would be some call like so:

我相信在你的情况下会有这样的电话:

//trigger the select of combobox 
cmb.select(function(dataItem) {
    return dataItem.text === theme;
});

Or just set the valueproperty in the object initializer

或者只是在对象初始设定项中设置value属性

value = ($.cookie('selectedTheme') || "default").toLowerCase(),