Jquery Session&表格过滤

时间:2010-05-03 15:22:32

标签: jquery html html-table filtering

这是我的Jquery

     <script type="text/javascript">  
 $(function() {
 var from = $.session("from");
var to = $.session("to");

var $th = $('#theTable').find('th');
// had to add the classes here to not grab the "td" inside those tables
var $td = $('#theTable').find('td.bluedata,td.yellowdata');

 $th.hide();
 $td.hide();

if (to == "Select" || from == "Select") {
  // shortcut - nothing set, show everything
  $th.add($td).show();
  return;
}

 var filterArray = new Array();
filterArray[0] = to;
 filterArray[1] = from;

  $.each(filterArray, function(i){
    if (filterArray[i].toString() == "Select")
    {
        filterArray[i] = "";
    }
});   
$($th).each(function(){
    if ($( this,":eq(0):contains('" + filterArray[0].toString() + "')") != null
        && $(this,":eq(1):contains('" + filterArray[1].toString() + "')") != null)
    {
        $(this).show();
    }
});
 $($td).each(function(){
    if ($( this,":eq(0):contains('" + filterArray[0].toString() + "')") != null
        && $(this,":eq(1):contains('" + filterArray[1].toString() + "')") != null)
    {
        $(this).show();
    }
}); 
  });
  </script>

这是我的表

 <table border="1" id="theTable">
    <tr class="headers">
        <th class="bluedata"height="20px" valign="top">63rd St. &amp; Malvern Av. Loop<BR/></th>
        <th class="yellowdata"height="20px" valign="top">52nd St. &amp; Lansdowne Av.<BR/></th>
        <th class="bluedata"height="20px" valign="top">Lancaster &amp; Girard Avs<BR/></th>
        <th class="yellowdata"height="20px" valign="top">40th St. &amp; Lancaster Av.<BR/></th>
        <th class="bluedata"height="20px" valign="top">36th &amp; Market Sts<BR/></th>
            <th class="bluedata"height="20px" valign="top">6th &amp; Market Sts<BR/></th>
        <th class="yellowdata"height="20px" valign="top">Juniper Station<BR/></th>
    </tr>
    <tr>
        <td class="bluedata"height="20px" title="63rd St. &amp; Malvern Av. Loop">
        <table width="100%"><tr><td>12:17am</td></tr><tr><td>12:17am</td></tr><tr><td>12:47am</td></tr></table>
        </td>
        <td class="yellowdata"height="20px" title="52nd St. &amp; Lansdowne Av.">
        <table width="100%"><tr><td>12:17am</td></tr><tr><td>12:17am</td></tr><tr><td>12:47am</td></tr></table>
        </td>
         <td class="bluedata"height="20px" title="Lancaster &amp; Girard Avs">
        <table width="100%"><tr><td>12:17am</td></tr><tr><td>12:17am</td></tr><tr><td>12:47am</td></tr></table>
         </td>
        <td class="yellowdata"height="20px" title="40th St. &amp; Lancaster Av.">
        <table width="100%"><tr><td>12:17am</td></tr><tr><td>12:17am</td></tr><tr><td>12:47am</td></tr></table>
        </td>
        <td class="bluedata"height="20px" title="36th &amp; Market Sts">
        <table width="100%"><tr><td>12:17am</td></tr><tr><td>12:17am</td></tr><tr><td>12:47am</td></tr></table>
        </td>
            <td class="bluedata"height="20px" title="6th &amp; Market Sts">
        <table width="100%"><tr><td>12:17am</td></tr><tr><td>12:17am</td></tr><tr><td>12:47am</td></tr></table>
        </td>
        <td class="bluedata"height="20px" title="Juniper Station">
        <table width="100%"><tr><td>12:17am</td></tr><tr><td>12:17am</td></tr><tr><td>12:47am</td></tr></table>
        </td>
    </tr>
</table>

我之前已在此处提出问题,并且我已成功将文本框值转换为下拉列表更改。然而,这有点不同。我正在使用sessions插件(工作正常)。在一个页面上,我有一组正常的下拉菜单,在提交时你会被带到一个运行上述功能的单独页面,但行/列都显示,它们似乎根本不会过滤。

编辑:看完代码之后,我认为这是因为如果项目存在(!= null),它只显示所有TH或TR而不是所选的那个。

1 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,我想出来了。

     <script type="text/javascript">  
 $(function() {
 var from = $.session("from");
var to = $.session("to");
var $th = $('#theTable').find('th');
var $td = $('#theTable').find('td.bluedata,td.yellowdata');

if (to == "Select" || from == "Select") {
  // shortcut - nothing set, show everything
  $th.add($td).show();
  return;
}

 $th.add($td).hide();
$th.each(function(idx) {
      var notSelected = $(this).text();
      if ((notSelected.indexOf(to) != -1) || (notSelected.indexOf(from) != -1)) {
         // the text had the to or the from in it - so show this tr
         $(this).show();
         // and show its corresponding td
         $td.eq(idx).show();
      }
    });                      
 });
</script>