0

I'm trying to do a pagination using angularJS + codeigniter, I have a controller called by "Cursos" and a function called by "api" to get the data

the function api code:

    $currentPage = $this->uri->segment(3); # Pagina atual
    $sizePage = $this->uri->segment(4); # Tamanho da pagina (num de itens que serão listados)
    $searchText = $this->uri->segment(5); # Argumento de pesquisa
    $offset = ($currentPage - 1) * $sizePage; // Obter o offeset da pagina para obter os resultados paginados
    $r = $this->cursos->obterCursos(0,$nomeCurso=null,"array",$sizePage,$offset);
    $array = array('DadosDosCursos' => $r, 'totalCount' => count($r));
    header('Content-Type: application/json');


    return $this->output
    ->set_content_type('application/json')
    ->set_status_header(200)
    ->set_output(json_encode($array));

    exit(json_encode($array));

and the result looks ok:

{"DadosDosCursos":[{"i_imc_ID":"2","i_imc_cargaHoraria":"40 horas","i_imc_categoriaIDS":"1,2","i_imc_dataCreated":"2017-02-20 11:36:36","i_imc_etapasAprendizagem":"Etapa 1\r\netapa 2","i_imc_imagemLogo":"web-design-criacao-de-sites_04042016162740.png","i_imc_imagemMiniatura":"imagepress-impressos-profissionais_05012016115837.jpg","i_imc_metaDescriptions":"meta description","i_imc_metaTags":"meta tags","i_imc_nome":"Nome do curso 2","i_imc_softwaresIDS":"1","i_imc_status":"1","i_imc_subtitulo":"Subtitulo aqui","i_imc_textoApresentacao":"Apresenta\u00e7\u00e3o"},{"i_imc_ID":"1","i_imc_cargaHoraria":"80 horas","i_imc_categoriaIDS":null,"i_imc_dataCreated":"2017-02-17 00:00:00","i_imc_etapasAprendizagem":"Etapa 1","i_imc_imagemLogo":"modelagem-com-3ds-max--v-ray--photoshop_04042016163311.png","i_imc_imagemMiniatura":"imageweb-criacao-de-sites_14122015100956.jpg","i_imc_metaDescriptions":"meta description","i_imc_metaTags":"meta tags","i_imc_nome":"Curso de desenvolvimento de sistemas","i_imc_softwaresIDS":"1","i_imc_status":"0","i_imc_subtitulo":"subtitulo","i_imc_textoApresentacao":null}],"totalCount":2}

view source code

JSON formated

my angularJS controller:

app.controller('activityTableCtrl', function($scope, $http) {

    $scope.currentPage = 1;
    $scope.totalItems = 0;
    $scope.pageSize = 10;
    $scope.searchText = '';
    getData();

    function getData() {
    $http.get('http://localhost/estudo/site/cursos/api/' + $scope.currentPage + '/' + $scope.pageSize
    //$http.get("../assets/api/database.json"
        )
        .then(function(data) {




            $scope.activity = [];
            $scope.totalItems = data.totalCount;
            $scope.startItem = ($scope.currentPage - 1) * $scope.pageSize + 1;
            $scope.endItem = $scope.currentPage * $scope.pageSize;

            if ($scope.endItem > $scope.totalCount) {
                $scope.endItem = $scope.totalCount;
            }

            angular.forEach(data.DadosDosCursos, function(temp){
                $scope.activity.push(temp);
            });

        });
    }

    $scope.pageChanged = function() {
        getData();
    }
    $scope.pageSizeChanged = function() {
        $scope.currentPage = 1;
        getData();
    }
    $scope.searchTextChanged = function() {
        $scope.currentPage = 1;
        getData();
    }
 })

I'm getting this error on console.log SyntaxError: Unexpected token  in JSON at position 0

I tried to load from another URL with JSON and everything was done well

1 Answer 1

1

This may or may not answer your question but it would be better if you just replaced this loop:

angular.forEach(data.DadosDosCursos, function(temp){
                $scope.activity.push(temp);
            });

With:

$scope.activity = data.DadosDosCursos

Because you are already emptying your $scope.activity array in the beginning of then and you are not modifying any structure of entries within data.DadosDosCursos ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.