Realtime data and tables

Realtime data and tables

sergassergas Posts: 4Questions: 1Answers: 0
edited December 2022 in Free community support

Hi!
I am doing realtime live table with additional data on page. Can you help me with code for updating data. Your example on datatables website for stock data don't show how to make it realtime and refresh data, so I decided to get data from json file which is generated each 5 second, may be it's not good but simple idea. Now I have problems I don't know how to solve:
1 'Loading' message on table update, each time I get data. Is it possible to remove it, i used trick in language translation to pmake it empty
2 I need to update some data on page outside table, I don't know how to pass it from json. What way to do this will be the best?

I am trying to use two json data files using 'setInterval', but it's not good, can I pass different data from json for table and for other valuse on page?

Any help would be very much appreciated.
thanks


$(document).ready(function () { let table = $('#example').DataTable({ language: {url: 'static/ru.json'}, pageLength: 10, fixedHeader: true, scrollX: true, scrollY: '50vh', scrollCollapse: false, searching: false, paging: false, processing: true, serverside: false, "ajax": { "url": "datafile.txt", "dataSrc": "" }, "columns": [ {"data": "Symbol", "searchable": "false"}, {"data": "Size", "searchable": "false"}, {"data": "Size1", "searchable": "false"}, {"data": "FundingRate", "searchable": "false"}, {"data": "diff", "searchable": "false"}, ], columnDefs: [ {'visible': false, 'targets': [2]}, { targets: [1, 2], render: function (data, type, row) { let color = 'black'; let size1 = row['Size1']; if (size1 < 0) { color = 'red'; } return '<span style="color:' + color + '">' + data + '</span>'; } }] }); }); setInterval(function () { $('#example').DataTable().ajax.reload(); }, 5000); setInterval( function () { $.ajax ({ "url": "datafile1.txt", "dataSrc": "", success: function(result) { document.getElementById('p1').innerHTML = result['p1']; document.getElementById('p2').innerHTML = result['p2']; console.log(result); }, }) }, 5000);

