johan
johan

Reputation: 1012

Angular Datatables use source object

With Angular Datatables I want to pre-load a JSON object with Ajax so that I can re-use the object elsewhere without doing another ajax request. But how do I load this object into the datatable?

    .controller('ResponsiveDatatableCtrl', function ($scope, $rootScope, DTOptionsBuilder, DTColumnBuilder, apiserv, $filter, $state, $http) {


    $scope.dataLoading2 = true;
    var vm = this;  
    var data = "?db="+ $rootScope.globals.currentUser.agents[$rootScope.globals.currentDB].db_name;
    var url = apiserv+"api.files.php"+data;

    var headers = {'Content-Type': 'application/x-www-form-urlencoded'};
    $http({
        method: 'POST',
        url: url,
        headers: headers,

    })
        .success(function (response) {
            $rootScope.globals.files = response;
            $scope.dataLoading2 = false;
            //console.log($rootScope.globals.files);


        });

    vm.dtOptions = DTOptionsBuilder.fromFnPromise($rootScope.globals.files)
        .withPaginationType('full_numbers')
        .withBootstrap() 
        .withOption('responsive', true);

})

Upvotes: 2

Views: 1066

Answers (1)

johan
johan

Reputation: 1012

Ok I have attempted the following and it seems to call my code under success but then the table doesn't update?

    vm.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {
        url: url,
        type: 'POST',
        headers: headers,
        data: function(data, dtInstance) {

        },
        success: function(response) {
            $rootScope.globals.files = response;
        }
    })
        .withPaginationType('full_numbers')
        .withBootstrap()
        .withOption('responsive', true);

Upvotes: 2

Related Questions