我如何让我的选择和两个复选框来过滤我的表,只显示所有匹配的数据javascript?

2ledvvac  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(81)

我有一个包含许多州的表,其中有两列显示活动是否适合家庭和/或讲西班牙语。我有一个列出州的下拉选择器和两个复选框,它们应该过滤适合家庭和/或讲西班牙语的匹配行。
我当前的下拉菜单过滤出州,只显示选中的州。我的两个复选框分别过滤行,以显示家庭友好或西班牙语。当两个复选框都选中时,它显示家庭友好或西班牙语。我希望它显示家庭友好和西班牙语,使所有其他不匹配的行消失。我的复选框目前正在考虑状态下拉菜单太多,我希望保持不变。
例如,如果您在我的表中选择Texas,并选中两个下拉菜单,我希望仅将Texas列为州、适合家庭且讲西班牙语的事件(应仅返回一个结果)。
我的代码:

<script>
        $("input[name='filterStatus'], #filter").change(function() {
        var classes = [];
        var filterValue = $("#filter").val();
        var row = $('.row-events');

        $("input[name='filterStatus']").each(function() {
          if ($(this).is(":checked")) {
            classes.push('.' + $(this).val());
          }
        });

        row.hide();
        row.each(function(i, el) {
          if ((classes == "" || classes.some(function(className) {
              return $(el).find('td' + className).html() != `&nbsp;`;
            })) && (filterValue == "all" || $(el).attr('data-type') == filterValue)) 
{
            $(el).show();
          }
        });
});
      </script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label id='label-state-text'>Select Your State:</label>
      <select class="state" id="filter">
        <option value="all" selected="selected">Select a State</option>
        <option value="AZ">Arizona</option>
        <option value="CA">California</option>
        <option value="NC">North Carolina</option>
        <option value="SC">South Carolina</option>
        <option value="TX">Texas</option>
      </select>
  </form>

 <form name="FilterForm" id="FilterForm" action="" method="">
    <input type="checkbox" name="filterStatus" value="family" />
    <label  for="filter_1">Family Friendly?</label>
    <input type="checkbox" name="filterStatus" value="ESP" />
    <label for="filter_2">Spanish Speaking?</label>
  </form>

 <table id="myTable-events">
<thead>
<tr>
<th class='event_heading'> Name </th>
<th class='event_heading'> Date </th>
<th class='event_heading'> State </th>
<th class='event_heading'> Family Friendly </th>
<th class='event_heading'> Spanish Speaking </th>    
</tr>
</thead>
<tr class="row-events" data-type="TX">
<td> Event 1 </td>
<td> 10/22/2023 </td>
<td> Texas </td>
<td class="family"> Yes </td>
<td class="ESP">&nbsp;</td>
</tr>
<tr class="row-events" data-type="CA">
<td> Event 11 </td>
<td> 10/22/2023 </td>
<td> California </td>
<td class="family"> Yes </td>
<td class="ESP">&nbsp;</td>
</tr>
<tr class="row-events" data-type="TX">
<td> Event 2 </td>
<td> 09/20/2023 </td>
<td> Texas </td>
<td class="family">&nbsp;</td>
<td class="ESP"> Yes </td>
</tr>
<tr class="row-events" data-type="NC">
<td> Event 3 </td>
<td> 06/18/2023 </td>
<td> North Carolina </td>
<td class="family">&nbsp;</td>
<td class="ESP">&nbsp;</td>
</tr>
<tr class="row-events" data-type="SC">
<td> Event 4 </td>
<td> 05/02/2023 </td>
<td> South Carolina </td>
<td class="family">&nbsp;</td>
<td class="ESP">Yes</td>
</tr>
<tr class="row-events" data-type="TX">
<td> Event 5 </td>
<td> 12/29/2023 </td>
<td> Texas </td>
<td class="family"> Yes </td>
<td class="ESP"> Yes </td>
</tr>
<tr class="row-events" data-type="CA">
<td> Event 6 </td>
<td> 11/20/2023 </td>
<td> California </td>
<td class="family">&nbsp;</td>
<td class="ESP"> Yes </td>
</tr>
<tr class="row-events" data-type="AZ">
<td> Event 7 </td>
<td> 08/09/2023 </td>
<td> Arizona </td>
<td class="family">&nbsp;</td>
<td class="ESP">&nbsp;</td>
</tr>
<tr class="row-events" data-type="TX">
<td> Event 8 </td>
<td> 10/19/2023 </td>
<td> Texas </td>
<td class="family">&nbsp;</td>
<td class="ESP"> Yes </td>
</tr>
<tr class="row-events" data-type="CA">
<td> Event 9 </td>
<td> 08/04/2023 </td>
<td> California </td>
<td class="family"> Yes </td>
<td class="ESP"> Yes </td>
</tr>
<tr class="row-events" data-type="AK">
<td> Event 10 </td>
<td> 03/26/2023 </td>
<td> Alaska </td>
<td class="family"> Yes </td>
<td class="ESP"> Yes </td>
</tr>

</table>
ghhaqwfi

ghhaqwfi1#

如果你换成这个会有帮助

<script>
$("input[name='filterStatus'], #filter").change(function() {
  var classes = [];
  var filterValue = $("#filter").val();
  var row = $('.row-events');

  $("input[name='filterStatus']").each(function() {
    if ($(this).is(":checked")) {
      classes.push('.' + $(this).val());
    }
  });

  row.hide();
  row.each(function(i, el) {
    var state = $(el).attr('data-type');
    var isMatch = (filterValue == "all" || state == filterValue) && (classes.length == 0 || classes.every(function(className) {
      return $(el).find('td' + className).html() != `&nbsp;`;
    }));
    if (isMatch) {
      $(el).show();
    }
  });
});
</script>

相关问题