admin管理员组

文章数量:1323729

I successfully filled my bobox. But now I'm trying to set default value for bobox. 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="bobox"
 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.

I successfully filled my bobox. But now I'm trying to set default value for bobox. 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="bobox"
 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.

Share Improve this question edited Dec 23, 2014 at 10:09 ManirajSS 2,3755 gold badges29 silver badges51 bronze badges asked Dec 10, 2014 at 14:18 user3573096user3573096 311 gold badge1 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

I assume you look for the index configuration option.

Likely the problem is in your model definition, i.e. what you are binding to the bobox.

According with your definition your JavaScript should be something like:

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.

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.

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:

<input id="type"
    data-role="bobox"
    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./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./2014.3.1119/styles/kendo.mon.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic./2014.3.1119/styles/kendo.default.min.css" />
<script src="http://cdn.kendostatic./2014.3.1119/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic./2014.3.1119/js/kendo.all.min.js"></script>

<input id="cbox"
       data-role="bobox"
       data-value-primitive="true"
       data-auto-bind="true"
       data-text-field="ProductName"
       data-value-field="ProductID"
       data-bind="value: Id, source: dataSourceType">

You can achieve like this.

var bobox = $("#kendoitems").data("kendoComboBox");
bobox.select(1);

you need to pass the index value to select(index).

Refer this http://jsfiddle/NdPze/63/

本文标签: javascriptKendo Ui comboboxset default valueStack Overflow