我有桌子在该表中,有一个status列,其中包含文本activeinactivewarning。我为status列创建了一个过滤器,但问题是当过滤器为active时,也会显示inactive行。

我如何做到这一点,以便如果用户选择active,则不会显示inactive

<select name="status" id="status" class="form-control">
  <option value="">Select Status</option>
  <option value="active">active</option>
  <option value="inactive">inactive</option>
  <option value="warning">warning</option>
</select>


$("#status", this).on("click", function() {
  if (SomeTable.column(4).search() !== this.value) {
    SomeTable
      .column(4)
      .unique()
      .search(this.value)
      .draw();
  }
});

最佳答案

最好在选择而不是单击时使用更改功能

<select name="status" id="status" onchange="filterData(event)" class="form-control">
<option value="">Select Status</option>
<option value="active">active</option>
<option value="inactive">inactive</option>
<option value="warning">warning</option>




----------------------功能类似----------------------------- ---

function filterData(event) {
    debugger
    var status = event.target.value;
    var rows = $("#myTable tr");
    rows.each(function (index, element) {
        if (!status)
            $(element).show();
        if (index > 0 && status) {
            if (status.toLowerCase() !== element.children[3].innerText.toLowerCase())
                $(element).hide();
            else
                $(element).show();
        }
    })
}


- - - - 表 - - - - - - - - -

<table id="myTable">
<tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
    <th>Action</th>
</tr>
<tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
    <td>Active</td>
</tr>
<tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
    <td>Warning</td>
</tr>
<tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
    <td>InActive</td>
</tr>
<tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
    <td>Warning</td>
</tr>
<tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
    <td>InActive</td>
</tr>
<tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
    <td>Active</td>
</tr>




----------为您的表提供一个名称,还检查该行具有活动,非活动,警告动作的子元素---

关于javascript - 使用DataTable search()搜索唯一值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59154717/

10-11 21:44
查看更多