Kendo ui 剑道组合框设置默认值

Kendo ui 剑道组合框设置默认值,kendo-ui,Kendo Ui,我有一个剑道组合框,有两个条目,分别是1号和2号。我希望在默认情况下设置第1项 有没有更好的方法?我似乎找不到它,尽管我确实将占位符属性设置为thing1,并且它起了作用。剑道文档说占位符是小部件为空时显示的提示。默认情况下不设置。我使用占位符是否有误?我相信您称之为占位符的optionLabel属性用于指定不表示选择的文本。从列表中选择该选项时,所选项目为空,因此不是设置默认值的正确解决方案,除非您希望默认选择为空。可以通过调用小部件实例上的select方法以编程方式设置选择。例如,以下示例将

我有一个剑道组合框,有两个条目,分别是1号和2号。我希望在默认情况下设置第1项

有没有更好的方法?我似乎找不到它,尽管我确实将占位符属性设置为thing1,并且它起了作用。剑道文档说占位符是小部件为空时显示的提示。默认情况下不设置。我使用占位符是否有误?

我相信您称之为占位符的optionLabel属性用于指定不表示选择的文本。从列表中选择该选项时,所选项目为空,因此不是设置默认值的正确解决方案,除非您希望默认选择为空。可以通过调用小部件实例上的select方法以编程方式设置选择。例如,以下示例将选择设置为“Thing1”

<div id="dropdownlist"></div>

<script type="text/javascript">
    $("#dropdownlist").kendoDropDownList({
        dataSource: [
                'Thing1',
                'Thing2',
                'Thing3'
            ],
            optionLabel: "None"
    });

    $("#dropdownlist").data("kendoDropDownList").select(1);
</script>
最后,如果使用声明式初始化的MVVM,可以将选择绑定到视图模型上的值。我没有用MVVM示例来复杂化我的答案,因为我假设您没有使用这种机制

   <script> 

        $(function () {

            var data = [
                { text: "12 Angry Men", value: "1" },
                { text: "Il buono, il brutto, il cattivo.", value: "2" },
                { text: "Inception", value: "3" },
                { text: "One Flew Over the Cuckoo's Nest", value: "4" },
                { text: "Pulp Fiction", value: "5" },
                { text: "Schindler's List", value: "6" },
                { text: "The Dark Knight", value: "7" },
                { text: "The Godfather", value: "8" },
                { text: "The Godfather: Part II", value: "9" },
                { text: "The Shawshank Redemption", value: "10" },
                { text: "The Shawshank Redemption 2", value: "11" }
            ];

            $("#movies").kendoComboBox({
                dataTextField: "text",
                dataValueField: "value",
                dataSource: data,

            });
            let combobox = $("#movies").data("kendoComboBox");//This "instantiates it"
            combobox.value("The Godfather");


        });

    </script>

<input id="movies" />
结果:

数据源是基于零的,因此它应该是$dropdownlist.datakendoDropDownList.select0;如果要指定optionLabel的'Thing1',空项是零索引。所以在我的例子中,select1实际上是正确的。如果没有optionLabel,它将是您建议的0。没有在那里看到optionLabel,对此表示抱歉;
   <script> 

        $(function () {

            var data = [
                { text: "12 Angry Men", value: "1" },
                { text: "Il buono, il brutto, il cattivo.", value: "2" },
                { text: "Inception", value: "3" },
                { text: "One Flew Over the Cuckoo's Nest", value: "4" },
                { text: "Pulp Fiction", value: "5" },
                { text: "Schindler's List", value: "6" },
                { text: "The Dark Knight", value: "7" },
                { text: "The Godfather", value: "8" },
                { text: "The Godfather: Part II", value: "9" },
                { text: "The Shawshank Redemption", value: "10" },
                { text: "The Shawshank Redemption 2", value: "11" }
            ];

            $("#movies").kendoComboBox({
                dataTextField: "text",
                dataValueField: "value",
                dataSource: data,

            });
            let combobox = $("#movies").data("kendoComboBox");//This "instantiates it"
            combobox.value("The Godfather");


        });

    </script>

<input id="movies" />