javascript - JQuery Tablesorter - 通过单击链接进行过滤

标签 javascript jquery tablesorter

我正在使用 @Mottie 的优秀 Tablesorter 分支,并且希望能够使用外部链接过滤表列。

  • 这并不是绝对必要的,但我想通过多次单击来打开和关闭过滤器。或者,我可以随时添加一个所有记录链接来重置列。
  • 我不需要将过滤器组合到单个列中。换句话说,数据不会同时是 1 月和 10 月。

我找到了一个表格排序 external link demo ,但它是排序的,而不是过滤器,并且它不会切换。

我还找到了table filter with buttons demo这是非常接近的。但是,正如我所提到的,我真的很喜欢链接,希望在可能的情况下切换链接,并且不需要组合过滤器。

提前致谢。

最佳答案

这实际上比我想象的要容易得多。这是一个工作demo直接来自上面 Mottie 的演示代码。我用链接替换了按钮,重命名了关联的类,使其更有意义,并替换了 JavaScript 函数上的类以匹配链接上的类。

公平警告:我并不声称了解一切,因此我的修改可能会出现非常愚蠢的错误。

 $('.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);
});

各个列中的过滤器组合在一起,但我目前只需要一个列过滤器,所以这对我来说并不是真正的问题。

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

$("#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);
});

关于javascript - JQuery Tablesorter - 通过单击链接进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753764/

相关文章:

javascript - 仅当新背景图像存在时才更改背景图像

javascript - 如何用Jquery触发一个js函数

javascript - Setinterval 动画有时暂停

javascript - 来自 tablesorter 的过滤器小部件不起作用

javascript - 如何将 tablesorter 中的低设置为默认值?

javascript - 如果映射项为 null 或为空,则通过连接 2 个其他属性使用替代值

javascript - 在 UI-Router 中以抽象状态填充公共(public) View

javascript - 在 ADF 中使用具有动态生成的 ID 的 Jquery

javascript - IE7 标准文档模式下的 SPAN/DIV 高度不正确

php - 根据用户在提交之前输入的文本框(数字)值对表格进行排序