【问题标题】:Select items of kendo-combobox from other controls从其他控件中选择 kendo-combobox 的项目
【发布时间】:2018-01-17 12:02:24
【问题描述】:

我有一个kendo-combobox。 Keyboard navigation works very well 当焦点位于 kendo-combobox 时。

但我想通过kendo-combobox 附近的按钮选择下一个可用项目。让我展示一下我的代码:

HTML:

<p>Favorite sport:</p>  
<kendo-combobox [data]="listItems" [allowCustom]="true" #superComboBox>
</kendo-combobox>

<button (click)="selectUpperItem()">
    Up
</button>
<button (click)="selectLowerItem()">
    Down
</button>

JavaScript

public listItems: Array<string> = 
[
   "Baseball", "Basketball", "Cricket", "Field Hockey", "Football", "Table Tennis", 
   "Tennis", "Volleyball"
];

selectUpperItem(){
   //pseudocode: this.superComboBox.nextUpperItem();    
   //How can I accomplish this?
}

selectLowerItem(){
   //pseudocode: this.superComboBox.nextLowerItem();    
   //How can I accomplish this?
}

控件如下所示:

因此,例如,当我单击Up 按钮时,应在kendo-combobox 处选择下一个可用项目。

是否可以通过kendo-combobox附近的按钮选择下一个可用项目?

【问题讨论】:

    标签: angular kendo-ui telerik kendo-ui-angular2 kendo-combobox


    【解决方案1】:

    如果您尝试使用向上/向下按钮选择选项,您可以执行以下操作。

    使用[(ngModel)] 跟踪当前选择:

    <kendo-combobox 
        [data]="listItems" 
        [allowCustom]="allowCustom" 
        [(ngModel)]="currentSelection">
    </kendo-combobox>
    

    然后在您的.ts 中检查currentSelection 是否为undefined 并相应地操作选择/索引:

    private string: currentSelection;
    private number: currentIndex;
    
    private selectUpperItem() {
      if (this.currentSelection == undefined) {
        this.currentSelection = this.listItems[this.listItems.length - 1];
        this.currentIndex = this.listItems.length - 1;
      } else {
        this.currentIndex = this.listItems.indexOf(this.currentSelection);
        this.currentSelection = this.listItems[this.currentIndex - 1];
      }
    }
    
    private selectLowerItem() {
      if (this.currentSelection == undefined) {
        this.currentSelection = this.listItems[0];
        this.currentIndex = 0;
      } else {
        this.currentIndex = this.listItems.indexOf(this.currentSelection);
        this.currentSelection = this.listItems[this.currentIndex + 1];
      }
    }
    

    设置为如果undefined 并单击向上将选择最后一个选项,当undefined 和向下单击将选择第一个选项。

    检查这个plunker。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多