Javascript “如何显示消息”;“未找到结果”;用于表格研究?

Javascript “如何显示消息”;“未找到结果”;用于表格研究?,javascript,html,spring,thymeleaf,Javascript,Html,Spring,Thymeleaf,我正在用spring boot在intellij上做一个简单的项目,我做了一个带有搜索输入的表。我想在搜索失败且未找到任何结果时显示一条消息。我怎么做 这是我的桌子: <div> <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.."> <table id="

我正在用spring boot在intellij上做一个简单的项目,我做了一个带有搜索输入的表。我想在搜索失败且未找到任何结果时显示一条消息。我怎么做

这是我的桌子:

<div>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="tablePatients" width="100%" border="1px solid black" class="table table-striped">
    <thead class = "thead-dark">
    <tr>
    <th>Id</th>
    <th>Nome</th>
    <th>Cognome</th>
    </tr>
    </thead>
    <tbody id="mytable">
    <tr th:each="patient : ${allPatients}">
    <td th:text="${patient.id}">1</td>
    <td th:text="${patient.name}">w</td>
    <td th:text="${patient.surname}">e</td>
    <td> <a href="show?id=10" th:href="@{show(idPatient=${patient.id} , idDoctor=${doctor.id})}"> show </a></td>
    <td> <a href="addPrescription?id=10" th:href="@{newPrescription(idPatient=${patient.id} , idDoctor=${doctor.id})}"> add prescription </a></td>
    </tr>
    </tbody>
    </table>

</div>

身份证件
诺姆
同源的
1.
W
E
这是我用于研究的脚本:

<script>
$(document).ready(function () {
    $("#myInput").on("keyup", function () { //here #input textbox id
        var value = $(this).val().toLowerCase();
        $("#mytable tr").filter(function () { //here #table table body id
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
        });
    });
});

$(文档).ready(函数(){
$(“#myInput”)。在(“keyup”上,函数(){//here#input文本框id
var value=$(this.val().toLowerCase();
$(“#mytable tr”).filter(函数(){//here#表体id
$(this).toggle($(this).text().toLowerCase().indexOf(value)>-1)
});
});
});

问题是,如何在使用的模板引擎中做到这一点。这不是一个真正的Spring问题,也不是HTML或JavaScript问题

您应该能够检查
allPatients
是否为空,然后呈现一些内容。如果您使用并希望在服务器端执行此操作:

<tr th:if="${#lists.isEmpty(allPatients)}">
  <td>no patients found...</td>
</tr>
<tr th:each="patient : ${allPatients}">
...

没有发现病人。。。
...
如果要在JavaScript逻辑中执行此操作,请执行以下操作(看起来它使用jQuery):

  • 筛选行后,检查结果是否为空
  • 如果是,请添加另一行以呈现空消息,例如:
$(“#mytable”).append('no patients found…');
  • 如果结果不是空的,请删除
    #空消息

Alternativley,您可以随时添加行,但使用
show()
hide()
而不是append和remove。

我认为您的标记应该更精确。这更多的是关于jquery和/或您使用的任何模板引擎的问题。也许是百里香?
$('#mytable').append('<tr id="empty-message"><td>no patients found...</td></tr>');