简体   繁体   English

JQuery Tablesorter - 点击链接过滤

[英]JQuery Tablesorter - Filter by clicking link

I'm using @Mottie's excellent fork of Tablesorter and would like to be able to filter table column(s) with external links. 我正在使用@ Mottie的Tablesorter的优秀分支,并希望能够使用外部链接过滤表列。

  • It isn't strictly necessary, but I'd like to make multiple clicks toggle the filter on and off. 这不是绝对必要的,但我想多次点击以打开和关闭过滤器。 Alternatively, I could always add an All Records link that resets the column(s). 或者,我总是可以添加一个重置列的All Records链接。
  • I don't need to combine filters in a single column. 我不需要在单个列中组合过滤器。 In other words, the data wouldn't be both January and October. 换句话说,数据不会是1月和10月。

I found a table sort with external link demo , but that one sorts, not filters, and it doesn't toggle. 我找到了一个带有外部链接演示的表格排序 ,但是那个排序,而不是过滤器,并且它不会切换。

I also found a table filter with buttons demo which is pretty close. 我还发现了一个带有按钮演示表格过滤器,非常接近。 However, as I mentioned, I'd really like links instead, would like to have the links toggle if possible, and don't need the filters to combine. 但是,正如我所提到的,我真的很喜欢链接,如果可能的话,想要链接切换,并且不需要过滤器来组合。

Thanks in advance. 提前致谢。

This was actually a lot easier than I thought. 这实际上比我想象的容易得多。 Here's a working demo that came directly from Mottie's demo code above. 这是一个直接来自上面的Mottie演示代码的工作演示 I replaced the buttons with links, renamed the associated class so it made more sense and replaced the class on the JavaScript function to match the one on the links. 我用链接替换了按钮,重命名了相关的类,因此它更有意义,并替换了JavaScript函数上的类以匹配链接上的类。

Fair warning: I don't claim to know everything, so my modifications could have very silly errors. 公平警告:我并不声称知道一切,所以我的修改可能会有非常愚蠢的错误。

 $('.link-filter').click(function() {
        var filters = $('table').find('input.tablesorter-filter'),
        col = $(this).data('filter-column'),
        txt = $(this).data('filter-text');
    // filters.val('');
    filters.eq(col).val(txt).trigger('search', false);
});

The filters in the various columns combine, but I only need a single column filter at the moment, so that's not really an issue for me. 各列中的过滤器组合在一起,但我现在只需要一个列过滤器,所以这对我来说不是一个问题。

Country:<br>
<a href="#" class="link-filter" data-filter-column="4" data-filter-text="">All Countries</a> | 
<a href="#" class="link-filter" data-filter-column="4" data-filter-text="Netherlands">Netherlands</a> | 
<a href="#" class="link-filter" data-filter-column="4" data-filter-text="Belgium">Belgium</a> | 
<a href="#" class="link-filter" data-filter-column="4" data-filter-text="Germany">Germany</a>
<br /><br />

<table id="festivaloverzichttable" class="tablesorter">
<thead>
<tr>
  <th width="17%" data-placeholder="Search...">Event</th>
  <th width="18%" data-placeholder="Search...">Date</th>
  <th width="9%" data-placeholder="Search...">Duration</th>
  <th width="12%" data-placeholder="Search...">Place</th>
  <th width="10%" data-placeholder="Search...">Country</th>
  <th data-placeholder="Zoek...">Genre(s)</th>
</tr>
</thead>
<tbody>
<tr>
  <td>Event 1</td>
  <td data-date="06-02">TBA</td>
  <td>2</td>
  <td>Oisterwijk</td>
  <td>Netherlands</td>
  <td>Hardstyle</td>
</tr>
<tr>
  <td>Event 2</td>
  <td data-date="10-11">11 October t/m 13 October</td>
  <td>3</td>
  <td>Volkel</td>
  <td>Netherlands</td>
  <td>Pop, Rock, Urban, Electronic</td>
</tr>
<tr>
  <td>Event 3</td>
  <td data-date="06-02">TBA</td>
  <td>1</td>
  <td>Amsterdam</td>
  <td>Netherlands</td>
  <td>Electronic</td>
</tr>
<tr>
  <td>Event 4</td>
  <td data-date="09-01">TBA</td>
  <td>1</td>
  <td>Utrecht</td>
  <td>Netherlands</td>
  <td>Electronic, Urban</td>
</tr>
<tr>
  <td>Event 5</td>
  <td data-date="07-06">6 July - 7 July</td>
  <td>2</td>
  <td>Beek en Donk</td>
  <td>Netherlands</td>
  <td>Electronic, Hardstyle</td>
</tr>

...

</tbody>
</table>​

Javascript 使用Javascript

$("#festivaloverzichttable").tablesorter({
    sortList: [[0, 0]],
    widgets: ['zebra', 'filter', 'saveSort'],
    widgetOptions: {
      filter_reset: 'button.reset'
    }
});

 $('.link-filter').click(function() {
      var filters = $('table').find('input.tablesorter-filter'),
      col = $(this).data('filter-column'),
      txt = $(this).data('filter-text');
      // filters.val('');
     filters.eq(col).val(txt).trigger('search', false);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM