2010-05-20 104 views
6

需要使用文本搜索,複選框和選擇從表格外部過濾表格。對於jQuery,PicNet Table Filter適用於在表格之外搜索和使用複選框......但我無法找到任何有關如何讓選擇框工作的示例。有人知道嗎?帶文本,複選框和選擇的jQuery表格過濾器

**我可能是太具體的在這裏,但認爲我會至少來電諮詢。*

我也開到比PicNet其他的可能性。

已更新:這是我的代碼到目前爲止,我想通過兩個yes/no複選框在主體頂部選擇一個選項。

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
<title>PicNet Table Filter Demo</title> 
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>     
<script type="text/javascript" src="picnet.table.filter.min.js"></script> 


<script type="text/javascript"> 
    $(document).ready(function() { 
     // Randomly Create Data Rows 


     // Initialise Plugin 
     var options1 = { 
      additionalFilterTriggers: [$('#onlyyes'), $('#onlyno'), $('#itemone'), $('#quickfind')], 
      clearFiltersControls: [$('#cleanfilters')], 
      matchingRow: function(state, tr, textTokens) { 
       if (!state || !state.id) { return true; }     
       var val = tr.children('td:eq(2)').text(); 
       var val2 = tr.children('td:eq(3)').text(); 
       switch (state.id) { 
        case 'onlyyes': return state.value !== 'true' || val === 'yes'; 
        case 'onlyno': return state.value !== 'true' || val === 'no'; 
        case 'itemone': return state.value !== 'true' || val2 === 'Item 1'; 
        default: return true; 
       } 
      } 
     }; 

     $('#demotable1').tableFilter(options1); 


    }); 
</script> 
<style> 
* { font-family:arial; font-size:8pt;} 
table, td, th { border: solid 1px silver; color:#666; padding:3px 5px 3px 5px} 
th { background-color:#333; color:#fff; font-size:0.85em } 
a { color:gray; } 
.filtering { background-color:light-gray} 
#jqtf_filters { 
list-style:none; 

} 
#jqtf_filters li { 
display:inline-block; 
position:relative; 
float:left; 
margin-bottom:20px 
    } 
    .hidden, tr.filters { display: none;} 

    </style> 
    </head> 
    <body>  
<b>Additional Filters for Table 1</b><br/> 
    Only Show Yes: <input type="checkbox" id="onlyyes"/>   
Only Show No: <input type="checkbox" id="onlyno"/> 
Only Show Item 1: <input type="checkbox" id="itemone"/> 
<br/>  
Quick Find: <input type="text" id="quickfind"/> 
<br/> 
<a id="cleanfilters" href="#">Clear Filters</a> 
<br/><b>Table 1</b><br/> 
    <table id='demotable1'> 
    <thead> 
     <tr><th>Text Column 1</th><th>Number Column</th><th>Yes/No Column</th><th filter-type='ddl'>List Column</th><th style='width:100px;' filter='false'>No Filter</th><th>Date Column</th><th filter='false'>Empty</th><th class="hidden" filter='false'></th></tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td>Value 102</td> 
      <td>420</td> 
      <td>yes</td> 
      <td>Item 1</td> 
      <td>hello</td> 
      <td>01/11//2009</td>     
      <td></td> 
      <td class="hidden">Ed Head</td> 
     </tr> 
     <tr> 
      <td>Value 134</td> 
      <td>987</td> 
      <td>no</td> 
      <td>Item 2</td> 
      <td>hi</td> 
      <td>03/11//2009</td>     
      <td></td> 
      <td class="hidden">Joe Blow</td> 
     </tr> 
     <tr> 
      <td>Value 654</td> 
      <td>722</td> 
      <td>yes</td> 
      <td>Item 3</td> 
      <td>hello</td> 
      <td>04/11//2009</td>     
      <td></td> 
      <td class="hidden">Jimmy</td> 
     </tr> 
    </tbody> 
    </table> 

    </body> 
    </html> 
+1

+1的鏈接不錯的插件。 – 2010-05-20 16:37:15

回答

-2

我不知道你在做什麼(「需要從表格外部過濾表格」 - wtf是否意味着?)。但是,如果您試圖使用jQuery獲取選擇框的值:

$('#yourSelectList').val() // Option value 
$('#yourSelectList :selected').text() // Option text value 
+0

如果你看看PicNet表格過濾插件,他們有兩個選擇,從表格內部和外部過濾。我試圖在表格外使用選項select來過濾列。 – Jeffrey 2010-05-20 17:04:10

2

Just made a small example for you to try out。只是一個概念的快速證明。

<select id="filter"> 
    <option value="dogs">dogs</option> 
    <option value="cats">cats</option> 
</select> 

<table id="boing" border="1"> 
<tr> 
<th>header</th> 
</tr> 
<tr> 
<td>dogs</td> 
</tr> 
<tr> 
<td>dogs</td> 
</tr> 
    <tr> 
<td>cats</td> 
</tr> 
    <tr> 
<td>cats</td> 
</tr> 
    <tr> 
<td>dogs</td> 
</tr> 
</table> 

而jQuery的:

$("#filter").change(function(){ 
    $("#boing").find("td").each(function(){ 
     if($(this).text() != $("#filter").val()) $(this).hide(); 
     else $(this).show(); 
    }); 
});​ 

如果你想隱藏/顯示整個行,做$(this).parent().hide()$(this).parent().show()

有一點要記住的是,如果你想要做的一個檢查每行中所有TD的下拉列表,你將不得不調整代碼,以便只有在沒有任何tds匹配下拉菜單時纔會隱藏該行。類似this

<select id="filter"> 
    <option value="dogs">dogs</option> 
    <option value="cats">cats</option> 
</select> 

<table id="boing" border="1"> 
<tr> 
<th>header</th> 
</tr> 
<tr> 
<td>dogs</td> 
<td>dogs</td> 
</tr> 
<tr> 
<td>dogs</td> 
<td>cats</td> 
</tr> 
    <tr> 
<td>cats</td> 
<td>dogs</td> 
</tr> 
    <tr> 
<td>cats</td> 
<td>cats</td> 
</tr> 
    <tr> 
<td>dogs</td> 
<td>cats</td> 
</tr> 
</table> 

而jQuery的:

$("#filter").change(function(){ 
    $("#boing").children('tbody').children('tr').not(':first').each(function(){ 
     var match = false; 
     $(this).children('td').each(function() { 
      if($(this).text() == $("#filter").val()) match = true; 
     }); 
     if(match) $(this).show(); 
     else $(this).hide(); 
    }); 
});​ 
+0

如何隱藏整行? http://jsfiddle.net/B7hMk/ – Jeffrey 2010-05-20 17:16:26

+0

如果你爲每一列添加一個選擇框,你將如何調整你的代碼?卡住了! – Ismailp 2012-02-29 20:51:42

+0

首先感謝bradley,@Jeffrey我在這裏做了一個樣本行:http://jsfiddle.net/BCreb/87/ - http://stackoverflow.com/questions/9878157/cant-get-tablesorter-checkbox - 過濾到工作/ 9882155#9882155希望它有幫助,歡呼聲 – 2012-03-27 02:00:26