简体   繁体   中英

refresh datatable after database update

In my html page,I have a table that displays data that come from my database. I want refresh the datatable when my database is updated without refresh all the page. I'm using PHP framwork Laravel.

 <table id="example" class="table"> <thead> <tr class="headings"> <th>ID &nbsp;&nbsp;&nbsp;</th> <th>first name </th> <th>last name </th> <th>E-mail </th> <th>birthday </th> </tr> </thead> <tbody> @foreach ($users as $user) <tr class="even pointer"> <td class=" ">{{ $user->id }}</td> <td class=" ">{{ $user->name }}</td> <td class=" ">{{ $user->name2 }}</td> <td class=" ">{{ $user->email }}</td> <td class=" ">{{ $user->birthday }}</td> </tr> @endforeach </tbody> </table> 

 setTimeout(function(){ $( "#mytable" ).load( "your-current-page.html #mytable" ); }, 2000); <button id="refresh-btn">Refresh Table</button> <script> $(document).ready(function() { function RefreshTable() { $( "#mytable" ).load( "your-current-page.html #mytable" ); } $("#refresh-btn").on("click", RefreshTable); // OR CAN THIS WAY // // $("#refresh-btn").on("click", function() { // $( "#mytable" ).load( "your-current-page.html #mytable" ); // }); }); </script> 

How to refresh table contents in div using jquery/ajax

Use this package, its great and i use it all the time. Which is a laravel wrapper for datatables.net

Github DataTables

Example using jQuery. Note this is but one of 1000 ways to do this. This just outlines the basic idea of the process of building a table using AJAX and Laravel. It's not meant to be a drop-in code segment.

Basic table code

<table id="example" class="table">
    <thead>
        <tr class="headings">
            <th>ID &nbsp;&nbsp;&nbsp;</th>
            <th>first name </th>
            <th>last name </th>
            <th>E-mail </th>
            <th>birthday </th>
        </tr>
    </thead>

    <tbody>
        <?php // empty for now ?>
    </tbody>
</table>

tablecontents.blade.php

<?php // Requires the $users table ?>
@foreach ($users as $user)
     <tr class="even pointer">
         <td class=" ">{{ $user->id }}</td>
         <td class=" ">{{ $user->name }}</td>
         <td class=" ">{{ $user->name2 }}</td>
         <td class=" ">{{ $user->email }}</td>
         <td class=" ">{{ $user->birthday }}</td>
     </tr>
@endforeach

AjaxController.php (or similar)

function getUpdatedTable() {
     $users = //get users
     return view("tablecontents", [ "users", $users ]);
} 

JavaScript

function updateTable() {
    $.ajax({
        url: "ajax/getUpdatedTable",
        success: function (data) {
             $("#example tbody").html(data);
        }

    });
}

$(document).ready(function () { 
     updateTable();
     setInterval(updateTable, 5000); //Re-update every 5 seconds     
});

Like mentioned inside the comments, you can use frameworks like jQuery to build a real-time module.

$( document ).ready( function () {
    $.get('generic-handler.php')
        .done( function (data) {
            $('#realtime').innerHTML = data;
        });
});

Inside your PHP file you put your code to display the database and then you can wrap it in a setInterval so your request is sent every X number of seconds.

Documentation:

$.get - jQuery
setInterval - JS

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