jquery - tablesorter - hide multiple tables based on dropdown select -


i using tablesorter plugin. filter widget works great on single table. trying 2 things tablesorter.

  1. i trying hide entire table based on value of dropdown list. got solution following code, wondering if there better way in tablesorter:

    $("#search_league").change(function () {     $("table").show();     $("table").each(function(){         if($("#search_league").val() != $(this).attr('id')){             $(this).hide();         };     }); }); 
  2. the second 1 find more difficult. trying hide tables dont contain value using dropdown select value. tables dont contain value should hidden.

the tables created mysql query , php. these table header.

    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>"; 

i tried solve problem #1 doesnt seem right approach. ideas?

for q1, answer looks great me. however, mdj has point - same result, less work.


question two:

use .each(classname) loop through tables, , .filter() return whether given table contains required text anywhere in tablecell.

here working simplified example:

jsfiddle demo slight improvement - colorize bg

html:

hide 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 = 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 (to make pretty):

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;} 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -