Check multiple Checkboxes to filter table rows matching each input label text

Viewed 98

In the snippet below, having one checkbox checked at a time works fine but checking multiple checkboxes doesn't work as desired. I'm guessing that when I check multiple checkboxes, for example White and Orange, the function is looking for WhiteOrange in the column cells, obviously leading to no results. How do I check for each label text one by one to get all matching results?

Also, how can I check for the initial state of the checkboxes before the change function so that only the results for White are shown when loading the page? I've tried running the filter function within the for loop before the change function, but it didn't work at all.

Thank you for your help; it is greatly appreciated!

$(function() {
  const focusInputs = document.querySelectorAll("input.focusbox");
  const focusColumns4 = document.querySelectorAll("table#colors tbody tr td.table-cell:nth-child(4)");
  for (const focusInput of focusInputs) {
    $(focusInput).on('change', function() {
      if (!$(focusInput).is(":checked")) {
        $(focusColumns4).parent('tr').show();
      } else if ($(focusInput).is(":checked")) {
        $(focusColumns4).filter(function() {
          return $(this).text() !== $(focusInput).parent().text();
        }).parent('tr').hide();
      }
    });
  }
});
body {
  font-size: 10px;
}

ul,
li {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

table#colors {
  margin-top: 5px;
}

table#colors tbody tr td.entry2 {
  width: 200px;
}

.cf-circle {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #C8C8C8;
  -webkit-transition: border-radius 1s, width 1s, height 1s;
  transition: border-radius 1s, width 1s, height 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<ul>
  <li><label class="focusbox-label"><input class="focusbox" type="checkbox" checked />White</label>
  </li>
  <li>
    <label class="focusbox-label"><input class="focusbox" type="checkbox" />Red</label>
  </li>
  <li>
    <label class="focusbox-label"><input class="focusbox" type="checkbox" />Orange</label>
  </li>
</ul>

<table id="colors">
  <tbody>
    <tr class="head-row">
      <th class="table-cell cYellow c1 entry1">Color</th>
      <th class="table-cell cYellow c1 entry2">Name</th>
      <th class="table-cell cYellow c1 entry3">HTML Color Code</th>
      <th class="table-cell cYellow c1 entry4">Shade Group</th>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FFFFFF;"></div>
      </td>
      <td class="table-cell entry2">White</td>
      <td class="table-cell entry3">#FFFFFF</td>
      <td class="table-cell entry4">White</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #F7E7CE;"></div>
      </td>
      <td class="table-cell entry2">Champagne</td>
      <td class="table-cell entry3">#F7E7CE</td>
      <td class="table-cell entry4">White</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #ED2839;"></div>
      </td>
      <td class="table-cell entry2">Red (Pantone)</td>
      <td class="table-cell entry3">#ED2839</td>
      <td class="table-cell entry4">Red</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #ED1B24;"></div>
      </td>
      <td class="table-cell entry2">Red (CMYK) (pigment red)</td>
      <td class="table-cell entry3">#ED1B24</td>
      <td class="table-cell entry4">Red</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FF8200;"></div>
      </td>
      <td class="table-cell entry2">UT orange</td>
      <td class="table-cell entry3">#FF8200</td>
      <td class="table-cell entry4">Orange</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FF8000;"></div>
      </td>
      <td class="table-cell entry2">Orange (wheel)</td>
      <td class="table-cell entry3">#FF8000</td>
      <td class="table-cell entry4">Orange</td>
    </tr>
  </tbody>
</table>

2 Answers

Note sure why the mix between jQuery and javascript, but try this way:

$(function() {
  const focusInputs = $("input.focusbox");

  focusInputs.change(function() {
    var checked = focusInputs.filter(function() {
      return $(this).is(":checked")
    })
    var colors = checked.map(function() {
      return $(this).parent().text()
    }).get()

    if (colors.length == 0) {
      $('#colors tr').show();
    } else {
      $('#colors tr').hide();
      $('#colors tr').filter(function() {
        return $.inArray($(this).find(".entry4").text(), colors) > -1
      }).show();
    }

  });
});

Demo

$(function() {
  const focusInputs = $("input.focusbox");

  focusInputs.change(function() {
    var checked = focusInputs.filter(function() {
      return $(this).is(":checked")
    })
    var colors = checked.map(function() {
      return $(this).parent().text()
    }).get()

    if (colors.length == 0) {
      $('#colors tr').show();
    } else {
      $('#colors tr').hide();
      $('#colors tr').filter(function() {
        return $.inArray($(this).find(".entry4").text(), colors) > -1
      }).show();
    }

  });
});
body {
  font-size: 10px;
}

ul,
li {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

table#colors {
  margin-top: 5px;
}

table#colors tbody tr td.entry2 {
  width: 200px;
}

