angular-datatables icon indicating copy to clipboard operation
angular-datatables copied to clipboard

ReloadData doesn't work

Open atarason opened this issue 11 years ago • 13 comments

I can't do reload after crud(add/delete) operations in grid.

Can everybody tell me how can I do reload grid? Thanks

atarason avatar Jan 30 '15 09:01 atarason

Depending on the renderer you are using:

  • with the Angular renderer: you will need to do it directly on your model. It's no use to call dtInstance.reloadData()
  • with the Promise renderer: it will clear the table, re-resolve the promise (so a request will be sent to the server to fetch the data) and call the draw() function.
  • with the Ajax renderer: it will call the load() function.

So, check if the request is indeed sent to the server or that your CRUD operations have been indeed taken into account in your server.

l-lin avatar Jan 30 '15 09:01 l-lin

I use angular-datatables with angular.js.

Tell me please how can I reach dtInstance.reloadData()?

atarason avatar Jan 30 '15 16:01 atarason

Data I get from server

atarason avatar Jan 30 '15 16:01 atarason

 $scope.dtOptions = DTOptionsBuilder
                .newOptions()
                .withOption('ajax', {
                    url: ngAuthSettings.apiServiceBaseUri + 'positionapi/GetOpenPositions',
                    type: 'GET',
                    dataSrc: 'data',
                    sAjaxSource: 'openPositions',
                    reloadData: function () {
                        this.reload = true;
                        return this;
                    },
                    headers: {
                        'Access-Control-Allow-Origin': '*',
                        'Access-Control-Allow-Methods': 'POST, GET, DELETE, PUT',
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                        'Authorization': 'Bearer ' + localStorageService.get('authorizationData').token
                    }
                })
                .withOption('fnRowCallback',
                    function (nRow) {
                        $compile(nRow)($scope);
                    })
                .withOption('serverSide', true)
                .withPaginationType('full_numbers')
                .withBootstrap();

atarason avatar Jan 30 '15 16:01 atarason

angular.module('yourModule', ['datatables'])
.controller('YourCtrl', YourCtrl);

function YourCtrl(DTOptionsBuilder, DTInstances) {
    var vm = this;
    var dtInstance;
    vm.dtOptions = DTOptionsBuilder.newOptions();
    vm.doSomeComplexStuff = doSomeComplexStuff;

    DTInstances.getLast().then(function(instance) {
        dtInstance = instance;
    });

    function doingSomeComplexStuff() {
        // Do your things
        dtInstance.reloadData();
        // Do your other things
    }
}

l-lin avatar Jan 30 '15 16:01 l-lin

Grade! Now, my grid has full operations. Thanks for your help

atarason avatar Jan 30 '15 17:01 atarason

I have same problem my code is here:

angular.module('ExchangesController', ['datatables'])
    .controller('ExchangesCtrl', function ($scope, $compile, DTOptionsBuilder, DTColumnBuilder, DTInstances) {
      var vm = this;
      vm.exchanges = {};
      var dtInstance;
      vm.dtOptions = DTOptionsBuilder.newOptions()
          .withOption('ajax', {
            url: 'http://localhost:3000/ServiceProviders/Exchange',
            type: 'POST'
          })
          .withDataProp('data')
          .withOption('processing', true)
          .withOption('serverSide', true)
          .withOption('createdRow', createdRow)
          .withPaginationType('full_numbers');
      vm.dtColumns = [
        DTColumnBuilder.newColumn('id').withTitle('ID'),
        DTColumnBuilder.newColumn('username').withTitle('Username'),
        DTColumnBuilder.newColumn(null).withTitle('Actions')
            .renderWith(actionHtml)
      ];

      DTInstances.getLast().then(function(instance) {
        dtInstance = instance;
      });

      vm.edit = function (exchange) {
        console.log('the exchange is ' + JSON.stringify(exchange) );
        dtInstance.reloadData();
      };

      vm.delete = function (exchange) {
        console.log('delete! the exchange is ' + JSON.stringify(exchange) );
        dtInstance.reloadData();
      };

      function createdRow(row, data, dataIndex) {
        // Recompiling so we can bind Angular directive to the DT
        $compile(angular.element(row).contents())($scope);
      };

      function actionHtml(data, type, full, meta){
        vm.exchanges[data.id] = data;
        return '<button class="btn btn-warning" ng-click="exchanges.edit(exchanges.exchanges[' + data.id + '])">' +
            '   <i class="fa fa-edit"></i>' +
            '</button>&nbsp;&nbsp;' +
            '<button class="btn btn-danger" ng-click="exchanges.delete(exchanges.exchanges[' + data.id + '])" )"="">' +
            '   <i class="fa fa-trash-o"></i>' +
            '</button>';
      };
    });

and it shows me this error: Error: [$injector:unpr] http://errors.angularjs.org/1.4.7/$injector/unpr?p0=DTInstancesProvider%20%3C-%20DTInstances%20%3C-%20ExchangesCtrl at Error (native) at http://localhost:3000/vendor/angular/angular.min.js:6:416 at http://localhost:3000/vendor/angular/angular.min.js:40:409 at Object.d as get at http://localhost:3000/vendor/angular/angular.min.js:40:483 at d (http://localhost:3000/vendor/angular/angular.min.js:38:394) at e (http://localhost:3000/vendor/angular/angular.min.js:39:161) at Object.instantiate (http://localhost:3000/vendor/angular/angular.min.js:39:310) at http://localhost:3000/vendor/angular/angular.min.js:80:313 at A.link (http://localhost:3000/vendor/angular-route/angular-route.min.js:7:268)

(anonymous function) @ angular.js:12599(anonymous function) @ angular.js:9526aa @ angular.js:6781K @ angular.js:7350g @ angular.js:7139(anonymous function) @ angular.js:7082(anonymous function) @ angular.js:7424r @ angular.js:7235x @ angular-route.js:935n.$broadcast @ angular.js:15905(anonymous function) @ angular-route.js:619(anonymous function) @ angular.js:13701n.$eval @ angular.js:15738n.$digest @ angular.js:14948n.$apply @ angular.js:15761h @ angular.js:9945K @ angular.js:10363z.onload @ angular.js:10413

How it will be Ok?

kouroshezzati avatar Nov 20 '15 14:11 kouroshezzati

I fixed it my problem is I did't inject dtInstance in table tag.

kouroshezzati avatar Nov 20 '15 15:11 kouroshezzati

I don't have DTInstances, I keep getting error from angular saying that there's no such service. :-(

dadoadk avatar Mar 21 '16 14:03 dadoadk

Update your angular-datatables version.

l-lin avatar Mar 21 '16 15:03 l-lin

v.0.5.4 wasn't released. Bower cannot update it if it wasn't released.

dadoadk avatar Mar 23 '16 20:03 dadoadk

Very good! Thank's

rabelorp avatar Apr 01 '16 12:04 rabelorp

i want also delete raw after refresh datatable but i can't know how can do that any one please let me know. i am use this version angular-datatable v0.5.2.

Coderwill avatar Mar 27 '17 07:03 Coderwill