Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - My table filter with javascript is not working

I'm doing a project on intellij with springboot and i have a dynamic table. I put a search box and add a script that in theory it has work but it doesn't.

This is my table with the input box:

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="tablePatients" width="100%" border="1px solid black" >
<thead>
<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>

This is the script:

<script>
$(document).ready(function(){
    $("#myInput").on("keyup", function() {
        var value = $(this).val().toLowerCase();
        $("#myTable tr").filter(function() {
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
        });
    });
});

These are the libraries that i imported in the head:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

Can someone please help me to understand what i did wrong?

question from:https://stackoverflow.com/questions/65942659/my-table-filter-with-javascript-is-not-working

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

1 Answer

0 votes
by (71.8m points)

your table id is wrong in your script, try this:

$(document).ready(function(){
    $("#myInput").on("input", function() {
        var value = $(this).val().toLowerCase();
        $("#mytable tr").filter(function() {
            if($(this).text().toLowerCase().indexOf(value) === -1){
              $(this).hide();
            }else{
              $(this).show();
            }
        });
    });
});
<input type="text" id="myInput" placeholder="Search for names..">
<table id="tablePatients" width="100%" border="1px solid black" >
<thead>
<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>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...