jQuery实时过滤,如何在匹配时返回整行?

时间:2016-01-04 13:37:19

标签: javascript jquery html

我正在使用jQuery实时过滤器插件对整个表格进行即时搜索。该插件工作正常,但它按单元格过滤,我想搜索整个行(tr)匹配而不只是一个单元格。

HTML:

<h2 class="sub-header">List of Enabled Alarms</h2>
            <div id="prefetch">
                <input class="typeahead" type="text" placeholder="Alarm name search" id="alarm-search">
            </div>
      <div class="table-responsive">
        <table class="table table-striped" id="alarm-table">
          <thead>
            <tr>
              <th>Id</th>
              <th>Name</th>
              <th>Due Date</th>
              <th>Creation Date</th>
              <th>Frequency</th>
              <th>Enabled</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
                <tr>
                    <td>60</td>
                    <td>Alarm dev test</td>
                    <td>29-12-2015</td>
                    <td>28-12-2015</td>
                    <td>ExactTime</td>
                    <td>Enabled</td>
                    <td><a href="/alarm/load?id=60" class="btn btn-success" role="button">Edit</a> <a href="/alarm/delete?id=60" class="btn btn-danger" role="button">Delete</a></td>
                </tr>
                <tr>
                    <td>61</td>
                    <td>Testing email</td>
                    <td>05-01-2016</td>
                    <td>04-01-2016</td>
                    <td>ExactTime</td>
                    <td>Enabled</td>
                    <td><a href="/alarm/load?id=61" class="btn btn-success" role="button">Edit</a> <a href="/alarm/delete?id=61" class="btn btn-danger" role="button">Delete</a></td>
                </tr>
          </tbody>
        </table>

JAVASCRIPT:

<script type="text/javascript">
$('#alarm-table').liveFilter('#alarm-search', 'tbody tr', {
      filterChildSelector: 'tr'
    });
</script>

此代码的结果是部分行信息。如果我写a它会返回匹配但不是整行的所有单元格,如我所料。如果可能,我只想按列name进行搜索。

1 个答案:

答案 0 :(得分:1)

根据此plugin,您可以为匹配元素传递过滤功能。请尝试以下代码。

<script type="text/javascript">
    $('#alarm-table').liveFilter('#alarm-search', 'tbody tr', {
        filter: function(el, val){
            return $(el).find('td:eq(1)').text().toUpperCase().indexOf(val.toUpperCase()) >= 0;
        }
    });
</script>

或者更好的是,如果您可以为包含警报名称的单元格指定任何类名。在这种情况下,你的HTML看起来像:

<h2 class="sub-header">List of Enabled Alarms</h2>
            <div id="prefetch">
                <input class="typeahead" type="text" placeholder="Alarm name search" id="alarm-search">
            </div>
      <div class="table-responsive">
        <table class="table table-striped" id="alarm-table">
          <thead>
            <tr>
              <th>Id</th>
              <th>Name</th>
              <th>Due Date</th>
              <th>Creation Date</th>
              <th>Frequency</th>
              <th>Enabled</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
                <tr>
                    <td>60</td>
                    <td class="alarmName">Alarm dev test</td>
                    <td>29-12-2015</td>
                    <td>28-12-2015</td>
                    <td>ExactTime</td>
                    <td>Enabled</td>
                    <td><a href="/alarm/load?id=60" class="btn btn-success" role="button">Edit</a> <a href="/alarm/delete?id=60" class="btn btn-danger" role="button">Delete</a></td>
                </tr>
                <tr>
                    <td>61</td>
                    <td class="alarmName">Testing email</td>
                    <td>05-01-2016</td>
                    <td>04-01-2016</td>
                    <td>ExactTime</td>
                    <td>Enabled</td>
                    <td><a href="/alarm/load?id=61" class="btn btn-success" role="button">Edit</a> <a href="/alarm/delete?id=61" class="btn btn-danger" role="button">Delete</a></td>
                </tr>
          </tbody>
        </table>

并使用以下脚本:

<script type="text/javascript">
    $('#alarm-table').liveFilter('#alarm-search', 'tbody tr', {
        filter: function(el, val){
            return $(el).find('.alarmName').text().toUpperCase().indexOf(val.toUpperCase()) >= 0;
        }
    });
</script>