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

时间:2018-10-15 17:16:31

标签: javascript jquery

我有一个功能完美的

但是出现了在研究中增加另一个领域的需求,我不知道如何解决。它的形式有一个复选框,如果已填写,则将获取表中的数据为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,则只需要过滤product = true的字段,如果是,则仅过滤这些字段另一个是真的

编辑 通过选择正确的列并与true

进行比较,我设法解决了

1 个答案:

答案 0 :(得分: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";
                }
            }
        }
    }