如何使用JavaScript中的select将两个过滤器融合到一个表中?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用JavaScript中的select将两个过滤器融合到一个表中?相关的知识,希望对你有一定的参考价值。
我坚持使用我的页面,因为我在html中使用了两个不同的“选择”选项。我有我的脚本并且单独工作正常,但是当我尝试使用两个过滤器时,一个使另一个空白等等...请帮助我知道如何将这两个过滤器融合在一起工作。
$.getJSON("data.json", function(data){
var items = [];
$.each(data, function(key, val){
items.push("<tr>");
items.push("<td Sucursal= ''"+key+"''>"+val.nombreSucursal+"</td>");
items.push("<td Ciudad= ''"+key+"''>"+val.direccion.municipio+"</td>");
items.push("<td Lun-Sab= ''"+key+"''>"+val.horarios.Lun_Sab+"</td>");
items.push("<td Domingo= ''"+key+"''>"+val.horarios.Domingo+"</td>");
items.push("<td Horario= ''"+key+"''>"+val.horarios.horario+"</td>");
items.push("<td Contacto= ''"+key+"''>"+val.contacto.telefono+"</td>");
items.push("</tr>");
});
$("<tbody/>",{html: items.join("")}).appendTo("#tablita");
});
$('#filtro').change( function(e) {
var letter = $(this).val();
if (letter == 'ALL') {
$('tr').show();
}else {
$('tr').each( function(rowIdx,tr) {
$(this).hide().find('td').each( function(idx, td) {
if(idx===0||idx===1||idx===2) {
var check = $(this).text();
if (check && check.indexOf(letter) == 0) {
$(tr).show();
}
}
});
});
}
});
$('#filtro2').change( function(e) {
var letter = $(this).val();
if (letter == 'ALL') {
$ ('tr').show ();
}
else {
$('tr').each( function(rowIdx,tr) {
$(this).hide().find('td').each( function(idx, td) {
if(idx === 3) {
var check = $(this).text();
if (check && check.indexOf(letter) == 0) {
$(tr).show();
}
}
});
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="es" dir="ltr">
<head>
<!-- IDEA: estilos de pagina -->
<meta charset="utf-8">
<meta http-equiv="X-UA Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<LINK rel="StyleSheet" HREF="css/normalize.css" >
<LINK rel="StyleSheet" HREF="css/estilo.css" >
<LINK rel="StyleSheet" HREF="css/bootstrap.css" >
<LINK rel="StyleSheet" HREF="css/contact-buttons.css" >
<LINK rel="StyleSheet" HREF="css/demo.css" >
<LINK rel="StyleSheet" HREF="css/estiloReceta.css" >
<LINK rel="StyleSheet" HREF="css/estiloRegistro.css" >
<LINK rel="StyleSheet" HREF="css/inicio.css" >
<LINK rel="StyleSheet" HREF="css/jquery-ui.css" >
<title>Horarios</title>
<!-- IDEA: CONTROLADORES DE LA PAGINA PRINCIPAL -->
<link href="css/bootstrap.css" rel="stylesheet">
<script src="js/jquery-1.11.2.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="https://code.angularjs.org/1.5.5/angular-route.min.js"></script>
<script src="js/jquery-ui.js"></script>
<link href="css/estiloReceta.css" rel="stylesheet" type="text/css">
<script src="js/jspdf.min.js"></script>
<script src="js/pdfFromHTML.js"></script>
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!--<script>
var miAplicacion = angular.module('SCH', []);
miAplicacion.filter('unique', function () {
return function (collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function (item) {
var key = item[keyname];
if (keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
});
miAplicacion.controller('controladorBD', ['$scope', function ($scope) {
$scope.sucursales = [
]
}]);
</script>-->
<script>
function cargaSeccion(div, fichero) {
$('.' + div).fadeOut(50, function () {
$('.' + div).html(fichero);
});
$('.' + div).fadeIn(50);
}
</script>
</head>
<header>
<div class="header">
<!--<img src="img/SHC.png" alt="Sucahersa" height="20%">-->
<h1>SUCURSALES</h1>
</div>
</header>
<body>
<!-- IDEA: ".....................................................................................CONTROLADORES DE PAGINA ORIGINAL.................................." -->
<script src="js/demo.js"></script>
<div id="pdf2htmldiv" class="container-fluid cabecera">
<center>
</center>
</div>
<div class="container-fluid barmenu">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<a class="navbar-brand" href="../index.php">INICIO</a></div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav navbar-right">
<li><a href="../nosotros.html">QUIENES SOMOS</a></li>
<li><a href="catalogo.php">SUCURSALES</a></li>
<li><a href="../productos.php">PRODUCTOS MAYOREO</a></li>
<li><a href="../productos2.php">PRODUCTOS MENUDEO</a></li>
<li><a href="../recetas/catalogo.php">RECETARIO</a></li>
<li><a href="../contacto.html">CONTACTO</a></li>
<li><a href="../solicitud.html">BOLSA DE TRABAJO</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
</div>
<!-- IDEA: ".....................................................FIN DE LOS CONTROLADORES............................................................................." -->
<div class="contenedor">
<div class="filtrado">
<h3 style= "padding-left: 40%; text-transform: uppercase; font-weight: bold; line-height: 40px; background-color: orange;">Fines de Semana</h3>
<label for="buscador">Buscar: </label>
<select class="" name="filtrado" id="filtro">
<option value="">Ciudad</option>
<option value="Salamanca">Salamanca</option>
<option value="León">León</option>
<option value="Celaya">Celaya</option>
<option value="Moroleon">Moroleon</option>
<option value="Cortazar">Cortazar</option>
<option value="Juventino">Juventino Rosas</option>
<option value="Guanajuato">Guanajuato</option>
<option value="Irapuato">Irapuato</option>
<option value="Julian">San Julian</option>
<option value="Sialo">Sialo</option>
<option value="Uriangato">Uriangato</option>
<option value="Valle">Valle de Santiago</option>
</select>
<select class="" name="filtrado" id="filtro2">
<option value="">Domingos</option>
<option value="Si">Si</option>
<option value="No">No</option>
</select>
</div>
<div id="divtabla">
<div id="divfondo">
<table class="table table-bordered table-striped table-hover" id="tablita" >
<thead> <!-- IDEA:cabecera de la tabla ----------------------------------------------------------->
<tr>
<th>Sucursal</th>
<th>Ciudad</th>
<th>Lun-Sab</th>
<th>Domingo</th>
<th>Horario</th>
<th>Contacto</th>
</tr>
</thead>
<tbody>
<!-- cuerpo de la tabla --------------------------------------------------------------->
</tbody>
</table>
</div>
</div>
<div>
<center>
<p class="derechos">® Super Carnicerías Hernández S.A. de C.V. Todos los Derechos Reservados 2018 .</p>
</center>
</div>
</div>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/jquery-ui_1.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script>
</script>
</body>
</html>
答案
如果您想要一种过滤多个过滤器函数的方法,则需要存储过滤器数组(或过滤器对象)。
然后循环遍历每一行并调用filters.every
以确保所有条件匹配所有过滤器。
(function($) {
/* where column is indexed from 1 */
$.fn.fromTableColumn = function($table, column) {
return this.append($table.find('tbody td:nth-child(' + column + ')')
.map((i, td) => $(td).text()).toArray()
.filter((v, i, a) => a.indexOf(v) === i)
.sort((a, b) => a.localeCompare(b))
.map((val, i) => new Option(val, val)))
.prepend(new Option('ALL', 'ALL'))
.find('option:eq(0)').prop('selected', true);
}
})(jQuery);
var filters = [
// City filter
function($row, index, query) {
if (query[0].value === 'ALL') return true;
return $row.find('td:nth-child(3)').text().trim() === query[0].value;
},
// Country filter
function($row, index, query) {
if (query[1].value === 'ALL') return true;
return $row.find('td:nth-child(4)').text().trim() === query[1].value;
}
];
// Populate drop-downs from column values.
$('select[name="city"]').fromTableColumn($('#people'), 3);
$('select[name="country"]').fromTableColumn($('#people'), 4);
// Filter on any of the drop-downs.
$('select[name="city"], select[name="country"]').on('change', function(e) {
var query = $('form[name="filters"]').serializeArray();
$('#people tbody tr').each((i, tr) => {
$(tr).toggle(filters.every(filter => filter($(tr), i, query)));
});
});
.filter-lbl {
font-weight: bold;
margin-right: 0.5em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="filters">
<span class="filter-lbl">
<label>City:</label>
<select name="city"></select>
</span>
<span class="filter-lbl">
<label>Country:</label>
<select name="country"></select>
</span>
</form>
<!-- Generated 100 rows of random data from: https://www.generatedata.com/ -->
<table id="people" cellpadding="1" cellspacing="1">
<thead>
<tr>
<th>Name</th>
<th>Street</th>
<th>City</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>Richard Carson</td>
<td>6621 A Street</td>
<td>Middlesbrough</td>
<td>Gambia</td>
</tr>
<tr>
<td>Neil Carter</td>
<td>467 Turpis. Rd.</td>
<td>Eernegem</td>
<td>Kyrgyzstan</td>
</tr>
<tr>
<td>Chester Leach</td>
<td>3259 Sapien. Avenue</td>
<td>Burcht</td>
<td>Guam</td>
</tr>
<tr>
<td>Gareth Orr</td>
<td>P.O. Box 818, 9867 Dolor St.</td>
<td>Motala</td>
<td>French Southern Territories</td>
</tr>
<tr>
<td>Bruno Avila</td>
<td>Ap #961-6695 Proin Road</td>
<td>Brunn am Gebirge</td>
<td>Tuvalu</td>
</tr>
<tr>
<td>Dolan Wyatt</td>
<td>P.O. Box 753, 5072 Quis Road</td>
<td>Orai</td>
<td>Pitcairn Islands</td>
</tr>
<tr>
<td>Malik Larson</td>
<td>P.O. Box 500, 7399 Eros Street</td>
<td>Jolanda di Savoia</td>
<td>Jamaica</td>
</tr>
<tr>
<td>Moses Davenport</td>
<td>699-620 Erat, Street</td>
<td>Mont-Saint-AndrŽ</td>
<td>Madagascar</td>
</tr>
<tr>
<td>Bevis Mcgee</td>
<td>P.O. Box 822, 9692 Non, Avenue</td>
<td>Frankfurt</td>
<td>Ghana</td>
</tr>
<tr>
<td>Octavius Mcintyre</td>
<td>Ap #539-4942 Accumsan Avenue</td>
<td>Toowoomba</td>
<td>Zimbabwe</td>
</tr>
<tr>
<td>Tate Morales</td>
<td>295-327 Nec, Ave</td>
<td>Gualdo Cattaneo</td>
<td>Iceland</td>
</tr>
<tr>
<td>Orson Boyer</td>
<td>611-2727 Mi St.</td>
<td>Midway</td>
<td>Bahrain</td>
</tr>
<tr>
<td>Ira Nguyen</td>
<td>6999 Sed Rd.</td>
<td>Kurnool</td>
<td>Gabon</td>
</tr>
<tr>
<td>Nigel Fernandez</td>
<td>Ap #609-8173 Dictum St.</td>
<td>Seborga</td>
<td>Central African Republic</td>
</tr>
<tr>
<td>Camden Burke</td>
<td>158-6818 Tellus Av.</td>
<td>Raipur</td>
<td>Aruba</td>
</tr>
<tr>
<td>Brendan Quinn</td>
<td>406-4678 Nullam Rd.</td>
<td>Langen</td>
<td>Togo</td>
</tr>
<tr>
<td>Plato Bradshaw</td>
<td>P.O. Box 210, 1406 Amet, Rd.</td>
<td>Villers-la-Bonne-Eau</td>
<td>Niue</td>
</tr>
<tr>
<td>Bernard Langley</td>
<td>P.O. Box 709, 8927 Convallis Rd.</td>
<td>Burnaby</td>
<td>Egypt</td>
</tr>
<tr>
<td>Lamar Hull</td>
<td>433-8524 Mauris Rd.</td>
<td>Beaumont</td>
<td>Bermuda</td>
</tr>
<tr>
<td>Mohammad Mosley</td>
<td>147 Non, Ave</td>
<td>Burlington</td>
<td>Barbados</td>
</tr>
<tr>
<td>Macon Knapp</td>
<td>P.O. Box 899, 2310 Dui St.</td>
<td>Molina</td>
<td>Norfolk Island</td>
</tr>
<tr>
<td>Reece Mcgowan</td>
<td>P.O. Box 247, 8448 Sed Street</td>
<td>Silchar</td>
<td>Seychelles</td>
</tr>
<tr>
<td>Buckminster Wilder</td>
<以上是关于如何使用JavaScript中的select将两个过滤器融合到一个表中?的主要内容,如果未能解决你的问题,请参考以下文章
如何将两个属性作为 text_method 传递给 rails 中的 collection_select
如何将两个参数传递给 Javascript/NodeJS 中的 API“获取”请求
如何将 SELECT 查询中的数据作为 <option> 提取到 <select>