Html code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TEST</title>
    <script type="text/javascript" src="/static/js/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/static/css/jquery.dataTables.min.css"/>
    <script type="text/javascript" src="/static/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/static/css/nev-style.css"/>
    <style>
        td.highlight {
        font-weight: bold;
        color: red;
    }
        .main-wrap {
            display: flex;
            margin: auto;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>

<body>
    <div id="main">
        <div class="main-wrap">
            <div id="p1"></div>
            <div id="p2"></div>
        </div>
        <hr>
    <table id="example" class="display" style="width:100%">
        <thead>
        <tr>
            <th>Symbol</th>
            <th>Size</th>
            <th>Size1</th>
            <th>Funding</th>
            <th>Difference</th>
        </tr>
        </thead>
    </table>
</div>
<script type="text/javascript" src="/static/js/nev-datatable.js"></script>
</body>

</html>

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Answers

  • kthorngrenkthorngren Posts: 21,204Questions: 26Answers: 4,926

    1 'Loading' message on table update, each time I get data. Is it possible to remove it, i used trick in language translation to pmake it empty

    Do you get errors in the browser's console?

    2 I need to update some data on page outside table, I don't know how to pass it from json. What way to do this will be the best?

    Not sure what you mean. You can use -ajax.json() to get the latest JSON response. If this doesn't answer your question please provide more details.

    I am trying to use two json data files using 'setInterval', but it's not good,

    The code in line 50 - 60 doesn't involve Datatables. Please proivde more details of the issue - do you get errors?

    can I pass different data from json for table and for other valuse on page?

    You should be able to do what you want but I'm not clear on what you are asking. Please provide more details.

    There is noting obviously wrong with the code in lines 46-48. Please provide a link to your page or a test case replicating the issues so we can help debug.

    Kevin

  • sergassergas Posts: 4Questions: 1Answers: 0

    Thank you Kevin for fast reply!
    You can check current test version here
    85.192.41.18:5000/

    now it's other version of code using flask backend, but I will go to other version.
    In short I want to make realtime updates in table and some valuse outside table, do you have an example ? What the best way to pass data from backend and how to pass data which is not in table and will be also updated in realtime. Also how to avoid reload data when I make sorting, becose it takes time to read data ?
    thank you

    $(document).ready(function () {
        let table = $('#example').DataTable({
            language: {url: 'static/ru.json'},
            pageLength: 10,
            fixedHeader: true,
            scrollX: true,
            scrollY: '50vh',
            scrollCollapse: false,
            searching: false,
            paging: false,
            processing: true,
            serverSide: true,
            "ajax": {
               
                "url": "http://85.192.41.18:5000/getajax",
                "type": "POST",
                "dataType": "json",
                "dataSrc": "data",
                "csrfmiddlewaretoken": "{{ csrf_token }}"
            },
            "columns": [
                {"data": "Symbol", "searchable": "false"},
                {"data": "Size", "searchable": "false"},
                {"data": "Size1", "searchable": "false"},
                {"data": "FundingRate", "searchable": "false"},
                {"data": "diff", "searchable": "false"},
            ],
    
            columnDefs: [
                    {'visible': false, 'targets': [2]},
                {
                targets: [1, 2],
                render: function (data, type, row) {
                    let color = 'black';
                    let size1 = row['Size1'];
                    if (size1 < 0) {
                        color = 'red';
                    }
                    return '<span style="color:' + color + '">' + data + '</span>';
                }
            }]
    
        });
    });
    
    setInterval(function () {
        $('#example').DataTable().ajax.reload();
    }, 10000);
    
    setInterval( function () {
       
        $.ajax ({
                "url": "http://85.192.41.18:5000/getsixvar",
                "type": "GET",
                "dataType": "json",
                "dataSrc": "data",
                "csrfmiddlewaretoken": "{{ csrf_token }}",
            success: function(result) {
                document.getElementById('p1').innerHTML = result['p1'];
                document.getElementById('p2').innerHTML = result['p2'];
                console.log(result);
                },
            })
        }, 5000);
    
  • kthorngrenkthorngren Posts: 21,204Questions: 26Answers: 4,926

    It looks like your test case is updating the table. Is there an issue with the updates?

    Also how to avoid reload data when I make sorting, becose it takes time to read data ?

    You have server side processing enabled. Do you need server side processing? See the Server Side Processing docs for more info. If you turn off server side processing then the sorting will be performed client side.

    Kevin

  • sergassergas Posts: 4Questions: 1Answers: 0

    I don't know how to pass data which is outside table using datatbles json request, do you have an example ? I need to pass data to html data from json, i think it should be function wich is processing json data.

  • kthorngrenkthorngren Posts: 21,204Questions: 26Answers: 4,926
    edited December 2022

    You re referring to this code?

    setInterval( function () {
        
        $.ajax ({
                "url": "http://85.192.41.18:5000/getsixvar",
                "type": "GET",
                "dataType": "json",
                "dataSrc": "data",
                "csrfmiddlewaretoken": "{{ csrf_token }}",
            success: function(result) {
                document.getElementById('p1').innerHTML = result['p1'];
                document.getElementById('p2').innerHTML = result['p2'];
                console.log(result);
                },
            })
        }, 5000);
    

    It seems to work. Your data is always the same value so it doesn't look like it changes. Datatables is not involved with this. Stack Overflow is a good resource for general Javascript questions.

    Kevin

  • sergassergas Posts: 4Questions: 1Answers: 0

    If I will use this code example, how can I extract variables from it that I will use not in the table, but to display data separately from the table

    $(document).ready(function () {
        $('#example').DataTable({
            ajax: '../ajax/data/arrays.txt',
        });
    });
    
  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin

    You mean how to get the JSON data? You can use the ajax.json() method to get the original JSON object returned from the server. Or you could use rows().data() to get the data for specific rows.

    Allan

Sign In or Register to comment.