在函数中添加另一个搜索字段

问题描述 投票:4回答:1

我有一个完美的功能

但是需要出现,为研究添加另一个领域,我不知道如何解决。它在表单中有一个复选框,如果填充,它会在表中获取数据为true,如果不是数据为false,如何在下面的代码中添加?如果可能的话,我有疑问。

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";
      }
    }
  }
}
<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>
<div class="col-sm-3" style="text-align:left;">
<input type="checkbox" asp-for="Produtos" 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" name="Servico" id="Servico"/>
<label asp-for="Servico" class="control-label"></label>
<span asp-validation-for="Servico" class="text-danger"></span>
</div>

在这个表中,有这两个字段,如果它们被标记,则需要进行过滤,如果标记了产品,则只需要带有product = true的字段,如果是另一个字段,则只有另一个字段。是真的

编辑我设法解决,只需选择正确的列并与true比较

javascript jquery
1个回答
0
投票

我通过这种方式解决了这个问题:

  for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[filtro];
        td1 = tr[i].getElementsByTagName("td")[4];
        td2 = tr[i].getElementsByTagName("td")[3];
        var tbBoolean = ($(td2).text() == "True" ? true : false);
        if ($('#Venda').prop("checked") == true) {
            if (td) {
                if (td.innerHTML.toUpperCase().indexOf(filter) > -1 && $(td1).text() == idEmpresa || $(td1).text() == "") {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
        else {
            if (td) {
                if (td.innerHTML.toUpperCase().indexOf(filter) > -1 && tbBoolean == false) {
                    if ($(td1).text() == idEmpresa || $(td1).text() == "") {
                        tr[i].style.display = "";
                    }
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.