.cf-circle {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #C8C8C8;
  -webkit-transition: border-radius 1s, width 1s, height 1s;
  transition: border-radius 1s, width 1s, height 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<ul>
  <li><label class="focusbox-label"><input class="focusbox" type="checkbox" checked />White</label>
  </li>
  <li>
    <label class="focusbox-label"><input class="focusbox" type="checkbox" />Red</label>
  </li>
  <li>
    <label class="focusbox-label"><input class="focusbox" type="checkbox" />Orange</label>
  </li>
</ul>

<table id="colors">
  <tbody>
    <tr class="head-row">
      <th class="table-cell cYellow c1 entry1">Color</th>
      <th class="table-cell cYellow c1 entry2">Name</th>
      <th class="table-cell cYellow c1 entry3">HTML Color Code</th>
      <th class="table-cell cYellow c1 entry4">Shade Group</th>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FFFFFF;"></div>
      </td>
      <td class="table-cell entry2">White</td>
      <td class="table-cell entry3">#FFFFFF</td>
      <td class="table-cell entry4">White</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #F7E7CE;"></div>
      </td>
      <td class="table-cell entry2">Champagne</td>
      <td class="table-cell entry3">#F7E7CE</td>
      <td class="table-cell entry4">White</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #ED2839;"></div>
      </td>
      <td class="table-cell entry2">Red (Pantone)</td>
      <td class="table-cell entry3">#ED2839</td>
      <td class="table-cell entry4">Red</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #ED1B24;"></div>
      </td>
      <td class="table-cell entry2">Red (CMYK) (pigment red)</td>
      <td class="table-cell entry3">#ED1B24</td>
      <td class="table-cell entry4">Red</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FF8200;"></div>
      </td>
      <td class="table-cell entry2">UT orange</td>
      <td class="table-cell entry3">#FF8200</td>
      <td class="table-cell entry4">Orange</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FF8000;"></div>
      </td>
      <td class="table-cell entry2">Orange (wheel)</td>
      <td class="table-cell entry3">#FF8000</td>
      <td class="table-cell entry4">Orange</td>
    </tr>
  </tbody>
</table>

Your code is a rather odd mix of jQuery and native JS. The use of focusColumns4 also assumes that it is relevant to the colour of the checked box, when in fact it contains all the rows regardless of position in the DOM, or relation to the checkbox which triggered the change event.

What you're attempting to do can be achieved much more simply by setting the value of the checkboxes as the colour value to find within the DOM. Then you can build an array of the checked values and use that to hide/show the tr as necessary.

$(function() {
  let $focusInput = $('input.focusbox');
  let $focusRows = $('table#colors tbody tr');
  
  let setRowStates = () => {
    $focusRows.hide();
    let values = $focusInput.filter(':checked').map((i, el) => el.value).get();   
    values.forEach(colour => $focusRows.filter(`:has(.entry4:contains('${colour}'))`).show());
  };
  
  $focusInput.on('change', setRowStates).trigger('change'); 
});
body {
  font-size: 10px;
}

ul,
li {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

table#colors {
  margin-top: 5px;
}

table#colors tbody tr td.entry2 {
  width: 200px;
}

.cf-circle {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #C8C8C8;
  -webkit-transition: border-radius 1s, width 1s, height 1s;
  transition: border-radius 1s, width 1s, height 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<ul>
  <li><label class="focusbox-label"><input class="focusbox" type="checkbox" value="White" checked />White</label>
  </li>
  <li>
    <label class="focusbox-label"><input class="focusbox" type="checkbox" value="Red" />Red</label>
  </li>
  <li>
    <label class="focusbox-label"><input class="focusbox" type="checkbox" value="Orange" />Orange</label>
  </li>
</ul>

<table id="colors">
  <tbody>
    <tr class="head-row">
      <th class="table-cell cYellow c1 entry1">Color</th>
      <th class="table-cell cYellow c1 entry2">Name</th>
      <th class="table-cell cYellow c1 entry3">HTML Color Code</th>
      <th class="table-cell cYellow c1 entry4">Shade Group</th>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FFFFFF;"></div>
      </td>
      <td class="table-cell entry2">White</td>
      <td class="table-cell entry3">#FFFFFF</td>
      <td class="table-cell entry4">White</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #F7E7CE;"></div>
      </td>
      <td class="table-cell entry2">Champagne</td>
      <td class="table-cell entry3">#F7E7CE</td>
      <td class="table-cell entry4">White</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #ED2839;"></div>
      </td>
      <td class="table-cell entry2">Red (Pantone)</td>
      <td class="table-cell entry3">#ED2839</td>
      <td class="table-cell entry4">Red</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #ED1B24;"></div>
      </td>
      <td class="table-cell entry2">Red (CMYK) (pigment red)</td>
      <td class="table-cell entry3">#ED1B24</td>
      <td class="table-cell entry4">Red</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FF8200;"></div>
      </td>
      <td class="table-cell entry2">UT orange</td>
      <td class="table-cell entry3">#FF8200</td>
      <td class="table-cell entry4">Orange</td>
    </tr>
    <tr class="s1">
      <td class="table-cell entry1">
        <div class="color-field cf-default cf-circle" style="background-color: #FF8000;"></div>
      </td>
      <td class="table-cell entry2">Orange (wheel)</td>
      <td class="table-cell entry3">#FF8000</td>
      <td class="table-cell entry4">Orange</td>
    </tr>
  </tbody>
</table>

Related