简体   繁体   English

使用 JavaScript 中的多列过滤表数据

[英]Filtering table data using multiple columns in JavaScript

I am trying to make a filter/search table and I am using the JavaScript from this site.我正在尝试制作一个过滤器/搜索表,我正在使用这个站点的 JavaScript。

Here's the script:这是脚本:

function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

You can change which column to be used as the filter by changing the index in "td = tr[i].getElementsByTagName("td") [0] ;"您可以通过更改 "td = tr[i].getElementsByTagName("td") [0] ;" 中的索引来更改要用作过滤器的列

0 means the first column will be used, 1 means the second column, etc. 0 表示将使用第一列,1 表示第二列,依此类推。

Using the same script, how can I use multiple columns to be the filter?使用相同的脚本,如何使用多个列作为过滤器?

EDIT:编辑:

This is the tableinside my 'exampletable.php':这是我的“exampletable.php”中的表格:

<input type="text" id="myInput" onKeyUp="myFunction()" placeholder="Search"

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">

 
<?php
    include 'connection.php'; //to connect to my database, "database"
    
    $data = mysqli_query($con, "SELECT*FROM database;");
    while($d = mysqli_fetch_array($data)){
?>
                      
    <table class="table table-bordered" id="myTable" width="100%" cellspacing="0">
    <thead>
    
          <tr>
              <th class="text-center">Name</th>
              <th class="text-center">Age</th>
              <th class="text-center">Hobby</th>
          </tr>
    </thead>

          <tr>
              <td class="text-center"><?php echo $d['name']; ?></td>
              <td><?php echo $d['age']; ?></td>
              <td><?php echo $d['hobby']; ?></td>
          </tr>

    </table>

          <?php } ?>
       

As you can see, I have the column "Name, Age, and Hobby" using the function inside the JavaScript, if the index is 0 (the index for the filter column) then the first column (Name) will be used as the filter.如您所见,我在 JavaScript 中使用 function 列“姓名、年龄和爱好”,如果索引为 0(过滤器列的索引),则第一列(姓名)将用作过滤器.

For example, when I type "Catherine" in the search bar, then the page will only show the data with "Catherine" inside the Name column.例如,当我在搜索栏中输入“Catherine”时,页面将仅显示 Name 列中带有“Catherine”的数据。

What I wanted to do is to use say both Name and Hobby column as the filter.我想要做的是使用名称和爱好列作为过滤器。 So, say when I type "Cat" in the search bar, then the page will only show the data with "Cat" inside the Name and also Hobby column.因此,假设当我在搜索栏中输入“猫”时,页面将只显示名称和爱好列中带有“猫”的数据。

Adding another for loop to evaluate all tds (columns) will help you with that.添加另一个 for 循环来评估所有 tds(列)将帮助您。

This might be helpfull:这可能会有所帮助:

// This variable switch if the filter catch any result
    var isset = false;

    //This loop evalaute all rows
    for (i = 0; i < tr.length; i++) {
        //Here we search for all columns in this row
        //note that we get rid of [0] at the end because we're looking for all
        //tds, not just the first one
        let tds = tr[i].getElementsByTagName("td");
        //Here we evaluate each column for this row
        for (let td of tds) {
            //If td has a value
            if (td) {
                txtValue = td.textContent || td.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    //If the filter catch any result, we show the row
                    isset = true;
                }
            }
        }
        //The decision of show or hide the row is made here because
        //the loop has to evaluate all columns before decide if show or hide the entire row
        if (isset) {
            //If the filter catch any result, we show the row
            tr[i].style.display = "";
        } else {
            //If the filter catch no result, we hide the row
            tr[i].style.display = "none";
        }
        //This is to reset the variable for the next loop
        isset = false;
    }

Your entire function will be something like this:您的整个 function 将是这样的:

function myFunction() {
    // Declare variables
    var input, filter, table, tr, td, i, txtValue;
    // This variable switch if the filter catch any result
    var isset = false;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("myTable");
    tr = table.getElementsByTagName("tr");

    // This variable switch if the filter catch any result
    var isset = false;

    //This loop evalaute all rows
    for (i = 0; i < tr.length; i++) {
        //Here we search for all columns in this row
        //note that we get rid of [0] at the end because we're looking for all
        //tds, not just the first one
        let tds = tr[i].getElementsByTagName("td");
        //Here we evaluate each column for this row
        for (let td of tds) {
            //If td has a value
            if (td) {
                txtValue = td.textContent || td.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    //If the filter catch any result, we show the row
                    isset = true;
                }
            }
        }
        //The decision of show or hide the row is made here because
        //the loop has to evaluate all columns before decide if show or hide the entire row
        if (isset) {
            //If the filter catch any result, we show the row
            tr[i].style.display = "";
        } else {
            //If the filter catch no result, we hide the row
            tr[i].style.display = "none";
        }
        //This is to reset the variable for the next loop
        isset = false;
    }
}

I have tested the code with this script我已经用这个脚本测试了代码

<input type="text" id="myInput" onKeyUp="myFunction()" placeholder="Search">



<table class="table table-bordered" id="myTable" width="100%" cellspacing="0">
    <thead>

        <tr>
            <th class="text-center">Name</th>
            <th class="text-center">Age</th>
            <th class="text-center">Hobby</th>
        </tr>
    </thead>

    <tr>
        <td class="text-center">Alejandro</td>
        <td>12</td>
        <td>Football</td>
    </tr>
    <tr>
        <td class="text-center">Peralta</td>
        <td>12</td>
        <td>Alejandro</td>
    </tr>
    <tr>
        <td class="text-center">Casemiro</td>
        <td>12</td>
        <td>Soccer</td>
    </tr>
    <tr>
        <td class="text-center">Moreno</td>
        <td>12</td>
        <td>Tenis</td>
    </tr>
    <tr>
        <td class="text-center">Alejandro</td>
        <td>12</td>
        <td>Moreno</td>
    </tr>

</table>

<script>
function myFunction() {
    // Declare variables
    var input, filter, table, tr, td, i, txtValue;
    // This variable switch if the filter catch any result
    var isset = false;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("myTable");
    tr = table.getElementsByTagName("tr");

    // This variable switch if the filter catch any result
    var isset = false;

    //This loop evalaute all rows
    for (i = 0; i < tr.length; i++) {
        //Here we search for all columns in this row
        //note that we get rid of [0] at the end because we're looking for all
        //tds, not just the first one
        let tds = tr[i].getElementsByTagName("td");
        //Here we evaluate each column for this row
        for (let td of tds) {
            //If td has a value
            if (td) {
                txtValue = td.textContent || td.innerText;
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    //If the filter catch any result, we show the row
                    isset = true;
                }
            }
        }
        //The decision of show or hide the row is made here because
        //the loop has to evaluate all columns before decide if show or hide the entire row
        if (isset) {
            //If the filter catch any result, we show the row
            tr[i].style.display = "";
        } else {
            //If the filter catch no result, we hide the row
            tr[i].style.display = "none";
        }
        //This is to reset the variable for the next loop
        isset = false;
    }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM