簡體   English   中英

如何以編程方式更改vaadin-combo-box中的selectedItem

[英]how change selectedItem in vaadin-combo-box in polymer programmatically

我對聚合物有疑問

 valueCategoryChange: function() { this.set("mycategory", this.$.comboCategory.selectedItem); }, 
 <vaadin-combo-box on-value-changed="valueCategoryChange" id="comboCategory" items="{{categories}}" item-value-path="id" value="{{category22}}" item-label-path="display" required> 

如果我從組合框選擇一切正常,並且valueCategoryChange()顯示selectedItem。 但是當我從值中以編程方式從組合框中選擇一項時, this.category22 = data.CatId; ,該項目顯示在組合框中,但在valueCategoryChange函數中, this.$.comboCategory.selectedItemnull請幫助我

如果您使用如下所示:

<vaadin-combo-box selected-item="{{mycategory}}" id="comboCategory" items="{{categories}}" item-value-path="id" value="{{category22}}" item-label-path="display" required>
<div> Selected Item is [[mycategory]]</div>

那么您將擁有一個選定的item屬性作為mycategory 另外,您將不需要valueCategoryChange函數和on-value-changed="valueCategoryChange"事件。 此外,如果您需要設置事件並想使用功能,可以使用以下功能:

valueCategoryChange: function() {
  console.log(this.mycategory);  // is the selected item that you can use 
  // this.set("mycategory", this.mycategory)  will not be useful :)) 
},

(在以下代碼段下運行)或DEMO

  HTMLImports.whenReady( ()=> { class XFoo extends Polymer.Element { static get is() { return 'x-foo'; } } window.customElements.define(XFoo.is, XFoo) }) 
  <head> <script <base href="https://cdn.rawgit.com/download/polymer-cdn/2.6.0.2/lib/"> <script src="webcomponentsjs/webcomponents-lite.js"></script> <link rel="import" href="polymer/polymer.html"> <link rel="import" href="https://cdn-origin.vaadin.com/vaadin-core-elements/master/vaadin-combo-box/vaadin-combo-box.html"> <link rel="import" href="https://cdn-origin.vaadin.com/vaadin-core-elements/master/vaadin-grid/all-imports.html"> </head> <body> <x-foo id="xfoo"items="{{categories}}"></data-table-d> <dom-module id="x-foo"> <template> <vaadin-combo-box selected-item="{{mycategory}}" id="comboCategory" items="{{categories}}" value="{{category22}}" ></vaadin-combo-box><br><br> <div> Selected Item is [[mycategory]]</div> <script> var el = document.getElementById('xfoo'); el.categories=['Cat1', 'Cat2','Cat3']; </script> </template> </dom-module> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM