0

I am trying to load data into an angularJs datatable. I can log deferred.promise. But I can't get it to return the Json object that the angular datatables need.

Here is the controller

app.controller('AdminListCtrl', 
       function ($scope, $rootScope, $q, 
                      DTOptionsBuilder, DTColumnDefBuilder, adminService, ApiService) {

    var vm = this;
    vm.deferred = $q.defer();
    vm.loadAdmin = function () {
        ApiService.get("admin").then(function (response) {
            if (response) {
                if (response.success == true) {
                    adminService.adminList = response.data;
                    vm.deferred.resolve(adminService.adminList);
                    console.log(vm.deferred.promise);
                    return vm.deferred.promise;
                }else{
                    console.log(response);
                }
            }else {
                console.log('error request ');
            }
        });
    }
    vm.loadAdminInit = function () {
      if (adminService.adminList.length == 0) {
        vm.loadAdmin();
      }else{
       console.log(adminService.adminList);
      }
    }

    function stateChange(iColumn, bVisible) {
      console.log('The column', iColumn, ' has changed its status to', bVisible);
    }

    vm.dtOptions = DTOptionsBuilder
      .fromFnPromise(vm.loadAdminInit())
      .withPaginationType('full_numbers')
      .withDisplayLength(10)
      // Add Bootstrap compatibility
      .withBootstrap()
      // Active ColVis plugin
      .withColVis()
      // Add a state change function
      .withColVisStateChange(stateChange)
      // Exclude the last column from the list
      .withColVisOption('aiExclude', [2])
      // Add Table tools compatibility
      .withTableTools('scripts/vendor/datatables/TableTools/swf/copy_csv_xls_pdf.swf')
      .withTableToolsButtons([
        'copy',
        'print', {
          'sExtends': 'collection',
          'sButtonText': 'Save',
          'aButtons': ['csv', 'xls', 'pdf']
        }
      ]);
    vm.dtColumns = [
      DTColumnDefBuilder.newColumnDef(0),
      DTColumnDefBuilder.newColumnDef(1),
      DTColumnDefBuilder.newColumnDef(3),
      DTColumnDefBuilder.newColumnDef(4),
      DTColumnDefBuilder.newColumnDef(5).notSortable()
    ];

  });
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • Cant you just use `.fromFnPromise(function() { return ApiService.get("admin").$promise })` ?? – davidkonrad May 16 '16 at 10:13
  • I would like the response stored in an angular service there by it will exist through out the app no matter the view the user is on. Last time I had the result on the controllers every time I switched views I had to request the data again – Matina Matina May 19 '16 at 06:13
  • 1: what error are you getting ? 2: are you trying to store data in adminService.adminList so that you can access it later ? 3: where did you define your .dataprop() ? 4: what is ApiService.get, can I see the code for that ? – Wcan May 26 '16 at 06:14

0 Answers0