【问题标题】:Filter table row in polymer element聚合物元素中的过滤表行
【发布时间】:2015-06-21 10:48:56
【问题描述】:

我的聚合物元素中有一个表格,我希望它根据输入字段中提供的关键字过滤行

<polymer-element name="my-element">
<template>
<paper-input label="Search" floatingLabel="false" class="search-main" on-keyup="{{search}}"></paper-input>
<table id="table">
     <thead>
        <tr>
            <th>First Name</th>
            <th>Middle Name</th>
            <th>Last Name</th>
        </tr>
     </thead>
     <tbody>
         <tr>
            <td>John</td>
            <td>Cr</td>
            <td>Smith</td>
        </tr>
        <tr>
            <td>keety</td>
            <td>cros</td>
            <td>bran</td>
        </tr>
     </tbody>
</table>
</template>
<script>
    Polymer('my-element', {
        search: function() {
            var rows = this.$.table.querySelector('tr');
            var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ')',
            reg = RegExp(val, 'i'),
            text;

            $(rows).show().filter(function() {
                text = $(this).text().replace(/\s+/g, ' ');
                return !reg.test(text);
            }).hide();                  
       }
    })
    ;
</script>
</polymer-element>

没有收到任何控制台错误但过滤器不起作用

我是聚合物新手,所以请告诉我我做错了什么?

【问题讨论】:

  • 根据 Polymer 文档,this.$.table 引用应引用 id 为 table 的元素,但是您的表没有 id 属性。尝试将&lt;table&gt; 更改为&lt;table id="table"&gt;
  • 哦,这是在我的代码中,不小心被我删除了……我已经编辑了我的帖子

标签: jquery polymer tablefilter


【解决方案1】:
 <polymer-element name="my-element">
 <template>
 <paper-input label="Search" floatingLabel="false" value="{{valHeader}}"    class="search-main" on-keyup="{{search}}"></paper-input>
 <table id="table">
 <thead>
    <tr>
        <th>First Name</th>
        <th>Middle Name</th>
        <th>Last Name</th>
    </tr>
 </thead>
 <tbody>
     <tr>
        <td>John</td>
        <td>Cr</td>
        <td>Smith</td>
    </tr>
    <tr>
        <td>keety</td>
        <td>cros</td>
        <td>bran</td>
    </tr>
 </tbody>
 </table>
 </template>
 <script>
Polymer('my-element', {
  search: function() {
            var rows = this.$.table.querySelectorAll('tbody tr');
            var val = '^(?=.*\\b' + $.trim(this.valHeader).split(/\s+/).join('\\b)(?=.*\\b') + ')',
            reg = RegExp(val, 'i'),
            text;

            $(rows).show().filter(function() {
                text = $(this).text().replace(/\s+/g, ' ');
                return !reg.test(text);
            }).hide();                  
       }
})
;
</script>
</polymer-element>

{{valHeader}} 在您的笔记列表元素中创建一个属性,该属性绑定到纸张输入的输入值,我们可以使用 this.valHeader 访问它

【讨论】:

    【解决方案2】:

    方法querySelector 仅返回第一个元素,在您的情况下是&lt;thead&gt; 中的&lt;tr&gt;。您需要改用querySelectorAll 并在&lt;tbody&gt; 中仅选择&lt;tr&gt;

    替换:

    var rows = this.$.table.querySelector('tr');
    

    与:

    var rows = this.$.table.querySelectorAll('tbody tr');
    

    另外,$(this).val() 似乎不是访问&lt;paper-input&gt; 值的正确方法。您需要将&lt;paper-input&gt; 更改为:

    &lt;paper-input label="Search" floatingLabel="false" value="{{searchVal}}" class="search-main" on-keyup="{{search}}"&gt;&lt;/paper-input&gt;

    然后您将能够在您的代码中以this.searchVal 而不是$(this).val() 的形式访问其值。

    【讨论】:

    • 已经尝试过你的代码,但我的行仍然没有被过滤
    • 是的,在 的情况下我们不能使用 $(this).val()...我已经发布了我的问题的答案,但是谢谢指出错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多