来自iron-ajax响应的纸张自动完成建议来源

时间:2018-01-20 07:32:25

标签: polymer

我已使用paper-autocomplete-suggestions创建自动填充输入,我想从iron-ajax响应中获取来源,这是我的代码:

<iron-ajax id="procedureSource"
                   url="api.json">
</iron-ajax>
<template is="dom-repeat" id="sub-intents" items="{{data.data}}">
     <div class="autocomplete-wrapper">
          <paper-input name="sub_intent[procedure_id]" id="selectProcedure[[item.id]]" label="Select Procedure" on-keypress="_checkProcedureKeyPress"></paper-input>
          <paper-autocomplete-suggestions id="selectProcedureSuggestion[[item.id]]" for="selectProcedure[[item.id]]" remote-source></paper-autocomplete-suggestions>
      </div>
</template>

我在on-keypress上使用paper-input事件来触发iron-ajax请求,输入值长度应该等于或大于3.这是我的脚本:

<script>
Polymer({
     is: 'input-autocomplete',
     _checkProcedureKeyPress: function (event){
     var keyword = event.target.value;
     var target_id = event.target.closest('paper-input').id.replace('selectProcedure', '');
           if(keyword.length >= 3){
               this.$.procedureSource.params = {name: keyword};
               var request = this.$.procedureSource.generateRequest();
               var p = request.completes;

               p.then(function(xhr, response) {
                   var data = request.response;
                   var autocompleteSuggestions = document.querySelector('#selectProcedureSuggestion' + target_id);

                   autocompleteSuggestions.source = data;
                   autocompleteSuggestions.addEventListener('autocomplete-selected', function (event) {
                       var selected = event.detail.text;
                       console.log(selected);
                   });
               })
           }
})
</script>

自动填充功能未显示,我确信源代码可以从iron-ajax响应,元素目标(paper-autocomplete-suggestion元素)获得。

2 个答案:

答案 0 :(得分:2)

看起来此组件仅在remoteSource is false时处理source,并且必须在关联的source获取值之前设置<input>(例如,通过静态数据) 。否则使用remoteSource,您必须手动设置建议数据(在处理来自远程源的原始数据之后)。

在您的情况下,虽然直接设置_suggestions(受保护的属性)主要起作用,但可能建议使用元素的公共API(suggestions()),以便内部{{3} }。

indices are properly reset

答案 1 :(得分:1)

我发现了.. 必须将数据分配到_suggestions属性,而不是source属性

var data = request.response;
var autocompleteSuggestions = document.querySelector('#selectProcedureSuggestion' + target_id);
autocompleteSuggestions._suggestions = data;
autocompleteSuggestions.addEventListener('autocomplete-selected', function (event) {
   var selected = event.detail.text;
});