【问题标题】:Sort Multiple Fields with jQuery Tablesorter Using Select Field使用 jQuery Tablesorter 使用 Select Field 对多个字段进行排序
【发布时间】:2014-04-24 15:00:18
【问题描述】:

我正在使用 Mottie 的 Tablesorter 分支:https://github.com/Mottie/tablesorter

我想要做的是使用实际表之外的多个选择字段对表进行过滤或排序。所以我不想使用表格标题下显示的过滤器。

我找到了一些可以一一发挥作用的选项。比如将这两个添加到我的tablesorter脚本中:

 jQuery(".selectParty").bind('change', function (e) {
     var cols=[]
     cols[2] = $(this).val()
    jQuery('table').trigger('search', [cols]);
 })


 jQuery(".selectState").bind('change', function (e) {
     var cols=[]
     cols[0] = $(this).val()
    jQuery('table').trigger('search', [cols]);
 })

这里是表格上方的 HTML 选择字段:

     <select class="selectState tablesorter-filter" data-column="0">
     <option class="reset" value="">No Filter</option>
     <option>Illinois</option>
     <option>Indiana</option>
     </select>    

     <select class="selectParty tablesorter-filter" data-column="2">
     <option class="reset" value="">No Filter</option>
     <option>Democratic</option>
     <option>Republican</option>
     </select>

       <table class="tablesorter"><thead><tr><th data-placeholder="Search" class="filter-match">State</th><th data-placeholder="Search" class="filter-match">Name</th><th data-placeholder="Search" class="filter-match">Party</th></tr></thead><tbody>

这些单独工作正常,但假设我先选择按伊利诺伊排序,它会按伊利诺伊对表格进行排序。但随后我继续按 Democractic 排序,它重置了 State 过滤器以显示所有带有Democratic 的行。

目前是否可以让 tablesorter 让您按表格本身之外的多个字段进行过滤?我见过一些例子,你可以通过按住 Shift 来按多列排序,但这在平板电脑上不起作用……非常感谢任何帮助!

【问题讨论】:

    标签: jquery tablesorter


    【解决方案1】:

    您可以修改特定列的过滤器,而不是清除 col 变量,如下所示:

    var cols = [];
    
    jQuery(".selectParty").bind('change', function (e) {
        cols[2] = $(this).val();
        jQuery('table').trigger('search', [cols]);
    });
    
    jQuery(".selectState").bind('change', function (e) {
        cols[0] = $(this).val();
        jQuery('table').trigger('search', [cols]);
    });
    

    【讨论】:

    • 谢谢莫蒂!我是这一切的新手,并且使用编程界非常乐于助人的人自学 jQuery 和 PHP!
    猜你喜欢
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多