gpt4 book ai didi

javascript - 当没有行与搜索输入匹配时,如何隐藏表的 元素? - Javascript

转载 作者:行者123 更新时间:2023-11-28 19:11:57 25 4
gpt4 key购买 nike

我有一张表,其中包含正式员工、试用员工和受训人员的列表。搜索栏正在运行并突出显示匹配的人员姓名。我想要发生的是删除 <thead>未在其下找到结果时的表格元素。

Here's the sample output

另外,我想要 <thead>如果用户没有在搜索栏中输入任何内容,元素及其下方的元素将再次显示。与此输出不同:

I don't want this happening on the table

我真的希望我可以在我的 Javascript 函数中添加一些东西,这样我的代码就不会改变太多。

这是我的数据库“multi_login”的数据库结构:

multi_login database structure


HTML:


<div class="col-sm-12" style="margin-bottom:0;padding-bottom: 0;">
<table class="table-borderless text-center" style="width:90%; height:10%; margin:0 auto; padding:0;">
<tr>
<td style="width:25%;">
</td>
<td style="width:50%;">
<div class="nj_searchbar">
<i class="material-icons" style="color:#cc0000;font-weight: bold;">search</i>
<input type="text" id="search-personnel" onkeyup="searchPerson()" placeholder="Who are you looking for?" >
<i class="material-icons clear" style="display: none;color:#cc0000;font-weight: bold;">clear</i>
<div id="resultFound">...</div>

</div>
</td>
<td style="width:50%;">
<button href="#" title="Add Personnel" id="add" class="add-btn-dark btn" style="float:right;" data-toggle="modal" data-target="#editPersonModal"><i class="material-icons icon_dark">group_add</i>&nbsp;Add New Personnel</button>
</td>
</tr>
</table>


</div>
<div class="col-sm-12">
<!-- <button href="#" title="Add Personnel" id="add" class="add-btn-dark btn btn-outline-dark" style="float:right;margin-right:5%;margin-bottom:1%;" data-toggle="modal" data-target="#editPersonModal"><i class="material-icons icon_dark">group_add</i>&nbsp;Add New Personnel</button> -->
<table class="table table-hover table-bordered text-center personTable" id="personnelTable" style="width:90%; margin:0 auto;padding:0;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);border-radius:50px;">
<thead class="employeeList">
<tr>
<th class="theader" style="color:white;">Employee Name</th>
<th class="theader" style="color:white;">Date of Birth</th>
<th class="theader" style="color:white;">TIN</th>
<th class="theader" style="color:white;">SSS Number</th>
<th class="theader" style="color:white;">PhilHealth Number</th>
<th class="theader" style="color:white;">Pag-IBIG MID No./RTN</th>
<th class="theader" style="color:white;">ACTIONS</th>
</tr>
</thead>
<?php display_employee(); ?>
<thead class="probList">
<tr>
<th class="theader" style="color:white;">Probationary Name</th>
<th class="theader" style="color:white;">Date of Birth</th>
<th class="theader" style="color:white;">TIN</th>
<th class="theader" style="color:white;">SSS Number</th>
<th class="theader" style="color:white;">PhilHealth Number</th>
<th class="theader" style="color:white;">Pag-IBIG MID No./RTN</th>
<th class="theader" style="color:white;">ACTIONS</th>
</tr>
</thead>
<?php display_probationary(); ?>
<thead class="trainList">
<tr>
<th class="theader" style="color:white;">Trainee Name</th>
<th class="theader" style="color:white;">Date of Birth</th>
<th class="theader" style="color:white;">TIN</th>
<th class="theader" style="color:white;">SSS Number</th>
<th class="theader" style="color:white;">PhilHealth Number</th>
<th class="theader" style="color:white;">Pag-IBIG MID No./RTN</th>
<th class="theader" style="color:white;">ACTIONS</th>
</tr>
</thead>
<?php display_trainee(); ?>
</table>
</div>

CSS:

.table tr td
{
font-size:15px;
vertical-align: middle;

}

.personTable td:hover
{
color:#cc0000;
font-weight: 500;
}

/*background color of headers in table*/
.theader
{
background-color: black;
}

