Заголовок таблицы исчезает при поиске в таблице HTML с помощью функции Javascript - PullRequest
0 голосов
/ 29 мая 2019

У меня возникли проблемы с функцией поиска в php-скрипте. Функция поиска написана на javascript и должна проходить по всей таблице ниже и выбирать соответствующий контент в то же время, когда пользователь вводит в поле поиска. Поиск функций на вкладке «Beskrivelse». Однако, когда я начинаю печатать в поле поиска, заголовок таблицы (Имя пользователя, Beskrivelse, Links, Kommentar, Billede, Rediger) также исчезает, поскольку он также является частью таблицы.

Мой вопрос; как сохранить постоянный заголовок при поиске в строках таблицы под заголовком? Заголовок не должен исчезать в любое время.

Показывается заголовок таблицы Заголовок таблицы скрыт

Вот полный код скрипта php:

<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM 3difpensum ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM 3difstortab ORDER BY ID DESC"); // using mysqli_query instead
?>

<html>
<head>
	<title>Homepage</title>
	<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")[1];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

</script>
<style>
	.container {
		width: 100%;
		text-align: center;
		margin-top: 50px;
		padding: 5px;
	}

	.searchfield {
		margin: 0 auto;
		width: 400px;
		height: 40px;
		font-size: 14px;
		display: inline-block;
		font-family: Helvetica;
	}

	.addmore {
		background-color: #535b63; /* Green */
		border: none;
		color: white;
		padding: 11px 0px;
		text-align: center;
		text-decoration: none;
		display: inline-block;
		font-size: 14px;
		color: #ffffff;
		font-family: Helvetica;
		width: 400px;
	}

	.secondcontainer {
		width: 100%;
		text-align: center;
		padding: 5px;
	}
	.thirdcontainer {
		width: 100%;
		text-align: center;
		margin-top: 30px;
	}
	.tableview {
		margin: 0 auto;
	}
	.tableheader {
		text-align: center;
		font-family: Helvetica;
		font-size: 14px;
		background-color: #535b63;
		color: #fff;
	}
	.tableheader td {
		padding: 5px;
	}
</style>
</head>

<body>
<a href="logout.php">Logout</a>

<div class="container">
	<input type="text" class="searchfield" id="myInput" onkeyup="myFunction()" placeholder="Søg efter Beskrivelse...">
</div>
<div class="secondcontainer">
	<a href="add.html" class="addmore">Add New Data</a><br/><br/>
</div>

<div class="thirdcontainer">
	<table width='70%' class="tableview" border=0 id="myTable">
	<tr class="tableheader">
		<td>Username</td>
		<td>Beskrivelse</td>
		<td>Links</td>
		<td>Kommentar</td>
		<td>Billede</td>
		<td>Rediger</td>
	</tr>
	<?php
	//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
	while($res = mysqli_fetch_array($result)) {
		echo "<tr>";
		echo "<td>".$res['ejer']."</td>";
		echo "<td>".$res['beskrivelse']."</td>";
		echo "<td>".$res['links']."</td>";
		echo "<td>".$res['kommentar']."</td>";
		echo "<td>"."<img src='".$res['billede']."'></td>";
		echo "<td><a href=\"edit.php?ID=$res[ID]\">Edit</a> | <a href=\"delete.php?ID=$res[ID]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
	}
	?>
	</table>
</div>

</body>
</html>

1 Ответ

0 голосов
/ 29 мая 2019

Ok. Проверьте это. Ваша проблема решена

<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM 3difpensum ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM 3difstortab ORDER BY ID DESC"); // using mysqli_query instead
?>

<html>
<head>
	<title>Homepage</title>
	<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")[1];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

</script>
<style>
	.container {
		width: 100%;
		text-align: center;
		margin-top: 50px;
		padding: 5px;
	}

	.searchfield {
		margin: 0 auto;
		width: 400px;
		height: 40px;
		font-size: 14px;
		display: inline-block;
		font-family: Helvetica;
	}

	.addmore {
		background-color: #535b63; /* Green */
		border: none;
		color: white;
		padding: 11px 0px;
		text-align: center;
		text-decoration: none;
		display: inline-block;
		font-size: 14px;
		color: #ffffff;
		font-family: Helvetica;
		width: 400px;
	}

	.secondcontainer {
		width: 100%;
		text-align: center;
		padding: 5px;
	}
	.thirdcontainer {
		width: 100%;
		text-align: center;
		margin-top: 30px;
	}
	.tableview {
		margin: 0 auto;
	}
	.tableheader {
		text-align: center;
		font-family: Helvetica;
		font-size: 14px;
		background-color: #535b63;
		color: #fff;
	}
	.tableheader td {
		padding: 5px;
	}
</style>
</head>

<body>
<a href="logout.php">Logout</a>

<div class="container">
	<input type="text" class="searchfield" id="myInput" onkeyup="myFunction()" placeholder="Søg efter Beskrivelse...">
</div>
<div class="secondcontainer">
	<a href="add.html" class="addmore">Add New Data</a><br/><br/>
</div>

<div class="thirdcontainer">
	<table width='70%' class="tableview" border=0 id="myTable">
	<tr class="tableheader">
		<th>Username</th>
		<th>Beskrivelse</th>
		<th>Links</td>
		<th>Kommentar</th>
		<th>Billede</th>
		<th>Rediger</th>
	</tr>
	<?php
	//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
	while($res = mysqli_fetch_array($result)) {
		echo "<tr>";
		echo "<td>".$res['ejer']."</td>";
		echo "<td>".$res['beskrivelse']."</td>";
		echo "<td>".$res['links']."</td>";
		echo "<td>".$res['kommentar']."</td>";
		echo "<td>"."<img src='".$res['billede']."'></td>";
		echo "<td><a href=\"edit.php?ID=$res[ID]\">Edit</a> | <a href=\"delete.php?ID=$res[ID]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
	}
	?>
	</table>
</div>

</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...