Filter jquery list with checkbox

Asked

Viewed 311 times

0

I have this function that filters a table, as the selected field and what was typed in the input, and works.

  function myFunction2() {
  var input, filter, table, tr, td, i, filtro;
  input = document.getElementById("busca2");
  filter = input.value.toUpperCase();
  table = document.getElementById("tablepesquisa2");
  tr = table.getElementsByTagName("tr");
  filtro = document.getElementById("filtroPesquisa2").value;

  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[filtro];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

But I have one checkbox and I wish if he had true, it filtered the product type field in the table, how can I proceed? This is my HTML table

<div class="form-group">
  <div class="col-sm-3">
    <select id="filtroPesquisa2" class="form-control">
      <option value="0">Código</option>
      <option value="1">Descrição</option>
    </select>
  </div>
  <div class="col-sm-7">
    <input type="text" id="busca2" placeholder="Pesquisa.." onkeyup="myFunction2();" class="form-control" />
  </div>
</div>
<div class="modal-body">
  <div class="table-overflow col-sm-12">
    <table class="table table-responsive table-hover" id="tablepesquisa2">
      <thead>
        <tr>
          <th>Código</th>
          <th>Nome</th>
        </tr>
      </thead>
      <tbody>
        @foreach (var item in Model.Produto) {
        <tr>
          <td>@item.Codigo</td>
          <td>@item.nome</td>
          <td align="right">
            <a href="#" onclick="fecha2();CarregaProduto('@item.Codigo');" title="Selecionar"><i class="fa fa-check-circle fa-lg"></i></a>&nbsp;
          </td>
        </tr>
        }

      </tbody>
    </table>
  </div>
</div>

This is the checkbox:

<div class="col-sm-3" style="text-align:left;">
<input type="checkbox" asp-for="Produtos" onclick="checkProduto();" name="Produtos" id="Produtos"/>
<label asp-for="Produtos" class="control-label"></label>
<span asp-validation-for="Produtos" class="text-danger"></span>
</div>
<div class="col-sm-3" style="text-align:left;">
<input type="checkbox" asp-for="Servico" onclick="checkServico();" name="Servico" id="Servico"/>
<label asp-for="Servico" class="control-label"></label>
<span asp-validation-for="Servico" class="text-danger"></span>
</div>

I wanted it this way: link, but with the filters that already exist in my form, I am not able to adapt, because I did not understand very well the code.

I arrived in this function, it does not return error, but does not bring the fields according to the checkbox:

function VerificaCheck() { 
        var input, filter, table, tr, td, i, filtro;
        input = document.getElementById("busca2");
        filter = input.value.toUpperCase();
        table = document.getElementById("tablepesquisa2");
        tr = table.getElementsByTagName("tr");
        filtro = document.getElementById("filtroPesquisa2").value;
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[filtro];
            var tipoProduto = td = tr[i].getElementsByTagName("td")["tipoProduto"];
            if (td) {
                if (tipoProduto == $('#Produtos').prop("checked")) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
  • And if you add the condition if ($('#SEUCHECK').prop("checked")) {} at the time of mounting the filter? There you can manipulate its clauses

  • I don’t understand how to do this, how to filter later.

  • It checks directly in the column HTML and makes visible each row of your table where you have the text typed in the input as you can see in this row if (td.innerHTML.toUpperCase().indexOf(filter) > -1). Lines that don’t have the search are invisible.

  • Yes, until then I understood the code, and it works, but it will filter what is typed, and the checkbox too, I do not know how to implement @Victorlaio

  • Would you like to add a new column with the Product Type and filter how the other filters already work?

  • I would answer yes, the field is bool type.

  • @Victorlaio and if you filtered first by clicking on the checkbox, and then left this function this way, it would be easier, wouldn’t it ? I’m trying to do

Show 2 more comments

1 answer

1


First you should add one input Hidden in its lines to pull this value directly into the filter containing the value of its Product Type (Inside its for even):

@foreach (var item in Model.Produto) {
    <tr>
      <input type="hidden" id="tipoProduto" value="@item.TipoProduto" />
      <td>@item.Codigo</td>
      <td>@item.nome</td>
      <td align="right">
        <a href="#" onclick="fecha2();CarregaProduto('@item.Codigo');" title="Selecionar"><i class="fa fa-check-circle fa-lg"></i></a>&nbsp;
      </td>
    </tr>
}

Then just modify your myFunction2 search method():

  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[filtro];
    tipoProduto = tr[i].getElementsByTagName("input")[0].value;
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1 && tipoProduto == $('#Produtos').prop("checked")) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }

The reasoning is more or less this, maybe you have to make some change to fit your code and your need. But the idea was passed.

I hope it helps you, any doubt I’m at your disposal.

  • It is returning the following error: tr[i]. getElementById is not a Function

  • In this line tipoProduto = tr[i].getElementById("tipoProduto").value;

  • I switched to pick the element by input type now

  • Same problem, I’m putting him inside the td to see if it solves the problem.

  • It has to be inside tr pro getelementsbytagname() work.

  • But he is, I’m even trying to separate only pro checkbox to see if it will work.

  • It really didn’t work, it always returns error in this line, tried in several ways.

  • 1

    I made some changes, but the logic was this, thank you.

  • Not at all! I’m at your disposal!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.