Reload an Angularjs table from time to time

Asked

Viewed 836 times

1

My Angularjs page displays a table with data coming from a JSON. What I want is that the table is updated from time to time, because the data is updated according to the second and I show only the last 10.

<tr ng-repeat="linha in linhas">
  <td ><abbr title="{{linha.descricao}}"> <a href=xxx.php?id_chamado={{linha.chamado}}>{{linha.chamado}}</a> </abbr>  </td>  
  <td >{{linha.data}}</td>
  <td >{{linha.nome}}</td>
  <td >{{linha.acao}}</td>
  <td >{{linha.cliente}}</td>   
  <td ><span class="label label-success">{{linha.sistema}}</span></td>
</tr>

Is there any simple way to reach this goal ?

My controller, everything works perfect in the page load. I just don’t know how to re-invoke this function inside the controller.

// JavaScript Document
var app = angular.module('sadApp', []);
app.controller('sadCtrl', function ($scope, $http) {
    $http.get('http://192.168.0.14/a/historicochamadologlidosJson.php').
    success(function(data,  status, headers, config) {
        $scope.linhas = data;
    })
});

I’ve tried this code here:

angular.element(document.getElementById('MainWrap')).scope().$apply();

where Mainwrap is the id of div:

<div ng-controller="sadCtrl" id="MainWrap"> 
  • Query the data every X milliseconds, and update the callback linhas on your model. I imagine that a part of it is already done, I don’t understand exactly where you are having difficulties.

  • The query is in the controller, I don’t know how to invoke it again, because Angular already automatically invokes when the page is loaded.

  • OK - include the controller in the question. and what I’ve tried to do to invoke again

1 answer

2


You can try the code below. What I did was to wrap in a "private" function the section that searches the data, and call this function both at startup, and at intervals determined by a timer:

// JavaScript Document
var app = angular.module('sadApp', []);
app.controller('sadCtrl', function ($scope, $http) {

    // Função que atualiza os dados
    // Somente disponível aqui dentro, não é método público
    function atualizaDados() {
        $http.get('http://192.168.0.14/a/historicochamadologlidosJson.php').
        success(function(data,  status, headers, config) {
            $scope.linhas = data;
        });
    }

    // Executa a função na inicalização
    atualizaDados();

    // Executa novamente a cada 60 segundos (60.000ms)
    // Altere o intervalo para o que achar mais adequado.
    setInterval(atualizaDados, 60000);
});
  • It worked Perfect. Exactly what I was looking for. Thank you very much. And with that I learned a little more from Angularjs

  • Actually I didn’t even use anything specific from angular other than what you already had, my complements were even normal JS.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.