PHP:函数.php

// connect to database
$userhost = 'root';
$userPass = '';
$dataBase = 'multi_login';
$host = 'localhost';
$db = mysqli_connect('localhost', 'root', '', $dataBase);


function display_employee()
{
global $db;
$query = "SELECT * FROM employees WHERE emp_type='Regular' AND emp_status='Active'";
$result = mysqli_query($db, $query);

echo "<tbody>";
while($row = mysqli_fetch_array($result))
{
$empMname = $row['emp_mname'];
$empbdate = $row['emp_bdate'];
// to display Month and Day only
$newdate = date('F d', strtotime($empbdate));

// to hide the first 6 numbers for TIN Number
$empTin = $row['emp_tin'];
if ($empTin !='')
{
$empTin = substr_replace($empTin, "xxxxxx" , 0, 6);
$newtin = sprintf("%s-%s-%s",
substr($empTin, 0,3),
substr($empTin, 3,3),
substr($empTin, 6));
}
else if ($empTin =='')
{
$newtin = '';
}
// to hide the first 7 numbers for SSS Number
$empSss = $row['emp_sss'];
if ($empSss !='')
{
$empSss = substr_replace($empSss, "xxxxxxx" , 0, 7);
$newsss = sprintf("%s-%s-%s",
substr($empSss, 0,2),
substr($empSss, 2,7),
substr($empSss, 9));
}
else if ($empSss =='')
{
$newsss = '';
}
// to hide the first 9 numbers for PHILHEALTH Number
$empPhealth = $row['emp_phealth'];
if ($empPhealth !='')
{
$empPhealth = substr_replace($empPhealth, "xxxxxxxxx" , 0, 9);
$newphealth = sprintf("%s-%s-%s",
substr($empPhealth, 0,2),
substr($empPhealth, 2,9),
substr($empPhealth, 11));
}
else if ($empPhealth =='')
{
$newphealth = '';
}
// to hide the first 10 numbers for PAGIBIG Number
$empPagibig = $row['emp_pagibig'];
if ($empPagibig !='')
{
$empPagibig = substr_replace($empPagibig, "xxxxxxxxxx" , 0, 10);
$newpagibig = sprintf("%s-%s-%s",
substr($empPagibig, 0,4),
substr($empPagibig, 4,4),
substr($empPagibig, 8));
}
else if ($empPagibig =='')
{
$newpagibig = '';
}
//Get the first letter of Middle Name and insert a dot
if($empMname != '')
{
$newMname = substr($empMname, 0,1).".";
}//if no Middle name, it will only be blank
else if($empMname == '')
{
$newMname = "";
}



echo "<tr><td>" . $row['emp_lname'] . ", " . $row['emp_fname'] . " " . $newMname . "</td><td>" . $newdate . "</td><td>" . $newtin . "</td><td>" . $newsss . "</td><td>" . $newphealth . "</td><td>" . $newpagibig . "</td>
<td>
<button id=\"$row[emp_id]\" class='btn btn-sm edit_data' data-toggle='modal' data-target='#editPersonModal' title='Edit' name='Edit'><i class='material-icons mIcon' alt='Edit'>edit</i></button>&nbsp;
<button id=\"$row[emp_id]\" class='btn btn-sm delete_data' data-toggle='modal' data-target='#deleteModal' title='Delete' name='Delete'><i class='material-icons mIcon' alt='Delete'>delete</i></button>
</td>
</tr>";
}
echo "</tbody>";
}

