【问题标题】:tablesorter - hide multiple tables based on dropdown selecttablesorter - 根据下拉选择隐藏多个表
【发布时间】:2014-10-11 20:54:13
【问题描述】:

我正在使用 tablesorter 插件。过滤器小部件在单个表上效果很好。现在我正在尝试用 tablesorter 做两件事。

  1. 我正在尝试根据下拉列表的值隐藏整个表格。我得到了以下代码的解决方案,但我想知道 tablesorter 中是否有更好的方法:

    $("#search_league").change(function () {
        $("table").show();
        $("table").each(function(){
            if($("#search_league").val() != $(this).attr('id')){
                $(this).hide();
            };
        });
    });
    
  2. 我觉得第二个更难。我正在尝试隐藏所有不包含特定值的表我正在使用下拉列表来选择值。所有不包含该值的表都应隐藏。

这些表是由 mysql 查询和 php.ini 创建的。这些是表头。

    echo "<table class='tablesorter' id='".$lid."' style='width:80%'>
<thead>
<tr>            
        <th colspan='2'><a href='league.php?lid=".$lid."'>".$getlid['LEA']."</a></th>
        <th class='num_caption' title='Spiele'>Sp</th>
        <th class='num_caption'  title='Siege'>S</th>
        <th class='num_caption'  title='Niederlagen'>N</th>
        <th class='num_caption'  title='Wertungspunkte'>WP</th>
        <th class='num_caption'  colspan='2' title='Korbpunkte'>Punkte</th>         
        <th class='num_caption'  title='Differenz Korbpunkte'>Dif</th>                          
        <th class='num_caption'  title='Spiele verloren mit Spielwertung'>Wert</th>     
        <th style='display:none';>Team</th>
    </tr>
</thead>";

我尝试解决 #1 之类的问题,但这似乎不是正确的方法。有什么想法吗?

【问题讨论】:

标签: jquery drop-down-menu filter tablesorter


【解决方案1】:

对于第一季度,您的回答对我来说看起来很棒。但是,MDJ 有一个好处 - 结果相同,工作量更少。


问题二:

使用.each(className) 循环遍历表格,使用.filter() 返回给定表格是否包含表格单元格中任何位置所需的文本。

这是一个工作简化示例:

jsFiddle Demo slight improvement - colorize bg

HTML:

Hide all tables NOT containing: <select id="mysel">
    <option value="go">Choose One:</option>
    <option value="car">car</option>
    <option value="bus">bus</option>
    <option value="apple">apple</option>
    <option value="joy">joy</option>
    <option value="all">show all</option>
</select>
<table class='tablesorter' id='id_a'>
    <thead>
        <tr>
            <th class="aa"><a href='league.php?lid=17'>HREF</a>
            </th>
            <th class='num_caption' title='Spiele'>Sp</th>
            <th class='num_caption' title='Siege'>S</th>
            <th class="bb">Team</th>
        </tr>
        <tr><td class="aa">HREF</td><td>car</td><td>bus</td><td class="bb">aa1</td></tr>
        <tr><td class="aa">HREF</td><td>car</td><td>bus</td><td class="bb">aa2</td></tr>
        <tr><td class="aa">HREF</td><td>car</td><td>bus</td><td class="bb">aa3</td></tr>
    </thead>
</table>

<table class='tablesorter' id='id_b'>
    <thead>
        <tr>
            <th class="aa"><a href='league.php?lid=17'>HREF</a>
            </th>
            <th class='num_caption' title='Spiele'>Sp</th>
            <th class='num_caption' title='Siege'>S</th>
            <th class="bb">Team</th>
        </tr>
        <tr><td class="aa">HREF</td><td>joy</td><td>joy</td><td class="bb">bb1</td></tr>
        <tr><td class="aa">HREF</td><td>joy</td><td>joy</td><td class="bb">bb2</td></tr>
        <tr><td class="aa">HREF</td><td>joy</td><td>joy</td><td class="bb">bb3</td></tr>
    </thead>
</table>

<table class='tablesorter' id='id_c'>
    <thead>
        <tr>
            <th class="aa"><a href='league.php?lid=17'>HREF</a>
            </th>
            <th class='num_caption' title='Spiele'>Sp</th>
            <th class='num_caption' title='Siege'>S</th>
            <th class="bb">Team</th>
        </tr>
        <tr><td class="aa">HREF</td><td>bus</td><td>apple</td><td class="bb">cc1</td></tr>
        <tr><td class="aa">HREF</td><td>bus</td><td>apple</td><td class="bb">cc2</td></tr>
        <tr><td class="aa">HREF</td><td>joy</td><td>joy</td><td class="bb">cc3</td></tr>
    </thead>
</table>

<table class='tablesorter' id='id_d'>
    <thead>
        <tr>
            <th class="aa"><a href='league.php?lid=17'>HREF</a>
            </th>
            <th class='num_caption' title='Spiele'>Sp</th>
            <th class='num_caption' title='Siege'>S</th>
            <th class="bb">Team</th>
        </tr>
        <tr><td class="aa">HREF</td><td>car</td><td>car</td><td class="bb">a1</td></tr>
        <tr><td class="aa">HREF</td><td>bus</td><td>bus</td><td class="bb">a2</td></tr>
        <tr><td class="aa">HREF</td><td>apple</td><td>joy</td><td class="bb">a3</td></tr>
    </thead>
</table>

jQuery/js:

$('#mysel').change(function(){
    var i = this.value;
    if (i=='go') return false;
    if (i=='all') {$('.tablesorter').show();$('#mysel').val('go');return false;}

    $('.tablesorter').each(function(){
        var tid = this.id;
        var xxx = hasitem(tid, i);
//alert(xxx[0]);
        if (typeof xxx[0] == 'undefined') {
            $('#'+tid).hide();
        }else{
            $('#'+tid).show();
        }
    });
});
function hasitem(tbl, item){
//alert('Table = ' +tbl+ '  Item = ' +item);
    var tableRow = $("#"+tbl+ " td").filter(function() {
        return $(this).text() == item;
    }).closest("tr");
    return tableRow;
}

CSS(使其漂亮):

table {border:1px solid grey;border-collapse:collapse;width:200px;}
table {margin:20px;}
th, td {border:1px solid grey;}
th, td {width:20px;text-align:center;}
.aa {width:50px;}
.bb {display:none;}
#id_a{background:wheat;}
#id_b{background:lavender;}
#id_c{background:paleyellow;}
#id_d{background:palegreen;}

【讨论】:

  • 这似乎是正确的方法。您如何将搜索集中在一个特定的列上?
  • 这完全是另一个问题,需要付出很多努力才能回答。请单独提问。
  • 我在一个单独的问题中发布了它。以防万一你能帮我解决这个问题。[link]stackoverflow.com/questions/25389293/…
  • 你的两个问题好像是一样的,还是我漏了什么?我回复了你的other question
【解决方案2】:

试试这个

$("#search_league").change(function () {
    $("table").hide();
    var tablesToShow=$("#search_league").val();
    $("#"+tablesToShow).show();

});

});

基本上与您在没有 for 循环的情况下尝试做的相反。

【讨论】:

  • 我喜欢这个。我现在正在使用你的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 2011-10-23
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
相关资源
最近更新 更多