使用Polymer根据输入值过滤表

时间:2013-09-16 12:35:24

标签: data-binding filter polymer html-table

Polymer中有什么东西或多或少等同于AngularJS'过滤器'功能吗?我查看了模板绑定,但找不到根据输入字段的值来过滤表格的方法......

<input value="{{ID}}">

<table [==> some Polymer magic here involving {{ID}}]>
   <tr>
       <th>ID</th>
       <th>VALUE</th>
   </tr>
   <tr>
       <td>FOO</td>
       <td>1</td>
   </tr>
   <tr>
       <td>BOO</td>
       <td>2</td>
   </tr>
   <tr>
       <td>FAA</td>
       <td>3</td>
   </tr>
   <tr>
       <td>BAA</td>
       <td>4</td>
   </tr>
</table>

然后在输入字段中键入“F”将使表格仅显示值等于1和3的行,而继续使用“O”只会显示“1”...

1 个答案:

答案 0 :(得分:5)

执行此操作(今天)的最佳方法是从过滤后的数据模型生成表格,并使用聚合物on-*处理程序对输入的按键做出反应

<polymer-element name="my-element">
  <template>
    <input type="text" on-keyup="{{filter}}">
    <table>
      <tr><th>ID</th><th>VALUE</th></tr>
      <template repeat="{{d in filteredData}}">
        <tr><td>{{d[0]}}</td><td>{{d[1]}}</td></tr>
      </template>
    </table>
  </template>
  <script>
    Polymer('my-element', {
      created: function() {
        this.data = [
          ['FOO', 1], ['BOO', 2], ['FAA', 3], ['BAA', 4]
        ]
        this.filteredData = this.data;
      },
      filter: function(e, detail, sender) {
        // Tests for anywhere in the string. Modify to match just the beginning.
        var regex = new RegExp(sender.value, 'i');
        this.filteredData = this.data.filter(function(d, idx, array) {
          return regex.test(d[0]);
        });
      }
    });
  </script>
</polymer-element>

<my-element></my-element>

演示:http://jsbin.com/parive/2/edit?html,output

将来,我们将在表达式中添加对过滤器函数的一流支持。请参阅12

相关问题