2010-05-20 24 views
6

Necesita filtrar una tabla desde fuera de la tabla, con una búsqueda de texto, casillas de verificación y selecciona. PicNet Table Filter para jQuery funciona para buscar y usar casillas de verificación fuera de la tabla ... aunque no puedo encontrar ningún ejemplo de cómo hacer que funcione un cuadro de selección. ¿Nadie sabe?jQuery filtro de tabla con texto, casillas de verificación, selecciona

** Podría estar recibiendo demasiado específico aquí, pero pensé que al menos había pregunte. *

También estoy abierto a las posibilidades que no sean PicNet.

ACTUALIZADO: Aquí está mi código hasta ahora, me gustaría una opción seleccionar en la parte superior del cuerpo por las dos casillas de verificación sí/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 para el enlace con el buen complemento. –

Respuesta

-2

que no tienen idea de lo que estás tratando de hacer ("necesidad de filtrar una tabla desde fuera de la mesa" -? Wtf quiere decir eso). Pero si usted está tratando de obtener el valor de un cuadro de selección utilizando jQuery:

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

Si observa el plugin de filtro de tabla PicNet, tiene dos opciones, filtrando desde dentro de la tabla y desde fuera de ella. Estoy tratando de filtrar una columna usando una opción de selección, fuera de la tabla. – Jeffrey

2

Just made a small example for you to try out. Solo una prueba rápida de concepto.

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

Y el jQuery:

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

Si desea ocultar/mostrar toda la fila, hacer $(this).parent().hide() y $(this).parent().show()

Una cosa a tener en cuenta es que si usted quiere hacer un menú desplegable que verifica todos los TD en cada fila, tendrá que modificar el código para que solo oculte la fila si NINGUNO de los tds coinciden con el menú desplegable. Algo así como 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> 

Y el 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

¿cómo puedo ocultar una fila completa? http://jsfiddle.net/B7hMk/ – Jeffrey

+0

¿Cómo modificarías tu código si tienes una casilla de selección para cada columna? ¡Atascado allí! – Ismailp

+0

Al principio, gracias Bradley, @Jeffrey He hecho una muestra para la fila aquí: http://jsfiddle.net/BCreb/87/ - http://stackoverflow.com/questions/9878157/cant-get-tablesorter-checkbox -filtering-to-work/9882155 # 9882155 Espero que ayude, ¡salud! –

Cuestiones relacionadas