简体   繁体   中英

How i can refresh a part of page using javascript

I try to filter information, but I only need that just the table refresh and display the information using javascript, and I can not find a good method to do it

I try to use this code for my page, but I dont know how to implement it

JS

    let url="https://server.test-cors.org/server?id=2934825&enable=true&status=200&credentials=false&methods=GET"

    async function refresh() {
          btn.style.visibility= "hidden";
          dynamicPart.innerHTML="Loading..."
          dynamicPart.innerHTML=await(await fetch(url)).text();
          setTimeout(refresh,2000);}

HTML of the metod

HTML
<div id="staticPart">Here is static part of page
<button id="btn" onclick="refresh()">Start refreshing (2s)</button>
</div>

<div id="dynamicPart">Dynamic part</div>

And this is my table in Html

<div class='d-flex justify-content-center' id="contentTable">
            <table class='table table-stripped' border=1 id="table">
                <thead class= 'thead-dark'>
                    <tr>
                        <th>#</th>
                        <th>Fecha</th>
                        <th>Documento</th>
                        <th>Nombres</th>
                        <th>Apellidos</th>
                        <th>Sede</th>
                        <th>Siglas</th>
                        <th>Administrador</th>                        
                        <th>Acciones</th>                        
                    </tr>
                </thead>
                <tbody>
                    <a href="/Jomar/users_control/controller/ControlAsisController.php?action=todos"></a>
                    <?php
                        for($i = 0; $i < count($datos); $i++){
                            $id = $datos[$i]['id_controlAsis'];
                            $rutaEd = "/Jomar/users_control/controller/ControlAsisController.php?action=editar&id_controlAsis=$id";
                            $rutaEl = "/Jomar/users_control/controller/ControlAsisController.php?action=eliminar&id_controlAsis=$id";
                            echo "<tr>";
                            echo "<td>". ($i+1) ."</td>";
                            echo "<td> {$datos[$i]['fecha']} </td>";
                            echo "<td> {$datos[$i]['documento']} </td>";
                            echo "<td> {$datos[$i]['nombres']} </td>";
                            echo "<td> {$datos[$i]['apellidos']} </td>";
                            echo "<td> {$datos[$i]['sede']} </td>";
                            echo "<td> {$datos[$i]['siglas']} </td>";
                            echo "<td> {$datos[$i]['administrador']} </td>";                            
                            echo "<td>
                            <a href='$rutaEd' class='badge badge-primary'>Editar </a>
                            <a href='$rutaEl' class='badge badge-warning'>Eliminar </a>
                            </td>";
                            echo "</tr>";
                        }

                    ?>
                </tbody>
            </table>
        </div>

Thanks for the help, sorry for my English

Making a lot of assumptions. You need to define the 'btn' and 'dynamicPart' variables before you use them.

Example:

let btn = document.querySelector("#id_of_some_button_in_html");
let dynamicPart = document.querySelector("#id_of_a_div");

Then you just need to call

refresh();

then the

setTimeout(refresh,2000);

of your code will run every 2 seconds.

let url="https://server.test-cors.org/server?id=2934825&enable=true&status=200&credentials=false&methods=GET"

async function refresh() {
      let btn = document.querySelector("#id_of_some_button_in_html");
      let dynamicPart = document.querySelector("#id_of_a_div");

      btn.style.visibility= "hidden";
      dynamicPart.innerHTML="Loading..."
      dynamicPart.innerHTML=await(await fetch(url)).text();
      setTimeout(refresh,2000);}

refresh();

Understanding this is not considered 'good' code, but I am making the assumption that you are just starting to learn JS and this code is not something that will be put into production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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