简体   繁体   English

AngularJS TypeScript Kendo UI DropDownList-选择默认值的错误

[英]AngularJS TypeScript Kendo UI DropDownList - bug with default value selected

I have list of objects for items and everything works fine, but now I want to add default value selected , but I have issues with it. 我有项目的对象列表,并且一切正常,但是现在我想添加默认值selected ,但是我遇到了问题。

Here's the HTML code for the drop-down: 这是下拉菜单的HTML代码:

<select kendo-drop-down-list 
        k-options="selectItems" 
        k-ng-model="selectedItem">
</select>

And in AngularJS (using TypeScript) I'm making the drop-down: 在AngularJS(使用TypeScript)中,我正在下拉列表:

this.itemsToSelect = [{id: 1, name: "One"}, {id: 2, name: "Two"}];

this.selectItems = {
    optionLabel: "Select items...",
    dataTextField: "name",
    dataValueField: "id",
    dataSource: new kendo.data.DataSource({
      transport: {
        read: (options) => {
          options.success(this.itemsToSelect);
        }
      }
    }),
    value: this.itemsToSelect[0]
  };

I don't have default value selected with this. 我没有为此选择默认值。

I tried the code from their documentation and it works with items as strings, but with items as objects works when I say something like: 我从他们的文档中尝试了该代码,它可以将项目作为字符串使用,但是当我说类似以下内容时,将项目作为对象可以使用:

value: this.itemsToSelect[0].id

So, their code looks like this: 因此,他们的代码如下所示:

<script>
    let items = [{id: 1, name: "one"}, {id: 2, name: "two"}];

    $("#dropdownlist").kendoDropDownList({
        dataTextField: "name",
        dataValueField: "id",
        dataSource: items,
        value: items[1].id
    });
</script>

I tried this in my code and it doesn't work. 我在代码中尝试过此方法,但无法正常工作。

Code doesn't have any errors. 代码没有任何错误。 Any suggestions? 有什么建议么?

EDIT 编辑

What I can do is this: 我能做的是:

this.selectedItem = this.itemsToSelect[0];

I've set k-ng-model in the HTML and I can just set the selected value like this. 我在HTML中设置了k-ng-model ,就可以像这样设置所选的值。

But, is there a way to do it through value in this.selectItems object? 但是,有没有办法通过this.selectItems对象中的来做到这this.selectItems

Make a local object representing your default value, like 制作一个代表您的默认值的本地对象,例如

{ 
id: 1,
name: "bob"
}

then on the control set autoBind to false. 然后在控件上将autoBind设置为false。

https://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist/configuration/autobind https://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist/configuration/autobind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM