Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

javascript - How can I filter a generated list from a query from PHP in JS?

I have a list of files from my database. I'm looking for a way to query them. but I'm receiving an error, which is... it only display the first item if my input matches the first. This is the form

<form>
    <input type="text" placeholder="Filter" aria-label="Search"
           onkeyup="myFunction()" id="myInput">
</form>

below is the php

<?php
  $query = "SELECT classname,cdate FROM classnametb`";
  $res = mysqli_query($db, $query);
  while ($r = mysqli_fetch_assoc($res)){
  $classname = $r['classname'];
  $classdate = $r['cdate'];
  $classdate = date("m/d/y", strtotime($classdate));
?>
<div id="myUL">
  <b>
    <a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
       class="btn btn-primary btn-icon" style="width: 20% !important;">
      <span class="btn-inner--text"><?php echo($classname . '(' . $classdate . ')'); ?></span>

    </a>
    <?php } ?>
  </b>
</div>

and I have the js here

    function myFunction() {
        var input, filter, div, b, a, i, txtValue;
        input = document.getElementById("myInput");
        filter = input.value.toUpperCase();
        div= document.getElementById("myUL");
        b = div.getElementsByTagName("b");
        for (i = 0; i < b.length; i++) {
            a = b[i].getElementsByTagName("a")[0];
            txtValue = a.textContent || a.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                b[i].style.display = "";
            } else {
                b[i].style.display = "none";
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are generating mutliple myUl divs inside while loop and you have not close </b> & </div> .Instead change your php code like below :

<div id="myUL"> <!--add opening tag here-->
<?php
  $query = "SELECT classname,cdate FROM classnametb`";
  $res = mysqli_query($db, $query);
  while ($r = mysqli_fetch_assoc($res)){
  //some codes..
?>
  <b>
    <a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
       class="btn btn-primary btn-icon" style="width: 20% !important;">
      <span class="btn-inner--text"><?php echo($classname . '(' . $classdate . ')'); ?></span>

    </a>
  </b> <!--close `b` tag-->
<?php } ?>
</div> <!--close `div` tag-->

Now, you can simply your js code by using .forEach to iterate through your b tag and hide/show it when match found.

Demo Code (Added comment in code) :

function myFunction() {
  var input, filter;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  //loop through `b` tag 
  document.querySelectorAll('#myUL b').forEach(function(el) {
    el.style.display = 'none'; // hide it
  });
  //loop through div -> `a` tag
  document.querySelectorAll('#myUL a.btn-icon').forEach(function(el) {
    //compare 
    if (el.textContent.toUpperCase().indexOf(filter) > -1) {
      el.closest('b').style.display = "block"; //if match show that div
    }
  })
}
b {
  display: block
}
<input type="text" placeholder="Filter" aria-label="Search" onkeyup="myFunction()" id="myInput">
<div id="myUL">
  <b>
     <a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
       class="btn btn-primary btn-icon" style="width: 20% !important;">
      <span class="btn-inner--text">abc</span>

    </a>   
  </b>
  <b>
     <a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
       class="btn btn-primary btn-icon" style="width: 20% !important;">
      <span class="btn-inner--text">abc13</span>

    </a>   
  </b>
  <b>
     <a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
       class="btn btn-primary btn-icon" style="width: 20% !important;">
      <span class="btn-inner--text">assee</span>

    </a>   
  </b>
  <b>
     <a href="studentclass.php?course=<?php echo($classname); ?>&&cdate=<?php echo($classdate); ?>"
       class="btn btn-primary btn-icon" style="width: 20% !important;">
      <span class="btn-inner--text">apple</span>

    </a>   
  </b>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...