Paging table with AngularJs

2

I was trying to put a pagination on my table, after a help I partially solved the problem, but it did not stay the way I expected.

I can already get the currentPage with each click on the < pagination> the problem is on how to load the data when page mute. The old form was using setFirstResult and setMaxResults of Hibernate but when I clicked on the next page it would always repeat the last line of the previous page on the new page.

What would be the best way to get the data according to the clicked page, what do I pass as a parameter for this query?

Front-end

Controller:

angular.module("oraculo").controller("colaboradorController", function($scope, $routeParams, $location, colaboradorAPI, colaboradores){

        $scope.tamanhoMaximo = 6;
        $scope.currentPage = 1;
        $scope.totalItems = 60;
        //Onde fica armazenada toda a consulta
        $scope.colaboradores = colaboradores.data;
        ...

Page:

...
</table>
<pagination total-items="totalItems" ng-model="currentPage" max-size="tamanhoMaximo" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages" ng-click="loading(currentPage)"></pagination>

Back-end

DAO:

@SuppressWarnings("unchecked")
    public List<Colaborador> listarColaboradores() {
        List<Colaborador> lista = new ArrayList<>();
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        Transaction transacao = null;
        try {
            transacao = sessao.beginTransaction();
            Query consulta = sessao.getNamedQuery("Colaborador.listar");
            lista = consulta.list();
            transacao.commit();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            sessao.close();
        }
        return lista;
    }

I was doing this way earlier:

DAO:

@SuppressWarnings("unchecked")
    public List<Colaborador> listarColaboradores(Integer paginaInicio, Integer count) {
        List<Colaborador> lista = new ArrayList<>();
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        Transaction transacao = null;

        try {
            transacao = sessao.beginTransaction();
            Query consulta = sessao.getNamedQuery("Colaborador.listar");
            consulta.setFirstResult(paginaInicio);
            consulta.setMaxResults(count);
            lista = consulta.list();
            transacao.commit();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            sessao.close();
        }
        return lista;
    }

Controller:

@Get
@Path("/colaboradores/{paginaInicio}")
    public void listarTodos(Integer paginaInicio) {
        result.use(Results.json())
       .withoutRoot()
       .from(colaboradorDAO.listarColaboradores(paginaInicio, 4))
       .serialize();
}

No Front-end :

Controller:

 $scope.loading = function(currentPage){
        console.log("currentPage: "+currentPage);
        colaboradorAPI.getColaboradores(currentPage).success(function(data){
            $scope.colaboradores = data;
        });
    }

Service:

var _getColaboradores = function(paginaInicio){
        return $http.get(config.baseURL + "/Oraculo/colaborador/colaboradores/" + paginaInicio);
};

In this way I call the function loading in ng-click of pagination , and pass currentPage . Then I pass currentPage as a parameter to back-end .

My Project: link

    
asked by anonymous 24.09.2015 / 19:55

1 answer

2

the> I have an example almost similar to yours except that in my case it is an infinite scroll similar to facebook.

Observe the following, when the user opens the page, 50 ($ scope.page.ini - > $ scope.page.end) / strong> ( $ scope.page.ini = 50, $ scope.page.end = 80, $ scope.page.ini = 80, $ scope.page.end = 110 and so on ) new records are displayed.

AppPeople.controller("PeopleController", function($scope, $people) {
    $scope.data = [];
    $scope.page = {ini : 0, end : 50, increment: 30};
    $scope.busy = false;

    $scope.search = function() {
        $scope.busy = true;
        $scope.promise = $people.query({ini:$scope.page.ini, end: $scope.page.end}, function(response) { 
            $scope.loadPeople(response);
            $scope.incrementPageNumber();
            $scope.busy = false;
        }).$promise;
    };

    $scope.incrementPageNumber = function(){
        $scope.page.ini  = $scope.page.end;
        $scope.page.end += $scope.page.increment;
    };

    $scope.loadPeople = function(people){
        angular.forEach(people, function(item, i) {
            $scope.data.push(item);
        });
    };
});

Following the click on the next page you will have:

  • setFirstResult = 0, setMaxResults = 50
  • setFirstResult = 50, setMaxResults = 80
  • setFirstResult = 80, setMaxResults = 110
  • setFirstResult = 110, setMaxResults = 130
  • setFirstResult = 130, setMaxResults = 140
  • Note: setFirstResult always receives the previous setMaxResults, except in the first query where it will be 0. SetMaxResults is always incremented every 30 seconds except the first time it will display 50.

    Final version: query.setFirstResult ((page -1) * 6); query.setMaxResults (page * 6);

        
    24.09.2015 / 20:06