kendo ui select a specifix index/text during first load

安稳与你 提交于 2020-01-01 12:22:12

问题


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.

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                       
});

回答1:


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/




回答2:


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 value property in the object initializer

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


来源:https://stackoverflow.com/questions/20832383/kendo-ui-select-a-specifix-index-text-during-first-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!