2

I want to make drop-down table filter depend on item.id. I tried several ways but they didn't work. What I want is to filter the table depending on item.id.

It worked using this code but when I try to change the selected value or item it does not work. Please help.

$("#filterText").change(function() {
  var rex = $('#filterText').val();

  if (rex == "/all/") {
    clearFilter()
  } else {
    var id = $('#' + rex);
    id.hide();
    // $('.toggle').filter(function () {
    //    return rex.test($(this).text());
    // }).show();
  }
});

function clearFilter() {
  $('#filterText').val('');
  $('.toggle').show();
}
<select id='filterText'>
  @foreach (var item in Model)
  {
    <option value="@item.ID">@item.Title.ToString()</option>
  }
</select>

<div class="panel-group" id="accordion">
  <div id="faqs" class="faqs">
    <table>
      @foreach (var item in Model) 
      { 
        foreach (var child in item.questions) 
        {
          <tr id="@item.ID" class="toggle faq faq-marketplace faq-authors">
            <td class="togglet">@child.Title</td>
            <td class="togglec">@child.Answer</td>
          </tr>
        } 
      }
    </table>
  </div>
</div>
1
  • $('#filterText').val(''); Logical changes to values do not generate events. As such, this line of code will not generate a change event. If you want to force a change event to happen you can trigger one. $('#filterText').val('').trigger('change'); I would try this first. Commented Feb 20, 2018 at 15:38

1 Answer 1

2

You can do something like:

If value is not all, select all trs and show all. Then, select all other trs except the value usine .not() and hide.

$(function() {
  $("#filterText").change(function() {
    var rex = $('#filterText').val();
    if (rex != "all") $("table tr").show().not('#' + rex).hide();
    else $("table tr").show();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id='filterText'>
   <option value="all"> All </option>
   <option value="1">Value 1</option>
   <option value="2">Value 2</option>
   <option value="3">Value 3</option>
</select>

<table>
  <tr id="1" class="toggle faq faq-marketplace faq-authors">
    <td class="togglet">Title 1</td>
    <td class="togglec">Whatever 1</td>
  </tr>
  <tr id="2" class="toggle faq faq-marketplace faq-authors">
    <td class="togglet">Title 2</td>
    <td class="togglec">Whatever 2</td>
  </tr>
  <tr id="3" class="toggle faq faq-marketplace faq-authors">
    <td class="togglet">Title 3</td>
    <td class="togglec">Whatever 3</td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.