Is it possible to create a table in Angular DataTables with a factory?

2

I'm using the famous angular-datatables plugin that combines Jquery Datatables with Angular.

I just learned how to use a factory to fetch data from a JSON file.

My question is: Could you use a factory on angular-datatables to use it as source ?

EDITED (06/22/2016)

This is what I have tried:

  • A global factory that rescues the data of a JSON with many URLs
  • //Factory que traerá la data Global
    statisticsModule.factory('globalFactory', function($rootScope, $http){
    
      var globalFactory = {};
    
      globalFactory.getUrl = function(){
        return $http.get('../statistics/php/config_statistics.json');
      };
    
      return globalFactory;
    
    });
  • A controller that uses that factory, find the specific URL that I want and use it as source in the datatable :
  • //Controller / Controlador
    statisticsModule.controller("dataController", dataController); //Fin controlador
    
      function dataController($scope, $http, globalFactory, DTOptionsBuilder, DTColumnBuilder){
        //Promise con el factory
        globalFactory.getUrl().then(function(response){
    
          //Obtener data
          var urlGlobal = response.data;
          //Filtrar data. arrayReportBD : Arreglo con las URL de los reportes
          var deserialize = angular.fromJson(urlGlobal.config.graph_conf_array.arrayReportBD);
          //Data Distribución de Estatus
          var urlStatus = deserialize[0];
    
          //Obtener data de Distribución de Estatus
          $http.get(urlStatus).success(function(data){
    
          console.log(data);
    
          var vm = this;
          vm.dtOptions = DTOptionsBuilder.fromSource(data)
          .withDOM('lfrtip')
          .withPaginationType('full_numbers')
          .withLanguage({
            "sEmptyTable":     "No hay datos para cargar en la tabla",
            "sInfo":           "Mostrando _START_ de _END_ de _TOTAL_ entradas",
            "sInfoEmpty":      "Mostrando 0 de 0 de 0 entradas",
            "sInfoFiltered":   "(filtradas _MAX_ entradas totales)",
            "sInfoPostFix":    "",
            "sInfoThousands":  ",",
            "sLengthMenu":     "Mostrar _MENU_ entradas",
            "sLoadingRecords": "Cargando...",
            "sProcessing":     "Procesando...",
            "sSearch":         "Buscar:",
            "sZeroRecords":    "No se encontraron registros",
            "oPaginate": {
                "sFirst":    "Primera",
                "sLast":     "Última",
                "sNext":     "Siguiente",
                "sPrevious": "Anterior"
              },
              "oAria": {
                "sSortAscending":  ": activar para ordenar de forma ascendente",
                "sSortDescending": ": activar para ordenar de forma descendente"
              }
            });
            vm.dtColumns = [
              DTColumnBuilder.newColumn('gob_code').withTitle('Cód. Gob.'),
              DTColumnBuilder.newColumn('fci_code').withTitle('Cód. FCI'),
              DTColumnBuilder.newColumn('name').withTitle('NOMBRE'),
              DTColumnBuilder.newColumn('status').withTitle('ESTATUS')
            ];
    
          }).error(function(err){
    
          });//Fin $http
    
        });//Fin promise
    
      }//Fin función
  • This is the part of my view that corresponds to the datatable:
  • <div ng-controller="dataController as showCase">
      <table datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" class="table table-striped table-bordered"></table>
    </div>

    POST-DATA (EDITED) If they ask:

  • It brings me the data correctly, what I can not do is use it in the datatable
  • asked by Ulises Vargas De Sousa 21.06.2016 в 17:45
    source

    1 answer

    2

    ** RESOLVED (06/23/2016) **

    I was able to solve it, here in another StackOverflow post in English I published the answer:

    link

        
    answered by 23.06.2016 / 16:03
    source