简体   繁体   English

如何添加搜索功能以过滤名称列表

[英]How can i add a search functionality to filter the list of names

I had retrieved a list of employees from the database using php code and I want to add a search functionality to filter the names. 我使用php代码从数据库中检索了一份员工列表,我想添加一个搜索功能来过滤名称。

How can I do this using js or php? 我怎么能用js或php做到这一点? I got struct in this from last one week, searched online but unable to find the solution. 我在过去的一周里得到了结构,在网上搜索但无法找到解决方案。

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Search Name">

<ul id="myUL" class="contacts-list">
  <?php
  $status=1;
  $sql1 =  $dbh->prepare("SELECT * FROM employee WHERE status='$status'");
  $sql1->execute();

  if($sql1->rowCount() > 0) { 
    while($row = $sql1->fetch(PDO::FETCH_ASSOC)){
  ?>
      <li>
        <a href="messages.php?employee_id=<?php echo $row['employee_id']; ?>">
          <div class="contacts-list-info">
            <span class="contacts-list-name">
          <?php echo $row['fname']." ".$row['lname']; ?>
            <small class="contacts-list-date pull-right">
              <?php if($row['status'] == '1') { echo 'Online'; } else { echo 'Offline'; } ?>
            </small>
          </span>
            <span class="contacts-list-msg">
            <?php echo $row['role']; ?>
          </span>
          </div>
        </a>
      </li>
  <?php
    }
  }
  ?>
</ul>

If you want to filter just on the client side, you can do something like this: 如果您只想在客户端进行过滤,可以执行以下操作:

Get all contacts (eg li tags) and apply a filter based on the users input. 获取所有联系人(例如li标签)并根据用户输入应用过滤器。

 function search() { var input = document.getElementById('search').value.toLowerCase(); var contacts = document.getElementsByTagName('li'); for (var i = 0; i < contacts.length; i++) { if (contacts[i].innerText.toLowerCase().includes(input)) { contacts[i].style.display = "block"; } else if (!contacts[i].innerText.toLowerCase().includes(input)) { contacts[i].style.display = "none"; } } } 
 <input type="text" id="search" onkeyup="search()"> Enter something to filter <br> <ul> <li>Some Dude</li> <li>Some Dudine</li> <li>John Doe</li> <li>Erica Doe</li> </ul> 

You can use like query as below: 您可以使用如下查询:

Select * from employee where status LIKE '%$status%';

As per your query you can get employee status from database. 根据您的查询,您可以从数据库中获取员工状态。

Hope this works for you. 希望这对你有用。

What do you want to do. 你想让我做什么。 If you want suggestion box like in search input field than u can simple use ajax request to fetch suggestion form DB and add typehead js library and than paste following code. 如果你想在搜索输入字段中的建议框比你可以简单地使用ajax请求来获取建议表格DB并添加typehead js库而不是粘贴下面的代码。

$('#id').typeahead({
                ajax: {
                    url: '../employeeList',
                    method: 'post',
                    triggerLength: 1
                }
            });

Here is a working exapmle for filtering in client-side using your structure. 这是一个使用您的结构在客户端进行过滤的工作原型

 document.getElementById("myInput").addEventListener('keyup', function() { var input = this.value.toLowerCase(); var people = document.getElementsByClassName("contacts-list-info"); for (var i of people) { if (i.innerText.toLowerCase().includes(input)) { i.closest("li").style.display = "list-item"; } else { i.closest("li").style.display = "none"; } } }); 
 <input type="text" id="myInput" placeholder="Search for names.." title="Search Name"> <ul id="myUL" class="contacts-list"> <li> <a href="messages.php?employee_id=1"> <div class="contacts-list-info"> <span class="contacts-list-name"> Test Test <small class="contacts-list-date pull-right"> Offline </small> </span> <span class="contacts-list-msg"> Manager </span> </div> </a> </li> <li> <a href="messages.php?employee_id=1"> <div class="contacts-list-info"> <span class="contacts-list-name"> Second Test <small class="contacts-list-date pull-right"> Online </small> </span> <span class="contacts-list-msg"> Manager </span> </div> </a> </li> <li> <a href="messages.php?employee_id=1"> <div class="contacts-list-info"> <span class="contacts-list-name"> Third Test <small class="contacts-list-date pull-right"> Online </small> </span> <span class="contacts-list-msg"> Employee </span> </div> </a> </li> <li> <a href="messages.php?employee_id=1"> <div class="contacts-list-info"> <span class="contacts-list-name"> Fourth Test <small class="contacts-list-date pull-right"> Offline </small> </span> <span class="contacts-list-msg"> Team member </span> </div> </a> </li> </ul> 

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

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