我想根据用户从多个select元素中选择的内容,使用jQuery hide/show过滤表格。使用jQuery从多个select元素中过滤表格
我希望用户能够从1,2或3个选择元素中选择多个值。所以也许他们会选择2名培训师,1名招聘人员和1名身份,或者只选一名培训师。规划创建一个在用户点击任何选项时运行的函数。
我看到它的方式,每个select元素都会有一个用户选择的值的数组。所以我需要遍历每个数组并将其与特定列中的文本进行比较。如果选项仅来自1个选择元素,将会很容易。但是因为它可能是1,2或3,所以我很难找到它。
任何帮助将非常感激。
表:
<table id="reportsTable">
<thead>
<th>Report Number</th>
<th>Date</th>
<th>Name</th>
<th>Trainer</th>
<th>Status</th>
</thead>
<tbody>
<tr>
<td>12345-1</td>
<td>05/01/2011</td>
<td>First Recruit</td>
<td>First Trainer</td>
<td>Complete</td>
</tr>
<tr>
<td>12345-2</td>
<td>05/02/2011</td>
<td>First Recruit</td>
<td>Second Trainer</td>
<td>In Progress</td>
</tr>
<tr>
<td>54321-1</td>
<td>05/03/2011</td>
<td>Second Recruit</td>
<td>First Trainer</td>
<td>Created</td>
</tr>
</tbody>
</table>
选择:
<select multiple="multiple" name="trainerFilter">
<option value="firsttrainer">First Trainer</option>
<option value="secondtrainer">Second Trainer</option>
</select>
<select multiple="multiple" name="recruitFilter">
<option value="firstrecruit">First Recruit</option>
<option value="secondrecruit">Second Recruit</option>
</select>
<select multiple="multiple" name="statusFilter">
<option value="created">Created</option>
<option value="inprogress">In Progress</option>
<option value="complete">Complete</option>
</select>
看起来我不能回答张贴到我的问题8小时,但是这是我想出了感谢@Spencer Ruport 。由于必须考虑所有可能的条目,最终结果比我预期的要复杂得多。用户可以从第一个选择元素中选择一些,第二个中没有任何东西,第二个中可能有两个。或者,也许用户不会从第一个和第2个中选择任何内容。对于任何给定的输入,可能有6个以上的滤波器比需要应用。
我确定有比这更好的方法,它看起来像@Alison可能已经链接到一个,但它的工作原理。
function filterReports() {
$('.report').hide(); //Set all rows to hidden.
trainerVals = $('#trainerFilter').val();
recruitVals = $('#recruitFilter').val();
statusVals = $('#statusFilter').val();
if (trainerVals) { //Check if any trainers are selected.
$.each(trainerVals, function(index, trainer) {
filtered = false;
classString = '';
classString += '.' + trainer;
if (recruitVals) { //Check if trainers and recruits are selected.
$.each(recruitVals, function(index, recruit) {
filtered = false;
secondString = '';
secondString = classString + '.' + recruit; //Concat to a new string so we keep the old one intact.
if (statusVals) { //Check if trainers, recruits and statuses are selected.
$.each(statusVals, function(index, status) {
filtered = false;
finalString = '';
finalString += secondString + '.' + status; //Again concat to a new string.
$(finalString).show();
filtered = true; //By setting filtered to true, we only run the show once.
});
}
if (!filtered) { //If trainers and recruits are selected, but not a status, we need to apply that filter.
$(secondString).show();
filtered = true;
}
});
}
if (!filtered && statusVals) { //If only trainers and statuses are selected, go through those.
$.each(statusVals, function(index, status) {
filtered = false;
finalString = '';
finalString += classString + '.' + status;
$(finalString).show();
filtered = true;
});
}
if (!filtered) { //If only trainers are selected, apply that filter.
$(classString).show();
filtered = true;
}
});
}
if (!filtered && recruitVals) { //If trainers are not selected, by recruits are, run through the recruits.
$.each(recruitVals, function(index, recruit) {
classString = '';
classString += '.' + recruit;
if (statusVals) { //Check if recruits and statuses are selected
$.each(statusVals, function(index, status) {
finalString = '';
finalString += classString + '.' + status;
$(finalString).show();
filtered = true;
});
}
if (!filtered) { //If only recruits are selected, apply that filter.
$(classString).show();
filtered = true;
}
});
}
if (!filtered && statusVals) { //If both trainers and recruits are not selected, but statuses are, run through those.
$.each(statusVals, function(index, status) {
classString = '';
classString += '.' + status;
$(classString).show();
filtered = true;
});
}
if (!filtered) {
//No filters selected.
}
$("tr").removeClass("even"); //Remove current zebra striping.
$("tr:visible:even").addClass("even"); //Add zebra striping only for rows that are visible.
}
我甚至没有想到这一点的例子。有时最简单的答案是最好的。我必须考虑每个select元素中的多个选项。有没有简单的方法来做到这一点,而不使用3 $ .each语句? – 2011-05-10 23:28:05
啊,我没有注意到你正在使用选择。由于所有项目都是隐藏起来的,我只需创建一个三级循环来循环选择并连接每个组合并为每个组合调用show,如果这样做合理的话。只需为包含所选项目的当前迭代的每个级别创建一个变量,然后更改我上面所写的连接线,以使用变量而不是val。 – 2011-05-11 00:07:30
感谢您的帮助!由于可能的用户输入,循环最终变得更加复杂。他们可以从第一个输入中选择一对,而第二个也可以选择一对,也可以从第三个输入中选择一些。我编辑了我提出的问题。我怀疑它甚至接近最有效的方式,但它是有效的。谢谢! – 2011-05-11 01:42:25