function display_probationary()
{
global $db;
$query = "SELECT * FROM employees WHERE emp_type='Probationary' AND emp_status='Active'";
$result = mysqli_query($db, $query);

echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$empMname = $row['emp_mname'];
$empbdate = $row['emp_bdate'];
// to display Month and Day only
$newdate = date('F d', strtotime($empbdate));
// to hide the first 6 numbers for TIN Number
$empTin = $row['emp_tin'];
if ($empTin !='')
{
$empTin = substr_replace($empTin, "xxxxxx" , 0, 6);
$newtin = sprintf("%s-%s-%s",
substr($empTin, 0,3),
substr($empTin, 3,3),
substr($empTin, 6));
}
else if ($empTin =='')
{
$newtin = '';
}
// to hide the first 7 numbers for SSS Number
$empSss = $row['emp_sss'];
if ($empSss !='')
{
$empSss = substr_replace($empSss, "xxxxxxx" , 0, 7);
$newsss = sprintf("%s-%s-%s",
substr($empSss, 0,2),
substr($empSss, 2,7),
substr($empSss, 9));
}
else if ($empSss =='')
{
$newsss = '';
}
// to hide the first 9 numbers for PHILHEALTH Number
$empPhealth = $row['emp_phealth'];
if ($empPhealth !='')
{
$empPhealth = substr_replace($empPhealth, "xxxxxxxxx" , 0, 9);
$newphealth = sprintf("%s-%s-%s",
substr($empPhealth, 0,2),
substr($empPhealth, 2,9),
substr($empPhealth, 11));
}
else if ($empPhealth =='')
{
$newphealth = '';
}
// to hide the first 10 numbers for PAGIBIG Number
$empPagibig = $row['emp_pagibig'];
if ($empPagibig !='')
{
$empPagibig = substr_replace($empPagibig, "xxxxxxxxxx" , 0, 10);
$newpagibig = sprintf("%s-%s-%s",
substr($empPagibig, 0,4),
substr($empPagibig, 4,4),
substr($empPagibig, 8));
}
else if ($empPagibig =='')
{
$newpagibig = '';
}
//Get the first letter of Middle Name and insert a dot
if($empMname != '')
{
$newMname = substr($empMname, 0,1).".";
}//if no Middle name, it will only be blank
else if($empMname == '')
{
$newMname = "";
}

echo "<tr><td>" . $row['emp_lname'] . ", " . $row['emp_fname'] . " " . $newMname . "</td><td>" . $newdate . "</td><td>" . $newtin . "</td><td>" . $newsss . "</td><td>" . $newphealth . "</td><td>" . $newpagibig . "</td>
<td>
<button id=\"$row[emp_id]\" class='btn btn-sm edit_data' data-toggle='modal' data-target='#editPersonModal' title='Edit' name='Edit'><i class='material-icons mIcon' alt='Edit'>edit</i></button>&nbsp;
<button id=\"$row[emp_id]\" class='btn btn-sm delete_data' data-toggle='modal' data-target='#deleteModal' title='Delete' name='Delete'><i class='material-icons mIcon' alt='Delete'>delete</i></button>
</td>
</tr>";
}
echo "</tr>";
}

function display_trainee()
{
global $db;
$query = "SELECT * FROM employees WHERE emp_type='Trainee' AND emp_status='Active'";
$result = mysqli_query($db, $query);

echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$empMname = $row['emp_mname'];
$empbdate = $row['emp_bdate'];
// to display Month and Day only
$newdate = date('F d', strtotime($empbdate));
// to hide the first 6 numbers for TIN Number
$empTin = $row['emp_tin'];
if ($empTin !='')
{
$empTin = substr_replace($empTin, "xxxxxx" , 0, 6);
$newtin = sprintf("%s-%s-%s",
substr($empTin, 0,3),
substr($empTin, 3,3),
substr($empTin, 6));
}
else if ($empTin =='')
{
$newtin = '';
}
// to hide the first 7 numbers for SSS Number
$empSss = $row['emp_sss'];
if ($empSss !='')
{
$empSss = substr_replace($empSss, "xxxxxxx" , 0, 7);
$newsss = sprintf("%s-%s-%s",
substr($empSss, 0,2),
substr($empSss, 2,7),
substr($empSss, 9));
}
else if ($empSss =='')
{
$newsss = '';
}
// to hide the first 9 numbers for PHILHEALTH Number
$empPhealth = $row['emp_phealth'];
if ($empPhealth !='')
{
$empPhealth = substr_replace($empPhealth, "xxxxxxxxx" , 0, 9);
$newphealth = sprintf("%s-%s-%s",
substr($empPhealth, 0,2),
substr($empPhealth, 2,9),
substr($empPhealth, 11));
}
else if ($empPhealth =='')
{
$newphealth = '';
}
// to hide the first 10 numbers for PAGIBIG Number
$empPagibig = $row['emp_pagibig'];
if ($empPagibig !='')
{
$empPagibig = substr_replace($empPagibig, "xxxxxxxxxx" , 0, 10);
$newpagibig = sprintf("%s-%s-%s",
substr($empPagibig, 0,4),
substr($empPagibig, 4,4),
substr($empPagibig, 8));
}
else if ($empPagibig =='')
{
$newpagibig = '';
}
//Get the first letter of Middle Name and insert a dot
if($empMname != '')
{
$newMname = substr($empMname, 0,1).".";
}//if no Middle name, it will only be blank
else if($empMname == '')
{
$newMname = "";
}

echo "<tr><td>" . $row['emp_lname'] . ", " . $row['emp_fname'] . " " . $newMname . "</td><td>" . $newdate . "</td><td>" . $newtin . "</td><td>" . $newsss . "</td><td>" . $newphealth . "</td><td>" . $newpagibig . "</td>
<td>
<button id=\"$row[emp_id]\" class='btn btn-sm edit_data' data-toggle='modal' data-target='#editPersonModal' title='Edit' name='Edit'><i class='material-icons mIcon' alt='Edit'>edit</i></button>&nbsp;
<button id=\"$row[emp_id]\" class='btn btn-sm delete_data' data-toggle='modal' data-target='#deleteModal' title='Delete' name='Delete'><i class='material-icons mIcon' alt='Delete'>delete</i></button>
</td>
</tr>";
}
echo "</tr>";
}

