ajax table

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.4 kB
3
Indexable
Never
<script>
    // $('#dataTikus').DataTable();
    $('#pageId3').text('Data Logger Sensor');
    $('#pageId4').text('Data Logger Sensor');
        
    // Variable to store current page state
    var currentPageUmpan = 1; // Initialize with default page 1
    var currentPageTikus = 1;

    $(document).ready(function() {
        var table = $('#dataTikus').DataTable();
        // Save current page when the page changes
        $('#dataTikus').on('page.dt', function() {
            currentPageTikus = table.page.info().page + 1;
        });
    });

    // Initialize the DataTable when the page loads
    $(document).ready(function() {
        var table = $('#dataUmpan').DataTable();
        // Save current page when the page changes
        $('#dataUmpan').on('page.dt', function() {
            currentPageUmpan = table.page.info().page + 1;
        });
    });

    function updateTableTikus() {
        $.ajax({
            url: '/tikus/public/getDataTikustoTable',
            type: 'GET',
            success: function(response) {
                // Update table rows with new data
                var table = $('#dataTikus').DataTable();
                var pageInfo = table.page.info();
                var currentPageLength = pageInfo.length;

                table.clear().draw(); // Clear existing rows
                $.each(response.tikus, function(index, item) {
                    table.row.add([
                        '<td class="text-center">' + (index + 1) + '</td>',
                        '<td class="text-center">' + item.total + '</td>',
                        '<td class="text-center">' + item.created_at + '</td>'
                    ]).draw(false);
                });

                // Set the page back to the previously saved page
                table.page(currentPageTikus - 1).draw(false);
            },
            error: function(xhr, status, error) {
                console.error('Error:', error);
            }
        });
    }

    function updateTableUmpan() {
        $.ajax({
            url: '/tikus/public/getDataUmpan',
            type: 'GET',
            success: function(response) {
                // Update table rows with new data
                var table = $('#dataUmpan').DataTable();
                var pageInfo = table.page.info();
                var currentPageLength = pageInfo.length;

                table.clear().draw(); // Clear existing rows
                $.each(response.umpan, function(index, item) {
                    table.row.add([
                        '<td class="text-center">' + (index + 1) + '</td>',
                        '<td class="text-center">' + item.jarak + '</td>',
                        '<td class="text-center">' + item.created_at + '</td>'
                    ]).draw(false);
                });

                // Set the page back to the previously saved page
                table.page(currentPageUmpan - 1).draw(false);
            },
            error: function(xhr, status, error) {
                console.error('Error:', error);
            }
        });
    }

    setInterval(updateTableUmpan, 10000);
    setInterval(updateTableTikus, 30000);

    // setInterval(function () {
    //     updateTableUmpan();
    //     updateTableTikus();
    // }, 5000);
    </script>
Leave a Comment