javascript Kendo Ui 组合框 - 设置默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27403303/
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
Kendo Ui combobox - set default value
提问by user3573096
I successfully filled my combobox. But now I'm trying to set default value for combobox. For example let's say third value from source. This is my input and datasource:
我成功地填充了我的组合框。但现在我正在尝试为组合框设置默认值。例如,假设来自源的第三个值。这是我的输入和数据源:
<script>
viewModel.dataSourceType = new kendo.data.DataSource({
transport: {
read: {
url: "/api/Type/Get",
dataType: "json"
}
},
schema: {
id: "Id",
data: "Data",
model: {
id: "Id",
fields: {}
}
}
});
<input id="type"
data-role="combobox"
data-value-primitive="true"
data-auto-bind="true"
data-text-field="Name"
data-value-field="Id"
data-bind="value: model.Id, source: dataSourceType">
It's probably really easy but I'm strugling with that. Thank you.
这可能真的很容易,但我正在为此苦苦挣扎。谢谢你。
回答by OnaBai
Likely the problem is in your model
definition, i.e. what you are binding to the combobox.
问题可能出在您的model
定义中,即您绑定到组合框的内容。
According with your definition your JavaScript should be something like:
根据您的定义,您的 JavaScript 应该是这样的:
var dataSource = new kendo.data.DataSource({
type: "odata",
transport: {
...
}
});
var model = new kendo.observable({
dataSourceType: dataSource,
model : { Id: 2 }
});
kendo.bind($("#type"), model);
Where 2
is the value that you want as default (initial) value.
2
您想要作为默认(初始)值的值在哪里。
Realize that I have had to declare an extra model
for Id
since you say in your data-bind
definition that value
is model.Id
.
意识到我不得不声明一个额外的model
forId
因为你在你的data-bind
定义中说的value
是model.Id
.
Maybe you wanted to say:
也许你想说:
var model = new kendo.observable({
dataSourceType: dataSource,
Id: 2
});
kendo.bind($("#type"), model);
And then you should define the HTML as:
然后你应该将 HTML 定义为:
<input id="type"
data-role="combobox"
data-value-primitive="true"
data-auto-bind="true"
data-text-field="Name"
data-value-field="Id"
data-bind="value: Id, source: dataSourceType">
$(document).ready(function() {
var dataSource = new kendo.data.DataSource({
type: "odata",
serverFiltering: true,
transport: {
read: {
url: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products",
}
}
});
var model = new kendo.observable({
dataSourceType: dataSource,
Id: 2
});
kendo.bind($("#cbox"), model);
});
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.default.min.css" />
<script src="http://cdn.kendostatic.com/2014.3.1119/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1119/js/kendo.all.min.js"></script>
<input id="cbox"
data-role="combobox"
data-value-primitive="true"
data-auto-bind="true"
data-text-field="ProductName"
data-value-field="ProductID"
data-bind="value: Id, source: dataSourceType">
回答by Mohammed Farooq
You can achieve like this.
你可以这样实现。
var combobox = $("#kendoitems").data("kendoComboBox");
combobox.select(1);
you need to pass the index value to select(index).
您需要将索引值传递给 select(index)。
Refer this http://jsfiddle.net/NdPze/63/