Javascript:

// function to search personnel on table
function searchPerson() {

// Declare variables
var input, filter, table, tr, td, i, txtValue, index, countResult;
input = document.getElementById("search-personnel");
filter = input.value.toUpperCase();
table = document.getElementById("personnelTable");
tr = table.getElementsByTagName("tr");
countResult = 0;

// 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;

// first clear any previously marked text
// this strips out the <mark> tags leaving text (actually all tags)
td.innerHTML = txtValue;

index = txtValue.toUpperCase().indexOf(filter);
if (index > -1)
{

// using substring with index and filter.length
// nest the matched string inside a <mark> tag
td.innerHTML = txtValue.substring(0, index) + "<span class='highlight'>" + txtValue.substring(index, index + filter.length) + "</span>" + txtValue.substring(index + filter.length);

tr[i].style.display = "";

if(input.value != "")
{
countResult++;
}
else if(input.value == "")
{
countResult = -1;
}


}
else
{
tr[i].style.display = "none";

}
}



} // end of for loop for table rows


if(countResult > 1)
{
document.getElementById("resultFound").innerHTML = "<span class='nj_have'>" + countResult + " Results Found.. </span>";
}
else if(countResult == 1)
{
document.getElementById("resultFound").innerHTML = "<span class='nj_have'>" + countResult + " Result Found.. <span>";
}
else if(countResult == 0)
{
document.getElementById("resultFound").innerHTML = "<span class='nj_havent'>PERSONNEL NOT FOUND! Try again...</span>";
}
else if(input.value == "" && countResult < 0)
{
document.getElementById("resultFound").innerHTML = "<span>...</span>";
}

}

最佳答案

我将使用 jQuery,但如果需要,您也可以使用纯 javascript。

首先,当搜索为空时:

$('.personnelTable').find('thead').removeClass('no-results');

然后,在遍历 tr 之前,您将属性 data-has-results 添加到每个 thead,默认值为0:

$('.personnelTable').find('thead').attr('data-has-results', 0);

然后,对于每个匹配搜索词的tr:

$(tr[i]).closest('thead').attr('data-has-results', 1);

然后,在循环之后,隐藏没有结果的thead:

$('.personnelTable').find('thead').each(function () {
if ($(this).attr('data-has-results') == 0) {
$(this).addClass('no-results');
} else {
$(this).removeClass('no-results');
}
});

还有 CSS:

thead.no-results {
display: none;
}

关于javascript - 当没有行与搜索输入匹配时,如何隐藏表的 <thead> 元素? - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59099842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com