Polymer 2.x - 如何使这个搜索过滤器工作?

时间:2017-12-17 23:32:35

标签: polymer custom-component polymer-2.x custom-element

我想在Polymer 2.x网络应用中为元素实现搜索过滤器。
我在下面的Polymer 1.x中有这个代码,我不知道如何使它在Polymer 2.x中运行

如何从下面的Polymer 1.x中创建代码,以处理此Polymer 2.x页面:

my-home.html |脚本区域

  <script>
    class Myhome extends Polymer.Element {
      static get is() { return 'my-home'; }
    }

    window.customElements.define(Myhome.is, Myhome);
  </script>

Polymer 1.x的搜索过滤器代码:

HTML

 <div class="app-grid" style="display: flex;">
    <template items="[[data]]">
      <div class="item horizontal wrap layout" style="width: 300px; height: 300px; margin: 2%;">
        <paper-card heading="[[item.title]]" image="http://lorempixel.com/300/200">
           <div class="card-content">[[item.description]]</div>
           <div class="card-actions">
             <paper-button>Button</paper-button>
           </div>
        </paper-card>
      </div>
    </template>
  </div>

脚本

<script>
    Polymer({
      is: 'my-home',
      properties:{
          defaultData:{
            type:Array,
            value:[{title:'Color picture',description:'An RGB picture'},
              {title:'Grey image',description:'That\'s a grey picture'},
              {title:'3',description:'this is content 3'}]
          },
          data:{
            type:Array
          }
        },
        ready:function(){
          this.data = this.defaultData;
          this.$.search.addEventListener('keyup',this.searchChanged.bind(this));
        },
        searchChanged:function(e){
          var querySearch = this.$.search.value.toLowerCase();
          if(querySearch == ''){
            this.data = this.defaultData;
          }else{
            this.data = this.defaultData.filter(function(item){
              return item.title.toLowerCase().indexOf(querySearch) !== -1 || item.description.toLowerCase().indexOf(querySearch)  !== -1;
            });
          }
        }
    });
  </script>

1 个答案:

答案 0 :(得分:1)

您的许多脚本都必须更改,最重要的可能是升级到基于类的元素。 See the relevant section in the Polymer upgrade guide.这将包含adding a call to super.ready()作为ready()功能的第一行