Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Updating the datatable using socket-io doesn't work #681

Closed
MasterSpl1nter opened this issue Apr 7, 2016 · 2 comments
Closed

Updating the datatable using socket-io doesn't work #681

MasterSpl1nter opened this issue Apr 7, 2016 · 2 comments

Comments

@MasterSpl1nter
Copy link

Hi, I'm using jQuery DataTables with socket.io on AngularJS, and I'm pushing an item to the data binding list on a socket message and digesting afterwards. When it happened, the datatable recreated itself instead of just updating the data and not working properly. I'm also randomly get the error *Warning: Cannot reinitialise DataTable, and when I do, the datatable failed to display. (afterwards I get Loading... displayed in the table.)

AngularJS

var app = angular.module('App', ['ui.bootstrap','ngAnimate', 'datatables']);
app.factory('socket', function () {
    var socket = io.connect('http://' + document.domain + ':' + location.port + '/t');
    return socket;
});

app.controller('controller', function ($scope, socket, $timeout, DTOptionsBuilder, DTColumnBuilder) {
    $scope.data=[];
    $scope.headers = {'Name':'name','Title':'title'}
    socket.on('data', function (d) {
        d = angular.fromJson(d);
        $scope.data.push(d);
        $scope.$digest();
    });

    $scope.dtOptions = DTOptionsBuilder.newOptions().withPaginationType('full_numbers').withOption('bInfo', false);
    $scope.dtColumns = [];
    $scope.dtInstance = {};

    for (key in $scope.headers) {
        $scope.dtColumns.push(DTColumnBuilder.newColumn($scope.headers[key]).withTitle(key));
    }
});

HTML

<table id="tbl" datatable="ng" dt-options="dtOptions" dt-columns="dtColumns" dt-instance="dtInstance" class="table table-striped row-border hover">
   <thead></thead>
   <tbody>
       <tr class="fade" ng-model="d" ng-repeat="d in data">
          <td>{{ d.name }}</td>
          <td>{{ d.title }}</td>
       </tr>
   </tbody>
</table>

In socket.on i'm using $scope.$digest(); because if i won't I won't see updates in the table. it stays empty (only after I refresh the page I see the rows but still it doesn't update), I tried with $scope.dtInstance.rerender(); but the table stays empty.

@gubbanoa
Copy link

gubbanoa commented Apr 8, 2016

Seeing the the same issue when updating from websocket and running $scope.apply, as described by GiankOtarola commented on Mar 19, 2015 in issue #250 .

@fullstackwebdev
Copy link

fullstackwebdev commented Jul 20, 2016

edit: vinodf fix worked better
#262

@l-lin l-lin closed this as completed Nov 25, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants